Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell变量作用域_Powershell_Variables_Scope_Global Variables - Fatal编程技术网

Powershell变量作用域

Powershell变量作用域,powershell,variables,scope,global-variables,Powershell,Variables,Scope,Global Variables,在脚本中使用脚本:作用域和全局:作用域时,我很难理解它们之间的区别 有人能给我举个例子,其中一个全局作用域变量保留它的值,而scipt作用域变量不保留它的值吗?如果有疑问,请阅读: 全球: Windows PowerShell启动时有效的作用域。Windows PowerShell启动时存在的变量和函数已在全局范围内创建。这包括自动变量和偏好变量。这还包括Windows PowerShell配置文件中的变量、别名和函数。 […] 脚本: 脚本文件运行时创建的作用域。只有脚本中的命令才能在脚本范围

在脚本中使用
脚本:
作用域和
全局:
作用域时,我很难理解它们之间的区别

有人能给我举个例子,其中一个全局作用域变量保留它的值,而scipt作用域变量不保留它的值吗?

如果有疑问,请阅读:

全球:
Windows PowerShell启动时有效的作用域。Windows PowerShell启动时存在的变量和函数已在全局范围内创建。这包括自动变量和偏好变量。这还包括Windows PowerShell配置文件中的变量、别名和函数。
[…]
脚本:
脚本文件运行时创建的作用域。只有脚本中的命令才能在脚本范围内运行。对于脚本中的命令,脚本范围是本地范围

例如:

PS C:\> $foo = 'a' PS C:\> Get-Content .\test.ps1 "global 1: $global:foo" "script 1: $script:foo" $foo = 'b' # <- this modifies $foo in the script scope "global 2: $global:foo" "script 2: $script:foo" $global:foo = 'c' # <- this modifies $foo in the global scope "global 3: $global:foo" "script 3: $script:foo" PS C:\> .\test.ps1 global 1: a script 1: global 2: a script 2: b global 3: c script 3: b PS C:\> $foo c PS C:\>$foo='a' PS C:\>获取内容。\test.ps1 “全球1:$global:foo” “脚本1:$script:foo” $foo='b'#$foo
当然我读过了。还是不明白。问题是:在脚本中同时使用它们是否有区别。在你的例子中你没有这样做。嗯。。。是的,真的吗?您确实在脚本外部分配了“a”。而且在该脚本中未使用$script:abc=“a”或$global:xyz=“b”。我想了解,如果它有区别,如果我在脚本中为脚本:-scope或全局:-scope分配(!)一个变量,你认为你能自己测试这样简单的事情吗?我愿意解释这些东西,但我绝对不会用勺子喂你这些文档。