Powershell 脚本中哪些行返回输出?

发布于 2024-11-28 21:33:10 字数 260 浏览 0 评论 0原文

一般来说,是否有一种方便的方法来确定 PowerShell 脚本/函数的哪些行正在返回值(“未捕获”)?我希望有一种方法可以在调试时查询要返回值的当前状态。我可以在每一行之后检查它,看看添加了哪些行。

我有一些脚本正在工作,有些行正在将我的返回值转换为 Object[]。我通常将这些行通过管道传输到 Out-Null 来解决这种情况。我只想返回一个对象(我在函数末尾选择的对象)。

有些行是 Cmdlet 调用,有些是对其他函数的调用,有些是对 .NET 对象的函数调用。

In general, is there a convenient way to figure out which lines of a PowerShell script/function are returning values (are "uncaptured")? I was hoping there was a way to query the current state of the to-be-returned value while debugging. I can check it after each line to see which lines add to it.

I have some scripts at work and some lines are turning my return value into an Object[]. I usually pipe such lines to Out-Null to fix the situation. I only want one object returned (the one I pick at the end of the function).

Some of the lines are Cmdlet calls, some are calls to other functions, and some are function calls on .NET objects.

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

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

发布评论

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

评论(2

佼人 2024-12-05 21:33:10

我想您可以使用 Set-PsDebug -trace 1 来查看哪一行返回它们。

示例:

考虑下面的脚本:

function f{
1..10
}

function g{
f
return 11
}

g

执行 Set-PsDebug -trace 1 后,跟踪将如下所示:

DEBUG:    1+  <<<< .\test.ps1
DEBUG:    1+ function f <<<< {
DEBUG:    5+ function g <<<< {
DEBUG:   10+  <<<< g
DEBUG:    6+  <<<< f
DEBUG:    2+ 1.. <<<< 10
1
2
3
4
5
6
7
8
9
10
DEBUG:    7+ return <<<<  11
11

显然,您可以看到输出来自何处。但如果输出被捕获,你就不会得到这个。

另外,如果您不关心返回的其他对象,而只想获取使用 return 语句返回的最后一个对象,您始终可以执行类似 (func )[-1]func |选择 -last 1 (如评论中指出的)以获取最后一个。

I suppose you can use Set-PsDebug -trace 1 to see which line is returning them.

Example:

Consider the script below:

function f{
1..10
}

function g{
f
return 11
}

g

After doing Set-PsDebug -trace 1, the trace would be something like below:

DEBUG:    1+  <<<< .\test.ps1
DEBUG:    1+ function f <<<< {
DEBUG:    5+ function g <<<< {
DEBUG:   10+  <<<< g
DEBUG:    6+  <<<< f
DEBUG:    2+ 1.. <<<< 10
1
2
3
4
5
6
7
8
9
10
DEBUG:    7+ return <<<<  11
11

Clearly, you can see where the output is coming from. But if the output is captured, you wouldn't get this.

Also, if you don't care about the other objects that are being returned and only want to get the last one that you returned with the return statement, you can always do something like (func)[-1] or func | select -last 1 ( as pointed out in the comment) to get the last one.

烙印 2024-12-05 21:33:10

为了完成可能的答案,我想添加 2 个注释:

首先,如果您使用 func | select -last 1,如果返回数组本身,则必须将返回的对象包装到数组中。为什么?看一个失败的示例:

function MyOutputs {
    $list = new-Object Collections.ArrayList
    $list.Add('first')
    $list.Add('second')
    $list
}
myoutputs | Select -last 1 #doesn't work

其次,如果您不确切知道哪些命令返回输出,您可以像这样将它们全部Out-Null

function MyOutputs {
    . { 
        $list = new-Object Collections.ArrayList
        $list.Add('first')  # returns index
        $list.Add('second') # returns index
    } | Out-Null
    write-Host Returning...
    $list
}
$a = MyOutputs
Write-Host Result is
$a

只需尝试将 ArrayList 代码放在脚本块之外,您就会发现看看它做了什么。使用 . 符号在脚本块内运行它意味着脚本块在当前范围内执行。 Out-Null 只是吃掉 Add 方法的输出。

To complete the possible answer, I'd like to add 2 notes:

First, if you use func | select -last 1, you have to wrap returned object to array, if you return array itself. Why? Look at a failing sample:

function MyOutputs {
    $list = new-Object Collections.ArrayList
    $list.Add('first')
    $list.Add('second')
    $list
}
myoutputs | Select -last 1 #doesn't work

Second, if you don't know exactly what commands return output, you can Out-Null them all like this:

function MyOutputs {
    . { 
        $list = new-Object Collections.ArrayList
        $list.Add('first')  # returns index
        $list.Add('second') # returns index
    } | Out-Null
    write-Host Returning...
    $list
}
$a = MyOutputs
Write-Host Result is
$a

Just try to put the ArrayList code outside the scriptblock and you will see what it does. Running it inside scriptblock with . notation means, that the scriptblock is executed in current scope. Out-Null just eats the output from Add methods.

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