简体   繁体   English

如何在PowerShell中使用Set-Variable声明全局变量?

[英]How to declare global variable using Set-Variable in PowerShell?

Consider the below code: 考虑下面的代码:

Set-Variable -Name session -Value $null -Scope Global

function Manage-SecondAdmin {
    Close-Session
    $session = Open-Session
}

$session = Open-Session
Manage-SecondAdmin
$x = Invoke-Session $session
# Outputs success string or nothing in case of failure
if ($x) {
    # Does not come here
    Write-Host "Success"
} else {
    # Comes here
    Write-Host "Invalid session ID"
    $session = Open-Session
}
$x = Invoke-Session $session
# Now successful response

When I use the above code, it always go to else part as explained in the command. 当我使用上面的代码时,它总是按照命令中的说明转到其他部分。 I am aware of the keyword 'global'. 我知道关键字“全局”。 Is it needed when I use 'Set-Variable'? 使用“设置变量”时是否需要? What is the best approach for this? 最好的方法是什么?

$session in Manage-SecondAdmin is a different variable than $global:session . Manage-SecondAdmin中的$session是与$global:session不同的变量。 Apparently Close-Session (whatever that cmdlet might be) closes the existing session, and the subsequent Open-Session opens a new one, which is then assigned it to the local variable $session in the scope of the function while the global variable $session still holds the reference to the now closed session. 显然, Close-Session (无论该cmdlet可能是什么)都会关闭现有会话,随后的Open-Session将打开一个新Open-Session ,然后将其分配给函数范围内的局部变量$session ,而全局变量$session仍然保留对现在关闭的会话的引用。 You could probably fix that by changing $session to $global:session , but manipulating global variables in nested contexts is a bad practice. 您可能可以通过将$session更改$session $global:session $session来解决此问题,但是在嵌套上下文中操作全局变量是一种不好的做法。 Don't do that. 不要那样做

Have the function return the new session and assign the return value to a variable where the function is called: 让函数返回新会话,并将返回值分配给调用该函数的变量:

function Manage-SecondAdmin {
    Close-Session
    Open-Session
}

$session = Open-Session
$session = Manage-SecondAdmin
...

Pre-defining your variables with Set-Variable normally isn't required in PowerShell. 在PowerShell中,通常不需要使用Set-Variable预定义变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Powershell Set-Variable 重置变量而不是更新它 - Powershell Set-Variable resets Variable instead of updating it 集变量Powershell。 如何将变量链接到exe文件? - Set-Variable powershell. How do I link a variable to an exe file? Azure DevOps 条件无法评估 Powershell 设置变量 - Azure DevOps conditional can't evaluate Powershell set-variable 无法使用Set-Variable设置变量的Description属性 - Unable to set Description property of a variable using Set-Variable PowerShell 中的“$myvariable =”和 Set-Variable 有什么不同? - What's different between "$myvariable =" and Set-Variable in PowerShell? 在 Powershell 中设置变量不覆盖脚本参数? 尝试使用 JSON 读取 JSON 并覆盖脚本参数 - In Powershell set-variable not overriding Script Parameters? Trying to read JSON and override script parameters using JSON Set-Variable foo -Scope Global -Value“bar”和$ global:foo =“bar”之间的区别 - Difference between Set-Variable foo -Scope Global -Value “bar” and $global:foo = “bar” 在循环中使用Set-Variable设置子值(例如$ Myvar.item.value)的问题 - Issues using Set-Variable to set sub values (ex. $Myvar.item.value) in loop 如何在Powershell中定义全局变量 - How to define a global variable in Powershell 如何设置全局变量和 - How to set global variable and
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM