Ability to wait for Powershell script to complete before launching remote session
0 vote
In the powershell event setup, there is a timeout option and a Wait for Exit option, however neither of these will wait until script completion which means if we are starting an AWS instance remotely, it wont wait until the instance is online to attempt connection (timeout value must be between 10 and 100). We need the ability to wait until a script is complete to ensure a machine is up before attempting conneciton.
Hello,
The "wait for exit" option should be what you need in this case. You do need to build your script accordingly, and put the check you require in the script (in a While loop for example).
If this doesn't work despite this, are you able to share your script?
Regards,
Hubert Mireault
Here is the script I am using. When the connection is called, the system opens a powershell window and starts the script, but it also seems to start the timeout counter and when that expires, regardless of the script status, it will launch the connection in RDM.
#script to start instance
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Unrestricted
#create AWS connection
#Import-Module AWSPowerShell
aws-azure-login --profile default
Set-AWSCredential -ProfileName default
Set-DefaultAWSRegion -Region <redacted>
#declare desired instance to start:
$targetInstance = Get-EC2Instance -InstanceId '<redacted>'
#create function to handle start
function Wait-EC2InstanceState {
[OutputType('void')]
[CmdletBinding()]
param
(
[Parameter(Mandatory,ValueFromPipeline)]
[ValidateNotNullOrEmpty()]
[Amazon.EC2.Model.Reservation]$Instance,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[ValidateSet('running','stopped')]
[string]$DesiredState,
[Parameter()]
[ValidateNotNullOrEmpty()]
[int]$RetryInterval = 60
)
$instanceId = $Instance.Instances.InstanceId
Start-EC2Instance -InstanceId $instanceId
while ((Get-EC2INstanceStatus -IncludeAllInstance $true -InstanceId $instanceId).InstanceState.Name.Value -ne $DesiredState) {
Write-Host "Waiting for server to reach [$($DesiredState)]..."
Start-Sleep -Seconds $RetryInterval
}
}
$targetInstance | Wait-EC2InstanceState -DesiredState running -Verbose
#Sleep for set period to allow server to reach ready state
Start-Sleep -Seconds 180
Thank you for the script. I think I understand what's happening. There seems to be a 60 second timeout that isn't customizable, even when using the "wait for exit" feature. It's not linked to the actual setting, so we will make two changes:
I think with these changes it will work for your scenario. Let me know if that seems right.
Regards,
Hubert Mireault
Yes, I believe those changes would fix the issue.
Thanks!
Roy
Thanks Roy, we have made these changes internally and they will be available in the next RDM release.
Regards,
Hubert Mireault