[英]How to define a global variable in Powershell
I am trying to define a variable globally in powershell and get the value in one function and pass that value in different function but I am unable to do so.我试图在 powershell 中全局定义一个变量,并在一个函数中获取该值并在不同的函数中传递该值,但我无法这样做。 I googled and tried $global:myvariable but its not working.
我用谷歌搜索并尝试了 $global:myvariable 但它不起作用。 what wrong am i doing?
我做错了什么?
here is my code:这是我的代码:
$global:namerel = $null
Function GET-value{
$uriAccount = $orz + "_apis/release/releases/1914?api-version=6.0"
$responseRels = Invoke-RestMethod -Uri $uriAccount -Method get -Headers $AzureDevOpsAuthenicationHeader
$namerel = $responseRels.Name
write-host $namerel # it prints the required value
}
Function GET-rel{
$test = GET-value
write-host $namerel # nothing gets printed. its blank
}
To use a variable scoped global
, you need to use syntax $global:namerel
inside the functions.要使用变量作用域
global
,您需要在函数内使用语法$global:namerel
。 Without that, variable $namerel
will be just a new variable, local to that function.没有它,变量
$namerel
将只是一个新变量,该函数的局部变量。
Also, you may not need to use global
.此外,您可能不需要使用
global
。 Scope script
usually is broad enough.范围
script
通常足够广泛。 See About_scopes请参阅About_scopes
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.