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 {
  exit
}

Here is the Code:

Monday, July 14, 2014

Wait-Object Function Takes Place of Wait-ValueReturned

I previously posted a function Wait-ValueReturned but is did not seem like it flowed with the rest of PowerShell so I re factored the code into Wait-Object but I am not sure about how the command should return. I have considered first return bool from the command to indicate success, which means the command will always return true or false. My other option is to return to object that was being waited on if success and return error record on fail. here is examples of both. Which makes more sense to use in a script

 Returning Bool:

 Returning object or error: