title: Powershell warning and error handling
body: <p>The following code I am writing to replace a batch script which I have below.</p>
  
  <pre class="lang-bsh prettyprint-override"><code>$Script:srcpath = ((Get-Location).Path)
  $Script:configure = "$Script:srcpath\qtbase\configure.bat"
  
  if (Get-Item "$Script:srcpath\qtbase\configure.bat" -WarningAction (Write-Warning "$Script:configure not found. Did you forget to run 'init-repository'?")) {
      continue
  }
  </code></pre>
  
  <p>I am try to rewrite the qt configure batch script:</p>
  
  <pre class="lang-batch prettyprint-override"><code>set "srcpath=%~dp0"
  set "configure=%srcpath%qtbase\configure.bat"
  if not exist "%configure%" (
      echo %configure% not found. Did you forget to run "init-repository"? &gt;&amp;2
      exit /b 1
  )
  
  if not exist qtbase mkdir qtbase || exit /b 1
  
  echo + cd qtbase
  cd qtbase || exit /b 1
  
  echo + %configure% -top-level %*
  call %configure% -top-level %*
  set err=%errorlevel%
  
  cd ..
  
  exit /b %err%
  </code></pre>
  
  <p>The error I am getting in PowerShell is the following:</p>
  
  <pre>
  Get-Item : Cannot bind parameter 'WarningAction' to the target. Exception setting
  "WarningAction": "Object reference not set to an instance of an object."
  At line:4 char:67
  + ... rningAction (Write-Warning "$Script:configure not found. Did you forg ...
  +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : WriteError: (:) [Get-Item], ParameterBindingException
      + FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.PowerShell.Commands.GetItemCommand
  </pre>
  
  <p>The problem is the error being thrown is erroneous because the warning am calling is what should take its place to tell people that the item doesn't exist. so run "init-repository".</p>
  
  <p>There isn't a good, "if not exist" in PowerShell.</p>
  
  <p>Okay, there is, but it looks like this:</p>
  
  <pre><code>catch [System.Management.Automation.ItemNotFoundException]
  </code></pre>
  
  <p>which I am having problems with getting to work.</p>
  
  <p>Why am I  doing this before someone asks is because I feel that Microsoft will phase out CMD some time it's good to have updated scripts.</p>