Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
831 views
in Technique[技术] by (71.8m points)

powershell - Why won't Variable update?

I'm currently editing our new employee script for AD and I am running into an issue, I added 2 radio buttons for the home folder, one radio button will update the BaseHomeFolderPath to Path1 and the second will update it to Path2, I export the variable to a csv to verify that it works but it keeps showing up blank. I don't know if it is a scope issue or what I am doing wrong, any help will be greatly appreciated! Below is a copy of the code.

I tried using $script: and it didn't work either.

$BaseHomeFolderPath = ''

Set-Variable -Name $BaseHomeFolderPath -Scope Global

$radiobuttonAtlas_MouseClick = [System.Windows.Forms.MouseEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.MouseEventArgs]
    #TODO: Place custom script here
    $BaseHomeFolderPath = '\path1users'
}


$radiobuttonCerberus_MouseClick=[System.Windows.Forms.MouseEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.MouseEventArgs]
    #TODO: Place custom script here
    $BaseHomeFolderPath = '\path2users'
}

$buttonRun_MouseClick=[System.Windows.Forms.MouseEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.MouseEventArgs]
    #TODO: Place custom script here
    $TestValue = New-Object System.Object
    $TestValue | Add-Member -MemberType NoteProperty -Name "Path" -Value $BaseHomeFolderPath
    $TestValue | Export-CSV -NoTypeInformation -Path "C:UsersestuserDesktopTestcsv.csv"
}

I expect the BaseHomeFolderPath variable to be updated to whichever radio button is selected.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

tl;dr

In order to update the $BaseHomeFolderPath variable in the script scope (or any scope other than the local one), you must reference it in that scope explicitly:

$script:BaseHomeFolderPath = '\path1users'

Otherwise, without a scope specifier such as $script:, you'll implicitly create a new variable by that name in the current scope, which in your case is the child scope in which your event handlers run.


In PowerShell, when you assign to a variable with $var = ..., you either:

  • update a preexisting variable that was created in the same scope.

  • or implicitly create a new variable in the current scope.

The tricky part is that even though child scopes see variables created in parent scopes and can get their value by name only, assigning to them by name only creates a new, scope-local variable, and that new variable then shadows the original one in the current scope and all child scopes.

A simple demonstration, using call operator & to execute a script block ({ ... }) in a child scope:

$var = 'parent'
"in parent: before: $var"
& {
  "in child: before: $var" # sees $var from parent scope 
  $var = 'child'           # creates new $var in current scope
  "in child: after: $var"  # sees new $var, which shadows the parent's
}
"in parent: after: $var"   # still has original value

This prints:

in parent: before: parent
in child: before: parent
in child: after: child
in parent: after: parent

Note:

  • In addition to fixed-target scope specifiers $script: and $global:, you can use the Get-Variable / Set-Variable cmdlets with the -Scope parameter to target variables in scopes relative to the current one (up the call stack).

  • To promote modularity and maintainability, it's generally better to avoid accessing variables across scope boundaries - best to pass values around instead.

For more information, see:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...