powershell 将 ps1 脚本的输出分配给变量
首先,我对 powershell 和编程非常陌生。我有一个 powershell 脚本,它接受一些参数并输出一个值。 脚本的结果将类似于 9/10,其中 9 是节点总数中活动的数量。我想将输出分配给一个变量,这样我就可以根据该值调用另一个脚本。
这是我尝试过的,但它不起作用:
$active = (./MyScript.ps1 lb uid **** site)
我还尝试了以下操作,似乎为变量分配了一个空字符串
$active = (./MyScript.ps1 lb uid **** site | out-string)
在这两种情况下,它们都会运行并立即给我值,而不是将其分配给变量。当我调用该变量时,我没有得到任何数据。
Let me start with I am very new to powershell and programming for that matter. I have a powershell script that takes some arguments and that outputs a value.
The result of the script is going to be something like 9/10 where 9 would be the number active out of the total amount of nodes. I want to assign the output to a variable so I can then call another script based on the value.
This is what I have tried, but it does not work:
$active = (./MyScript.ps1 lb uid **** site)
I have also tried the following which seems to assign the variable an empty string
$active = (./MyScript.ps1 lb uid **** site | out-string)
In both cases they run and give me the value immediately instead of assigning it to the variable. When I call the variable, I get no data.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我会拥抱 PowerShell 的面向对象特性,而不是输出像“9/10”这样的字符串,而是在脚本输出中创建一个具有 NumActiveNodes 和 TotalNodes 等属性的对象,如下所示:
当然,用动态值替换 num active 和总节点数。请注意,未捕获的对象将自动出现在脚本的输出中。然后,如果这是您的脚本的唯一输出,您可以执行以下操作:
这将使那些使用脚本输出的人变得更好。事实上,输出在某种程度上是自我记录的,例如:
PS StackOverflow 什么时候开始在格式化 PowerShell 脚本方面如此糟糕?
I would embrace PowerShell's object-oriented nature and rather than output a string like "9/10", create an object with properties like NumActiveNodes and TotalNodes e.g. in your script output like so:
Of course, substitute in the dynamic values for num active and total nodes. Note that uncaptured objects will automatically appear on your script's output. Then, if this is your scripts only output, you can do this:
It will make it nicer for those consuming the output of your script. In fact the output is somewhat self-documenting e.g.:
P.S. When did StackOverflow start sucking so badly at formatting PowerShell script?
如果您不想更改脚本(并假设仅
$avail_count/$total_count
行是由脚本编写的),您可以执行以下操作:或者只需删除
write-host< /code> 并且只有
$avail_count/$total_count
然后执行:
If you don't want to change the script ( and assuming only that
$avail_count/$total_count
line is written by the script), you can do:Or just drop the
write-host
and have just$avail_count/$total_count
and then do:
您可以在脚本中执行
$global:foobar
,它将在脚本关闭后持续存在you could just do a
$global:foobar
in your script and it will persist after the script is closed我知道,这个问题有点老了,但它可能会帮助某人找到正确的答案。
我在使用另一个 PS 脚本执行 PS 脚本并将输出保存到变量中时遇到了类似的问题,这里有 2 个非常好的答案:
希望有帮助!
如果有的话请为他们投票,因为他们真的很棒!
I know, the question is a bit older, but it might help someone to find the right answer.
I had the similar problem with executing PS script with another PS script and saving the output into variable, here are 2 VERY good answers:
Hope it helps!
Please up-vote them if so, because they are really good!