2
我已将模块放入我的用户个人资料目录。该组织要求我的主目录位于网络共享上。如何从模块函数获取退出代码值?
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
。
'$ LASTEXITCODE'用于本地应用程序和'.ps1'脚本,不适用于函数。 – PetSerAl
@PetSerAl - 好的。如果我打开一个PowerShell交互主机并调用一个函数,有没有办法获得退出代码值?我必须调用cmdlet(.ps1)脚本才能获得函数返回值吗? – lit
同样,函数没有退出代码。如果您在讨论流水线输出,那么您需要将其分配给变量或通过流水线传递给下一个命令。 – PetSerAl