我正在使用WMI远程卸载软件,并且可以正常工作。我正在努力的是使用该结果(成功与否(来给出一个简单的输出消息,而不是命令的正常输出。 我通常为此使用 $lastexitcode,但无论命令是否成功,它都会运行到我成功的卸载消息。 这是我尝试使用的:
$app = Get-WmiObject Win32_Product -ComputerName "$computer" | where { $_.vendor -eq "APN, LLC" }
$app.Uninstall()
if ($lastexitcode -eq 0)
{
write-host -ForegroundColor Green "Programm Successfully Removed"
}
else
{
write-host -ForegroundColor red "There was a problem uninstalling the program"
}
当我将操作的输出留在它上时,它返回:
__GENUS : 2
__CLASS : __PARAMETERS
__SUPERCLASS :
__DYNASTY : __PARAMETERS
__RELPATH :
__PROPERTY_COUNT : 1
__DERIVATION : {}
__SERVER :
__NAMESPACE :
__PATH :
ReturnValue : 0
PSComputerName :
我想我可以用返回值做点什么,但我不确定如何做。 任何帮助将不胜感激。
编辑:由于布鲁斯的回答,解决方案:
$app = Get-WmiObject Win32_Product -ComputerName "$computer" | where { $_.vendor -eq "APN, LLC" }
$appuninstall = $app.Uninstall()
if ($appuninstall.returnvalue -eq 0)
{
write-host -ForegroundColor Green "Programm Successfully Removed"
}
else
{
write-host -ForegroundColor red "There was a problem uninstalling the program"
}
$LastExitCode
仅在运行本机命令(外部 .exe(时设置。在代码中,您希望捕获变量中对Uninstall()
的调用的结果,然后在if
语句中使用该对象的返回代码属性。