Quantcast
Channel: VMware Communities : All Content - All Communities
Viewing all articles
Browse latest Browse all 175326

foreach loop and GET-VM help needed .

$
0
0

Folks  , I ran into a  problem I am trying to  integrate a simple guest Restart on a set of VMs from a CSV file.

The idea is very simple , restart the vm and check that the vm has been power back ON  in good state.

 

I could not find what could be duplicating the tasks:

 

Here it is the CSV file with only 2 vms  

...

...

...

 

$vms = Import-CSV D:\...OS_restart.csv

 

If I run  the follow:

foreach ($vm in $vms) {

    $vmn = $vm.vmname

  

    #Write-Host "`n <> Phase: Restarting OS <>`n"

    #Restart-VMGuest -VM (Get-VM -name $VMN) -Confirm:$false

    #Write-Host "`n <> Validation Completed , Validating $vmn status <> `n"

   

    Get-VM -name $vmn | Select-Object  Name,PowerState

     

    }

 

I got  a false positive results only on the 1st  for cycle, it is reporting the Power State is OFF which is not true

Once the script gets to to  perform the 2nd loop  it tells the correct  PowerOn.

 

Name       : VM1

PowerState : PoweredOff

 

Name       : VM1

PowerState : PoweredOn

 

Name       : VM2

PowerState : PoweredOff

 

Name       : VM2

PowerState : PoweredOn

 

I thought ,  okay maybe the foreach loop is considering the length based on the numbers of Vms , so I  left just 1 vm .

I got the same result:

 

Name       : VM1

PowerState : PoweredOff

Name       : VM1

PowerState : PoweredOn

 

 

Also if I tried to put this into functions I got the same results :

 

 

function validate_os {

         param(

       [string]$VMName

          )

 

    write-host "--- : $VMName : ---" -ForegroundColor Cyan

         # I've tried all of those   lines, they worked but the same result , the first cycle  I got a power off status.

# The problem of using the Get-Unique it is I can't use the VM name because it will say the VM is poweroff when calling the restart VM

 

    Get-VM -name $VMName | Select-Object  Name,PowerState | Get-Unique

    #Get-VM -name $VMName | Select @{N = 'VMName'; E = {$_}; } , @{ Name='PowerState'; E ={$_.PowerState}}

    #Get-VM -name $VMName

    #Works also okay # Get-VM -Name $VMName | select {$_.Guest}, {$_.PowerState}  | Get-Unique

   }      

 

function restart_os {

         param(

       [string]$VMName

          )

 

    Get-VM $VMName  | Restart-VMGuest -Confirm:$false

      write-host "--- OS Restart on: $VMName  Completed ---" -ForegroundColor Cyan

}

 

 

foreach ($vm in $vms) {

    $vmn = $vm.vmname

    Write-Host "`n <> Phase: Restarting OS <>`n"

    #restart_os -VMName $vmn

    Write-Host "`n <> Validation Completed , Validating $vmn status <> `n"

    validate_os -VMName $vmn

 

 

This is what I got , however right after I got the error on console I can see the Restart instruction has been passed to the vm.

 

Restart-VMGuest : 3/27/2019 4:44:59 PM    Restart-VMGuest        Operation "Restart VM guest" failed for VM "VM2" for the following reason: The attempted operation cannot be performed

in the current state (Powered off).   

At D:\..._OS_restart.ps1:90 char:24

+      Get-VM $VMName  | Restart-VMGuest -Confirm:$false

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

    + CategoryInfo          : NotSpecified: (:) [Restart-VMGuest], VimException

    + FullyQualifiedErrorId : Client20_VmGuestServiceImpl_RestartVmGuest_ViError,VMware.VimAutomation.ViCore.Cmdlets.Commands.RestartVmGuest

 

 

Any suggestion would be appreciated .

Tools: Running, version:10305 (Current)

 

Also tried some other things and I got the same problem

 

     Restart-VMGuest -vm $VMName -Confirm:$false

     start-sleep -s 3

     wait-tools -vm $VMName

 

Thanks.


Viewing all articles
Browse latest Browse all 175326

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>