Scripts/Wait-BPAAgent.ps1
function Wait-BPAAgent { <# .SYNOPSIS Waits for AutoMate BPA agent to go online or offline. .DESCRIPTION Wait-BPAAgent waits for an agent to online or offline. .PARAMETER InputObject The agents to wait for. .PARAMETER Online Specify whether to wait for the agent to go online or offline (-Online:$false) .PARAMETER Timeout Seconds to wait for the agent status to change before timing out. .INPUTS Agents can be supplied on the pipeline to this function. .EXAMPLE # Wait for all offline agents to come online Get-BPAAgent | Where-Object {-not $_.Online} | Wait-BPAAgent # Wait for agent 'agent01' to go offline Get-BPAAgent -Name agent01 | Wait-BPAAgent -Online:$false .NOTES Author(s): : David Seibel Contributor(s) : Date Created : 07/06/2017 Date Modified : 05/01/2018 .LINK https://github.com/davidseibel/PoshBPA #> [CmdletBinding(DefaultParameterSetName = "All")] [OutputType([System.Object[]])] param( [Parameter(ValueFromPipeline = $true, ParameterSetName = "ByPipeline")] [ValidateNotNullOrEmpty()] $InputObject, [ValidateNotNullOrEmpty()] [switch]$Online = $true, [ValidateNotNullOrEmpty()] $Timeout ) BEGIN { if ($Timeout) { $startTime = Get-Date } $status = "online" if (-not $Online) { $status = "offline" } } PROCESS { foreach ($obj in $InputObject) { switch ($obj.TypeName) { "Agent" { $complete = $false while (-not $complete) { $temp = Get-BPAAgent -Name $obj.Name -BPAServer $obj.BPAServer if ($temp.Online -ne $Online) { Write-Verbose "Waiting for $($temp.TypeName) '$($temp.Name)' to go $status..." if ($Timeout) { $now = Get-Date if (($now - $startTime).Seconds -gt $Timeout) { $complete = $true Write-Error -Message "Timed out after $Timeout seconds waiting for $($temp.TypeName) '$($temp.Name)' to go $status!" -TargetObject $temp } } Start-Sleep 1 } else { $complete = $true Write-Verbose "Complete!" } } $temp } default { Write-Error -Message "Unsupported input type '$($obj.TypeName)' encountered!" -TargetObject $obj } } } } } |