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:

Monday, June 23, 2014

New Utility Function Wait-ValueReturned

I Just need a way to block a scripts execution while I wait on some command to return the correct value. So I created this little function to allow me to pass the command or commands and what i expect back and let it handle the work instead of littering my scripts main body with a bunch of loops, if / else, and sleeps, I can use this to encapsulate all that into a clean function.

Wednesday, June 11, 2014

PowerShell Objects - Import-Module Pt 3

The -AsCustomObject switch on the New-Module Cmdlet is also available in the Import-Module which opens up a whole new world of using PowerShell objects and modules. Imagine you need to import 2 modules to work with Hyper-V and VMWare in your script but after importing them in that order you notice that Get-VM for VMWare is visiable and the Get-VM command from the Hyper-v module is hidden. The are a couple of way to handle modules with command that are the same name. One would be to use the get-command command to invoke the correct command from the correct module. My perfered way is to use the -AsCustomObject switch when importing the modules. This way I hsave the modules and there commands neatly reference in variable so that it is easy to tell which module the command i am executng is going to run against. Example (sudo-code module names may net be correct)

.