Using PowerShell Direct for Script Locking

Here is one of the most helpful code snippets that I have come up with using PowerShell Direct:

function waitForPSDirect([string]$VMName, $cred){
   Write-Output “[$($VMName)]:: Waiting for PowerShell Direct (using $($cred.username))”
   while ((icm -VMName $VMName -Credential $cred {“Test”} -ea SilentlyContinue) -ne “Test”) {Sleep -Seconds 1}}

In essence this function allows you to block your script until the requested virtual machine has booted and is responding to PowerShell Direct.  This is immensely useful when you are provisioning virtual machine and need to know when the guest operating system is actually up and running – and has become a staple function in many of my scripts.