为什么变量不会更新? [英] Why won't Variable update?

查看:52
本文介绍了为什么变量不会更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在为 AD 编辑我们的新员工脚本,但遇到了一个问题,我为主文件夹添加了 2 个单选按钮,一个单选按钮将 BaseHomeFolderPath 更新为 Path1,第二个将其更新为 Path2,我将变量导出到 csv 以验证它是否有效,但它一直显示为空白.我不知道这是一个范围问题还是我做错了什么,任何帮助将不胜感激!下面是代码的副本.

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.

我尝试使用 $script: 也没有奏效.

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 = '\\path1\users'
}


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

$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:\Users\testuser\Desktop\Testcsv.csv"
}

我希望 BaseHomeFolderPath 变量更新为选中的任何单选按钮.

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

推荐答案

tl;dr

为了更新 script 范围(或除 local 之外的任何范围)中的 $BaseHomeFolderPath 变量,您必须引用它明确地在该范围内:

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 = '\\path1\users'

否则,如果没有诸如 $script: 之类的范围说明符,您将在 current 范围,在您的情况下是您的事件处理程序运行的子范围.

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.

在 PowerShell 中,当您使用 $var = ... 分配给变量时,您可以:

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

  • 更新在相同范围内创建的预先存在的变量.

或隐式创建一个新变量在当前作用域.

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

打印:

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

注意:

  • 除了 fixed-目标范围说明符 $script:$global: 之外,您还可以使用 带有 -Scope 参数的 Get-Variable/Set-Variable cmdlet 以相对作用域中的变量为目标(向上调用堆栈).

  • 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:

这个答案的最后一部分,提供了一个简洁的总结.

The last section of this answer, which provides a concise summary.

这篇关于为什么变量不会更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆