Add new custom path variable

Hi everyone, i'm bothering you again. I'm currently trying to integrate my own variables. I'm using these in PSADT version 3.8.4.

AppDeployToolkitMain.ps1
image

I tried this, but nothing happened. Does anyone have any ideas?

$envSystemProfileX86 = Join-Path -Path $adtSession.DirWindows -ChildPath 'System32\config\systemprofile'
$envSystemProfileX64 = Join-Path -Path $adtSession.DirWindows -ChildPath 'sysWOW64\config\systemprofile'
$envDefault = Join-Path -Path $adtSession.DirSystemDrive -ChildPath 'Users\Default'

cu
bolle

I do this in my shop.

There are 2 ways to do this.

  1. Add properties to the $adtSession variable:
$adtSession = @{
    # App variables.
    envSystemProfileX86 = Join-Path -Path $adtSession.DirWindows -ChildPath 'System32\config\systemprofile'
    envSystemProfileX64 = Join-Path -Path $adtSession.DirWindows -ChildPath 'sysWOW64\config\systemprofile'
    envDefault = Join-Path -Path $adtSession.DirSystemDrive -ChildPath 'Users\Default'
..

Then use them like this:
$adtSession.envSystemProfileX86
or
$($adtSession.envSystemProfileX86)

.
.

  1. Create Global variables:

You could define them AFTER the session is opened in the Initialization section

    ...
    # Open a new deployment session, replacing $adtSession with a DeploymentSession.
    $iadtParams = Get-ADTBoundParametersAndDefaultValues -Invocation $MyInvocation
    $adtSession = Remove-ADTHashtableNullOrEmptyValues -Hashtable $adtSession
    $adtSession = Open-ADTSession @adtSession @iadtParams -PassThru

    # My Global variables
    [String]$Global:envSystemProfileX86 = Join-Path -Path $adtSession.DirWindows -ChildPath 'System32\config\systemprofile'
    [String]$Global:envSystemProfileX64 = Join-Path -Path $adtSession.DirWindows -ChildPath 'sysWOW64\config\systemprofile'
    [String]$Global:envDefault = Join-Path -Path $adtSession.DirSystemDrive -ChildPath 'Users\Default'
}
catch
...

Then use them like this:
$Global:envSystemProfileX86
and sometimes this works too:
$envSystemProfileX86

FYI: Mitch does not likes us using $Global: variables in scripts.

1 Like

YouKnowMeSoWellCloseGIF

The main reason for not liking the use of globals is because they're usually always never necessary, and if you are, you have fundamental architectural issues in whatever you're doing.

For instance, with the below:

Why do they need to be global at all? They'll be accessible throughout your entire script without globally scoping as child scopes can access variables in parent scopes. None of the environment variables we export are global and no one's reported an issue. See? Not necessary :sign_of_the_horns::grin:

By the way, if those variables are of legitimate use to anyone, there's no problem adding them into the toolkit either. We haven't shipped 4.2.0 yet, nothing's too late.

Hello,
if I enter the variables as shown in the image, the script does not start and terminates immediately.

@mjr4077au I would be very happy if the 3 variables were integrated into the new version. :grinning_face:

cu
bolle

It's because you're trying to reference values within $adtSession before the object is even created. I only it was in an example above but it's not correct and the error should have been fairly explicit about the issue.

You'll have to drop the $adtSession stuff and use the Environment Variables directly:

envSystemProfileX86 = Join-Path -Path $Env:Windir -ChildPath 'System32\config\systemprofile'
envSystemProfileX64 = Join-Path -Path $Env:Windir -ChildPath 'sysWOW64\config\systemprofile'
envDefault = Join-Path -Path $Env:SystemDrive -ChildPath 'Users\Default'

I haven't yet had time to make the shift to the new 4.x version of PSADT so maybe I'm missing something obvious here but OP says he's doing this in PSADT v3.8.4. Searching my PSADT archives, the variable "$adtSession" does not exist in v3.8.4 (or any of the old versions I have).

In v4, $adtSession is a massive hash table that becomes an object when the session opens.

You can use it like purse for custom variables as it's virtually guaranteed to exist in your custom functions.

1 Like

It's true, I'm currently still using version 3.8.4, but I want to switch to version 4.1.8. The variables are from the old version and I'd like to integrate them into the new version.

With your adjustments to $adtsession, the script runs without errors again, but it still doesn't seem to find the path. I created a test folder under System Profile x64. When I try to delete it using the variable, nothing happens.

We can only help if we see what command you used.

Hi everyone,

I've added this now. But as I said, it's not working with the variable. I tried it with and without $.

image

cu
bolle

You would want something like:
Remove-FolderIfEmtpy -Path $($adtsession.envSystemProfileX86)

2 Likes

Thank you its works :+1:

1 Like

Added via Add in `envSystemProfile` environment variables as requested. · PSAppDeployToolkit/PSAppDeployToolkit@9f29351 · GitHub and Add in `defaultUserProfile` environment variables as requested. · PSAppDeployToolkit/PSAppDeployToolkit@a4ce105 · GitHub.

2 Likes