如何通过PowerShell中的功能更改脚本划分的变量?
让脚本
[int] $num01 = 2
function setNo ([int] $num02)
{
$num01 = $num02;
}
write-host $num01
setNo -num02 4
write-host $num01
setNo -num02 6
write-host $num01
产生输出
2
2
2
如何使其产生输出:
2
4
6
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有几种方法可以改变函数范围之外的变量值,例如:
set-variable
带有-scope脚本
参数或$ executionContext
:请注意,请注意,在此中,特定情况,以上是必要的,因为外部变量
$ num01
是 scalarar ,如果是 non-scalar < /em>,即阵列或哈希表:There are several ways of altering the value of a variable outside the function's scope, for example:
Set-Variable
with the-Scope Script
argument or$ExecutionContext
:Note that, in this particular case, one of the above is necessary because the outside variable
$num01
is a scalar, it would not be the case if it was a non-scalar, i.e. an array or hash table:它是
$ script:num01 = $ num02
正如圣地亚哥·斯帕尔顿(Santiago Squarzon)在评论中所写。
it's
$script:num01 = $num02
as Santiago Squarzon wrote in comments.