gpt4 book ai didi

powershell - 如何从模块函数中获取退出代码值?

转载 作者:行者123 更新时间:2023-12-03 00:14:01 24 4
gpt4 key购买 nike

我已将一个模块放入我的用户配置文件目录。该组织要求我的主目录位于网络共享上。

16:06:09.74 \\COMPNAME\SHNAME\Users\lit  H:\My Documents\WindowsPowerShell\Modules\MyUtils
H:>type MyUtils.psm1
function MyFunc()
{
[cmdletbinding()]
Param()

Write-Host "now from myfunc"
return 8
}

Export-ModuleMember -Function MyFunc

当我运行 MyFunc它似乎按预期运行。但是, $LASTEXITCODE总是 Null .
PS C:\src\powershell> MyFunc
now from myfunc
8
PS C:\src\powershell> if (!$LASTEXITCODE) { Write-Host "LASTEXITCODE is null"}
LASTEXITCODE is null

如果我使用 exit 8 ,然后我的交互式主机立即退出。同样的事情使用 $Host.SetShouldExit .

最佳答案

您可以尝试简单地将值分配给变量,例如:

[Int32] $myRc = myfunc;
$myRc;

'8' 出现在控制台上的事实表明 PowerShell 正在使用 Out-Default,因为您没有告诉它如何处理该值。

当然,如果你想以 PowerShell 方式做事,你应该在函数中使用 Write-Output,如果该函数将用作 cmdlet,而不是“帮助”函数。

祝你好运!

关于powershell - 如何从模块函数中获取退出代码值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41412086/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com