VMware Cloud Community
DJ77
Contributor
Contributor
Jump to solution

How to ensure that OS has been started after Start-VM

Guest - Windows OS

I am going following tasks in a script.

1. Shutdown VM

2. Revert Snapshot

3. Start VM

After start VM command I have to perform certain operations in Gust OS. But Start VM commands exits when Guest OS startup has started. How can I determine that Windows OS has been started successfully and I can start operation. Is there any specific CMDlet available to check the same?

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Sure, the following script does something like that

$vmName = <VM-name>
$esxCred = Get-Credential -Credential "ESX-account"
$vmCred = Get-Credential -Credential "guest-account"
$myScript = @"
net start
"@

$vm = Get-VM $vmName
$vm | Start-VM -Confirm:$false | Out-Null
# Wait till the VM is powered on
while((Get-VM $vmName).PowerState -ne "PoweredOn"){sleep 5}
# Wait till service is running
$msg = $vm | Invoke-VMScript -HostCredential $esxCred -GuestCredential $vmCred -ScriptType "bat" `
			-ScriptText $myScript -ErrorAction SilentlyContinue

The variable $msg will contain all the running services on the guest.

You can check if the specific service you want to be running is in the list otherwise you loop through these last lines until the service is there.

Something like this

....
do{
   $msg = $vm | Invoke-VMScript -HostCredential $esxCred -GuestCredential $vmCred -ScriptType "bat" `
			-ScriptText $myScript -ErrorAction SilentlyContinue
   sleep 5
} until($msg.Split("`n") | where{$_ -like "*Workstation*"})

The "until" condition will result in $null as long as the text "Workstation" is not present.

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

Afaik you have to check with the OS of the guest directly.

You can check for example if a specific service in the guest OS is started.

For that you could use the Invoke-VMScript cmdlet and run the "net start" command from the BAT file you pass via the cmdlet.

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
DJ77
Contributor
Contributor
Jump to solution

Thanks LucD for quick response.

But this Invoke Script will fail if OS startup is still going on.

Do I need to write a loop to check this action is successful? If yes, how can I do that. Some sample code will be available. I am newbie at Windows.

I have STAF installed on guest OS and I can check whether STAF signal is successful or not.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Sure, the following script does something like that

$vmName = <VM-name>
$esxCred = Get-Credential -Credential "ESX-account"
$vmCred = Get-Credential -Credential "guest-account"
$myScript = @"
net start
"@

$vm = Get-VM $vmName
$vm | Start-VM -Confirm:$false | Out-Null
# Wait till the VM is powered on
while((Get-VM $vmName).PowerState -ne "PoweredOn"){sleep 5}
# Wait till service is running
$msg = $vm | Invoke-VMScript -HostCredential $esxCred -GuestCredential $vmCred -ScriptType "bat" `
			-ScriptText $myScript -ErrorAction SilentlyContinue

The variable $msg will contain all the running services on the guest.

You can check if the specific service you want to be running is in the list otherwise you loop through these last lines until the service is there.

Something like this

....
do{
   $msg = $vm | Invoke-VMScript -HostCredential $esxCred -GuestCredential $vmCred -ScriptType "bat" `
			-ScriptText $myScript -ErrorAction SilentlyContinue
   sleep 5
} until($msg.Split("`n") | where{$_ -like "*Workstation*"})

The "until" condition will result in $null as long as the text "Workstation" is not present.

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
DJ77
Contributor
Contributor
Jump to solution

Awesome. Thanks a lot LucD. I can make forward progress with this approach

0 Kudos