powershell 将 ps1 脚本的输出分配给变量

发布于 2024-12-12 05:23:24 字数 401 浏览 0 评论 0原文

首先,我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

粉红×色少女 2024-12-19 05:23:24

我会拥抱 PowerShell 的面向对象特性,而不是输出像“9/10”这样的字符串,而是在脚本输出中创建一个具有 NumActiveNodes 和 TotalNodes 等属性的对象,如下所示:

new-object psobject -Property @{NumActiveNodes = 9; TotalNodes = 10}

当然,用动态值替换 num active 和总节点数。请注意,未捕获的对象将自动出现在脚本的输出中。然后,如果这是您的脚本的唯一输出,您可以执行以下操作:

$obj = .\MyScript.ps1
$obj.NumActiveNodes
9
$obj.TotalNodes
10

这将使那些使用脚本输出的人变得更好。事实上,输出在某种程度上是自我记录的,例如:

C:\PS> .\MyScript.ps1

NumActiveNodes        TotalNodes
--------------        ----------
             9        10

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:

new-object psobject -Property @{NumActiveNodes = 9; TotalNodes = 10}

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:

$obj = .\MyScript.ps1
$obj.NumActiveNodes
9
$obj.TotalNodes
10

It will make it nicer for those consuming the output of your script. In fact the output is somewhat self-documenting e.g.:

C:\PS> .\MyScript.ps1

NumActiveNodes        TotalNodes
--------------        ----------
             9        10

P.S. When did StackOverflow start sucking so badly at formatting PowerShell script?

扭转时空 2024-12-19 05:23:24

如果您不想更改脚本(并假设仅 $avail_count/$total_count 行是由脚本编写的),您可以执行以下操作:

$var= powershell .\MyScript.ps1

或者只需删除 write-host< /code> 并且只有 $avail_count/$total_count

然后执行:

$var = .\MyScript.ps1

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:

$var= powershell .\MyScript.ps1

Or just drop the write-host and have just $avail_count/$total_count

and then do:

$var = .\MyScript.ps1
掌心的温暖 2024-12-19 05:23:24

您可以在脚本中执行 $global:foobar ,它将在脚本关闭后持续存在

you could just do a $global:foobar in your script and it will persist after the script is closed

无畏 2024-12-19 05:23:24

我知道,这个问题有点老了,但它可能会帮助某人找到正确的答案。

我在使用另一个 PS 脚本执行 PS 脚本并将输出保存到变量中时遇到了类似的问题,这里有 2 个非常好的答案:

  1. Mathias
  2. mklement0

希望有帮助!

如果有的话请为他们投票,因为他们真的很棒!

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:

  1. Mathias
  2. mklement0

Hope it helps!

Please up-vote them if so, because they are really good!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文