定义全局变量

 在PS2.0下,button 可以直接调用在 checkbox里面定义的变量,如下:

$checkbox1_CheckedChanged={
#TODO: Place custom script here
if ($checkbox1.Checked) { $a= 1 }
else { $a = 0 }

}

$button1_Click={
#TODO: Place custom script here
Write-Host $a
}

当checkbox1 被选中,点击 button1按钮后,$a返回值为1;当checkbox1 未被选中,点击 button1按钮后,$a返回值为0

但是在PS3.0和4.0下,无论checkbox1 是否被选中,点击 button1按钮后,$a均没有返回值。换句话说就是无法直接调用在 checkbox里面定义的变量。

解决方法为定义全局变量,如下:

$Global:a = 0

$checkbox1_CheckedChanged={
#TODO: Place custom script here
if ($checkbox1.Checked) { $global:a = 1 }
else { $global:a = 0}
}

$button1_Click={
#TODO: Place custom script here
Write-Host $global:a   #此处写为 Write-Host $a 效果一样,只要在 checkbox1下定义为全局变量即可。
}

当checkbox1 被选中,点击 button1按钮后,$a返回值为1;当checkbox1 未被选中,点击 button1按钮后,$a返回值为0

原文地址:https://www.cnblogs.com/dreamer-fish/p/3913736.html