Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 如何在powershell脚本中捕获cmdlet的退出代码_Windows_Powershell_Scripting_Powershell 3.0 - Fatal编程技术网

Windows 如何在powershell脚本中捕获cmdlet的退出代码

Windows 如何在powershell脚本中捕获cmdlet的退出代码,windows,powershell,scripting,powershell-3.0,Windows,Powershell,Scripting,Powershell 3.0,我对powershell脚本编写非常陌生,我花了很长时间试图捕捉某个东西是失败了还是成功了。我有一个简单的例子: test1.ps1 get-psdrive -name ds | out-null if($? -ne "False") { echo "drive doesn't exist" } else { echo "Found drive" } 然而,这对我不起作用。我还尝试了变量$LastExitCode,但这也不起作用。我严重误解了一些事情。有人能给我指出正确的方向或给我看

我对powershell脚本编写非常陌生,我花了很长时间试图捕捉某个东西是失败了还是成功了。我有一个简单的例子:

test1.ps1

get-psdrive -name ds | out-null

if($? -ne "False")
{ 
   echo "drive doesn't exist"
}
else { echo "Found drive" }

然而,这对我不起作用。我还尝试了变量$LastExitCode,但这也不起作用。我严重误解了一些事情。有人能给我指出正确的方向或给我看一个工作示例吗

$drive = Get-PSDrive -Name ds 2>Out-Null

如果cmdlet成功,
$drive
将保存驱动器对象,否则其值为
$null

if ($drive -eq $null) {
  echo "Drive doesn't exist."
} else {
  echo "Found drive."
}

-EA SilentlyContinue在大多数情况下都不起作用,通常您必须在单独的一行上执行$ErrorAction=“SilentlyContinue”,或者如上所述执行>Out Null
if ($drive -eq $null) {
  echo "Drive doesn't exist."
} else {
  echo "Found drive."
}