使用参数(“按引用”参数)从 PowerShell 返回对象?

发布于 2024-10-19 18:41:08 字数 784 浏览 1 评论 0原文

我有一个 PowerShell (2.0) 脚本调用另一个脚本。我不仅希望收到主要输出,还希望收到一个可以单独使用的附加对象,例如在消息中显示摘要行。

让我们将 Test2.ps1 作为被调用的脚本:

param([String]$SummaryLine)
$Issues = "Potentially long list of issues"
$SummaryLine = "37 issues found"
$Issues

将 Test1.ps1 作为调用它的脚本:

$MainOutput = & ".\Test2.ps1" -SummaryLine $SummaryOutput
$MainOutput
$SummaryOutput

输出很简单:

Potentially long list of issues

虽然参数 $SummaryLine 由 Test2 填充,但 $SummaryOutput 在 Test1 中仍然未定义。

在调用 Test2 之前定义 $SummaryOutput 没有帮助;它只是保留调用 Test2 之前分配的值。

我尝试将 $SummaryOutput 和 $SummaryLine 设置为 [ref] 变量(显然可以使用函数),但调用 Test2 后 $SummaryOutput.Value 属性为 $null。

PowerShell 是否可以在参数中返回值?如果没有,有哪些解决方法?在 Test2 中直接分配父级范围的变量?

I have one PowerShell (2.0) script calling another. I want to receive back not only the main output, but an additional object that I can use separately, e.g. to display a Summary Line in a message.

Let's have Test2.ps1 as the script being called:

param([String]$SummaryLine)
$Issues = "Potentially long list of issues"
$SummaryLine = "37 issues found"
$Issues

And Test1.ps1 as the script that calls it:

$MainOutput = & ".\Test2.ps1" -SummaryLine $SummaryOutput
$MainOutput
$SummaryOutput

The output is simply:

Potentially long list of issues

Although the parameter $SummaryLine is filled in by Test2, $SummaryOutput remains undefined in Test1.

Defining $SummaryOutput before calling Test2 doesn't help; it just retains the value assigned before calling Test2.

I've tried setting up $SummaryOutput and $SummaryLine as a [ref] variables (as one can apparently do with functions), but the $SummaryOutput.Value property is $null after calling Test2.

Is it possible in PowerShell to return a value in a parameter? If not, what are the workarounds? Directly assigning a parent-scoped variable in Test2?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

失去的东西太少 2024-10-26 18:41:08

参考应该有效,你不会说你尝试时发生了什么。下面是一个示例:

Test.ps1:

Param ([ref]$OptionalOutput)

"Standard output"
$OptionalOutput.Value = "Optional Output"

运行它:

$x = ""
.\Test.ps1 ([ref]$x)
$x

这是您可能更喜欢的替代方案。

Test.ps1:

Param ($OptionalOutput)

"Standard output"
if ($OptionalOutput) {
    $OptionalOutput | Add-Member NoteProperty Summary "Optional Output"
}

运行它:

$x = New-Object PSObject
.\Test.ps1 $x
$x.Summary

Ref should work, you don't say what happened when you tried it. Here is an example:

Test.ps1:

Param ([ref]$OptionalOutput)

"Standard output"
$OptionalOutput.Value = "Optional Output"

Run it:

$x = ""
.\Test.ps1 ([ref]$x)
$x

Here is an alternative that you might like better.

Test.ps1:

Param ($OptionalOutput)

"Standard output"
if ($OptionalOutput) {
    $OptionalOutput | Add-Member NoteProperty Summary "Optional Output"
}

Run it:

$x = New-Object PSObject
.\Test.ps1 $x
$x.Summary
美人迟暮 2024-10-26 18:41:08

这更接近你想做的事情吗?

Test2.ps1

 $Issues = "Potentially long list of issues"
 $SummaryLine = "37 issues found"
 $Issues
 $SummaryLine

Test1.ps1

 $MainOutput,$SummaryOutput = & ".\Test2.ps1" 
 $MainOutput 
 $SummaryOutput

 param([String]$SummaryLine)
 $Issues = "Potentially long list of issues"
 $SummaryLine = "37 issues found"
 $Issues

是不合理的。您正在为 $SummaryLine 传递一个参数,然后立即将其替换为“发现 37 个问题”。该变量仅存在于被调用脚本运行的范围内。该脚本一完成,它就消失了。如果您想稍后使用它,则需要输出它并将其保存到调用脚本中的变量中。

Is this closer to what you want to do?

Test2.ps1

 $Issues = "Potentially long list of issues"
 $SummaryLine = "37 issues found"
 $Issues
 $SummaryLine

Test1.ps1

 $MainOutput,$SummaryOutput = & ".\Test2.ps1" 
 $MainOutput 
 $SummaryOutput

This:

 param([String]$SummaryLine)
 $Issues = "Potentially long list of issues"
 $SummaryLine = "37 issues found"
 $Issues

Is irrational. You're passing a parameter for $SummaryLine, and then immediatly replacing it with "37 issues found". That variable only exists in the scope the called script is running in. As soon as that script finishes, it's gone. If you want to use it later, you need to output it and save it to a variable in your calling script.

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