VMware Cloud Community
AnkurS11
Contributor
Contributor
Jump to solution

Second and third function calls before second and first gets over

Hi All,

I have created a Script to take snapshot of a VM and then VMtools and last VM Hardware upgrade.

The problem i am seeing here is its updating the VH Hardware Version but it kicks start before taking the snapshot and before updating the tools. It throws the error mentioned in the output section below for snapshot and also in VMtools update. I could see snapshot is taken but not sure about Vmtools.

Initially script was getting completed but now its getting stuck. attached is the screenshot as well.

Script :-

Function Make-Snapshot($vm)

{

  $snapshot =New-Snapshot -VM $vm -Name "BeforeUpgradingVMtoolsHW" -Description  "Snapshot taken before Tools and Hardware was updated" -Confirm

}

Function Delete-Snap()

{

  Get-VM |Get-Snapshot -Name "BeforeUpgradeVMware" |Remove-Snapshot -Confirm

  # $snapshot = Get-Snapshot -Name "BeforeUpgradeVMware" -VM $vm

  # Remove-Snapshot -Snapshot $snapshot -Confirm:$false

}

Function VMHWUpgrade($vm)

{

  Write-Host "$VM Shutting down" -ForegroundColor Red

  $PowerOffVM = Shutdown-VMGuest -VM $VM -Confirm

  do

{

Start-Sleep -s 1 | out-null

$i += 1

}

until($vm.PowerState -eq "Poweredoff")

            Write-Host "Starting upgrade hardware level on $vm."

            Set-VM -VM $VM -Version v10 -Confirm

            Start-VM -VM $VM

            Write-Host $vm "is up to date" -ForegroundColor Green

}

Function VMToolsUpgrade ($vm)

{

  Make-Snapshot $vm

  write-host "Updating VMware tools for VM $VM"

  get-vm $vm | Update-Tools -noreboot | write-host "VMware Tools Update Completed Successfully." -ForegroundColor Green

}

$VC = read-host "Please enter vCenter Name"

connect-viserver $VC

$VMlist = Get-content C:\temp\Script\HW-VMList.txt

foreach ($vm in $vmlist)

{

    Write-Host "Test $vm"

    VMToolsUpgrade $vm

    VMHWUpgrade $vm

    #Delete-Snap $vm

}

Output :-

C:\> P:\Ankur\Ankur Scripts\VMtoolsHWSnap-Upgrade.ps1

Please enter vCenter Name: VCNAME

Name                           Port  User                         

----                           ----  ----                         

VCName                   443   UserName              

HelperVM2016

New-Snapshot : 10/5/2018 9:25:28 AM    New-Snapshot        Operation is not valid due

to the current state of the object.   

At P:\Ankur\Ankur Scripts\VMtoolsHWSnap-Upgrade.ps1:3 char:14

+   $snapshot =New-Snapshot -VM $vm -Name "BeforeUpgradingVMtoolsHW" -Description  ...

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [New-Snapshot], VimException

    + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCor

   e.Cmdlets.Commands.NewSnapshot

Updating VMware tools for VM HelperVM2016

Update-Tools : 10/5/2018 9:25:30 AM    Update-Tools        Operation is not valid due

to the current state of the object.   

At P:\Ankur\Ankur Scripts\VMtoolsHWSnap-Upgrade.ps1:34 char:16

+   get-vm $vm | Update-Tools -noreboot | write-host "VMware Tools Update Complete ...

+                ~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [Update-Tools], VimException

    + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCor

   e.Cmdlets.Commands.UpdateVmTools

HelperVM2016 Shutting down

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Yes, rather (this version is the 1st one that is modules only, but it still requires a MSI file to install).

Subsequent versions (starting with 6.5.1, which is not the same as 6.5 R1) are installed from PSGallery (with Install-Module).

It looks like the v13 option was introduced in 6.5.1, see also Re: PowerCLI - upgrade VM hardware from v10 to v13 failing

Btw, the current version is 10.2.0, and I would advise to go for the latest one.


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

View solution in original post

0 Kudos
14 Replies
LucD
Leadership
Leadership
Jump to solution

From the error message it looks as if the New-Snapshot gets an exception, and then the Update-Tools is started.

You should try to find out why New-Snapshot gets that exception.
Can you run just New-Snapshot against that VM?

And you should perhaps build in some error checking.
If the snapshot doesn't complete successfully, abort.
You could do that with a Try-Catch construct.


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

0 Kudos
AnkurS11
Contributor
Contributor
Jump to solution

Thanks a lot LucD for replying.

I still learning powershell so not sure how can i use try-catch. but whats happening is that its taking snapshot i can see that but it seems that before the snapshot function gets finish, its calling tools update function and even before that get finished its power of the VM and start VMhardware upgrade.

So is there a way i can put a stop to other function calling before the previous gets over.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

If you do not use the RunAsync switch on the New-Snapshot cmdlet, it normally waits till the snapshot is complete, before continuing.
The message I see for the snapshot mentions an exception, and the snapshot progress bar is stuck at 0% it seems.
So I still think something goes wrong with the snapshot.

Did you try a New-Snapshot for the VM from the PS prompt?

Does that work?
Alternatively you could try to create a snapshot from the Web Client.
Just to make sure that taking a snapshot for that VM works.


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

0 Kudos
AnkurS11
Contributor
Contributor
Jump to solution

Earlier it was working fine. I also tested snapshot from webclient and PS as well with both able to take snapshot without any issue.

What you suggest about the VMtools update function its giving the same error there as well " Operation is not valid due to the current state of the object."

0 Kudos
LucD
Leadership
Leadership
Jump to solution

But can you take a snapshot now?

Does the VM perhaps need consolidation?
Do you have another VM you could test with?


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

0 Kudos
AnkurS11
Contributor
Contributor
Jump to solution

i dont have another vm at this point. Will create one and share results of it.

0 Kudos
AnkurS11
Contributor
Contributor
Jump to solution

Here is the correction i made in the Functions. it worked pretty fine for VM harware version 10 but it throws an error for v13 " The Operation is not supported on the object".

Function Make-Snapshot($vm)

{

# this will take the snapshot of the VM

    $snapshot = New-Snapshot -VM $vm -Name "Before Upgrading VMtoolsHW" `

     -Description "Snapshot taken before Tools and Hardware was updated" `

     -Confirm:$false -ErrorAction SilentlyContinue -RunAsync

# It will wait till the snapshot gets completed.

     if($snapshot -ne ""){

         Wait-Task -Task $snapshot | Out-Null

         $task = Get-task -id $snapshot.Id

         if($task -ne "" -and $task[0].State -eq "Success")

         {

            Write-Host "Snapshot created successfully for VM $vm" -ForegroundColor Green;

         }

         else

         {

            Write-host "Snapshot creation failed for VM $vm";

         }

     }

     else{

        Write-host "Unable to take snapshot for vm $vm";

     }

Function VMHWUpgrade($vm)

{

#This will ask for confirmation for shutting down the VM and it will upgrade the HW version to 13.

  Write-Host "$VM Shutting down" -ForegroundColor Red

  $PowerOffVM = Shutdown-VMGuest -VM $VM -Confirm

        do

{

Start-Sleep -s 1 | out-null

$VMstatus = Get-VM $vm

}

until($VMstatus.PowerState -eq "Poweredoff")

            Write-Host "Starting upgrade hardware level on $vm." -ForegroundColor Yellow

            Set-VM -VM $VM -Version v13 -Confirm | Out-Null

            $shVersion =Start-VM -VM $VM

            Write-host = "Current Hardware Version of $vm" $shVersion.Version

            Write-Host $vm "is up to date" -ForegroundColor Green

 

}

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Do you still have ESXi nodes that are not running 6.5?
If a VM is running on an ESXi node that is pre-6.5, the HW upgrade to v13 will fail.


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

0 Kudos
AnkurS11
Contributor
Contributor
Jump to solution

Yes we have esxi running on 6.5. when we do update from v8 to v13 manually it works but from script it fails. Also tried to upgrade from v8 to v10 via script and it works but failed for v13.

Please let me know if i am missing anything in the script.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Which PowerCLI version are you using?

Older PowerCLI versions do not support the v13 value yet.


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

0 Kudos
AnkurS11
Contributor
Contributor
Jump to solution

I am having powercli 6.5 Release 1 Build 4624819. is this old ?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Yes, rather (this version is the 1st one that is modules only, but it still requires a MSI file to install).

Subsequent versions (starting with 6.5.1, which is not the same as 6.5 R1) are installed from PSGallery (with Install-Module).

It looks like the v13 option was introduced in 6.5.1, see also Re: PowerCLI - upgrade VM hardware from v10 to v13 failing

Btw, the current version is 10.2.0, and I would advise to go for the latest one.


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

0 Kudos
AnkurS11
Contributor
Contributor
Jump to solution

Thanks a lot LucD after upgrading PowerCLI to latest (10.1) it worked. So once again thanks very much for help and support.pastedImage_1.png

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You're welcome.
Btw, just this morning, PowerCLI 11.0 was released


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

0 Kudos