[英]Difference between Set-Variable foo -Scope Global -Value “bar” and $global:foo = “bar”
What is the difference between Set-Variable foo -Scope Global -Value "bar" and $global:foo = "bar" Set-Variable foo -Scope Global -Value“bar”和$ global:foo =“bar”之间有什么区别?
I have seen that Set-Variable implements -WhatIf/-Confim so the assignment only happens if these are not set or the action is confirmed whereas the assignment happens regardless. 我已经看到Set-Variable实现了-WhatIf / -Confim,因此只有在未设置或动作得到确认的情况下才会进行赋值,而无论如何都会进行赋值。 Is there anything else lurking?
还有潜伏的东西吗?
UPDATE UPDATE
Following is an example of how the 2 are different in the presence of -Whatif / -Confirm: 以下是在-Whatif / -Confirm存在时2如何不同的示例:
Clear-Variable foo -ErrorAction:SilentlyContinue # just in case
Function repro {
[CmdletBinding(SupportsShouldProcess)] Param()
Set-Variable foo -Scope Global -Value "bar"
}
repro -WhatIf
Write-Host "`$global:foo=$($global:foo)"
You should see: 你应该看到:
What if: Performing the operation "Set variable" on target "Name: foo Value: bar".
$global:foo=
However: 然而:
Clear-Variable foo -ErrorAction:SilentlyContinue # just in case
Function repro {
[CmdletBinding(SupportsShouldProcess)] Param()
$global:foo = "bar"
}
repro -WhatIf
Write-Host "`$global:foo=$($global:foo)"
Now you get: 现在你得到:
$global:foo=bar
The difference isn't much but the Set-Variable
has a few more options available to it than just a regular assignment expression. 差异并不大,但
Set-Variable
有一些可用的选项,而不仅仅是常规赋值表达式。 It's just a matter of how verbose you want to be with your syntax and what you are trying to accomplish. 这只是一个问题,你希望如何使用你的语法以及你想要完成的事情。
These are simple, quick variable assignments not much to it. 这些是简单,快速的变量赋值,对它来说并不多。
$foo = "bar"
# If you want to change scope, add it
$global:foo = "bar"
$script:foo = "bar"
Set-Variable
CmdLet Set-Variable
CmdLet As you've pointed out, the Set-Variable
Cmdlet has a few more options with it, like -WhatIf
and -Confirm
. 正如您所指出的,
Set-Variable
Cmdlet还有一些选项,比如-WhatIf
和-Confirm
。 It also has a few other options like setting the type of variable with -Option
and -PassThru
. 它还有一些其他选项,比如使用
-Option
和-PassThru
设置变量类型。 -Option
allows for the setting of constants, readonly variables, or private scoped variables. -Option
允许设置常量,只读变量或私有范围变量。 -PassThru
is pretty useful to and I'll give an example below. -PassThru
非常有用,我将在下面给出一个例子。 A couple more parameters are also available to so review them here 还有更多参数供您在此处查看
# PassThru executes your Set-Variable and then continues to
# pass the variable through the pipeline so with it you can
# assign your variable and then immediately start working with it
Set-Variable -Name "FooArr" -Value @("bar", "baz", "boom") -PassThru | ForEach-Object {
$_.Value
}
$FooArr # Still assigned
bar
baz
boom
# Constant
Set-Variable -Name "ImAConstant" -Value "Try to delete me" -Option Constant
Remove-Variable -Name "ImAConstant"
# Error
Remove-Variable : Cannot remove variable ImAConstant because it is constant or
read-only. If the variable is read-only, try the operation again specifying the Force
option.
At line:2 char:1
+ Remove-Variable -Name "ImAConstant"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (ImAConstant:String) [Remove-Variable], Ses
sionStateUnauthorizedAccessException
+ FullyQualifiedErrorId : VariableNotRemovable,Microsoft.PowerShell.Commands.Remo
veVariableCommand
Set-Variable -Name "ImReadOnly" -Value "Delete me" -Option ReadOnly
Remove-Variable -Name "ImReadOnly" -Force # Removed
These are just a few examples. 这只是几个例子。 Pretty cool stuff.
很酷的东西。
In your function repro
, you use SupportsShouldProcess
in your CmdLetBinding()
but you still need to block it up for your assignment with some code for it to run the way you are thinking. 在你的函数
repro
,你用SupportsShouldProcess
在你CmdLetBinding()
但你仍然需要一些代码来阻止它为您的任务来运行它,你所想。
Function repro {
[CmdletBinding(SupportsShouldProcess)] Param()
if ($pscmdlet.ShouldProcess('$global:foo', "Sets Variable")){
$global:foo = "bar"
}
}
This will produce the same result as your original Set-Variable
call with -WhatIf
. 这将产生与使用
-WhatIf
原始Set-Variable
调用相同的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.