2

I'm extremely new to powershell scripting and I'm having a hell of a time trying to capture whether something simply failed or succeeded. I have a simple example:

test1.ps1

get-psdrive -name ds | out-null

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

This however isn't working for me. I also tried the variable $LastExitCode but that doesn't work either. I'm seriously misunderstanding something here. Can someone please point me in the right direction or show me a working example

4

1 回答 1

2

尝试这样的事情:

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

或者

$drive = Get-PSDrive -Name ds -EA SilentlyContinue

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

if ($drive -eq $null) {
  echo "Drive doesn't exist."
} else {
  echo "Found drive."
}
于 2013-06-12T18:36:56.533 回答