Thursday, July 17, 2014

The Winner is ... Wait-Object

I decided I like the for where is the object i was waiting for was returned, I would pass the InputObject on down the pipeline but if not Write-Error and return null. This allows me to 1. let the command run and do a null check on the return value to decide if the script should continue

Example 1.
$returned = $false | Wait-Object -ScriptBlock {Test-Path C:\Wait\For\Removal } -TimeOut 10

if($returned -ne $null) {
  #do something
else {
  #do something else

Also this lets me use a try / catch  with the ErrorAction set to stop causing the the catch to trigger in event of failure.

Example 2.
try {
  $returned = $false | Wait-Object -ScriptBlock {Test-Path C:\Wait\For\Removal } -TimeOut 10 -ErrorAction Stop
catch {

Here is the Code: