Search code examples
powershellvspherepowercli

Powershell script to shut down VMS - erros


First post but regular visitor. I'm attempting to cobble together a powershell script to kick off shutdowns on a list of vSphere machines in a .csv. vSphere is at version 5.1u2 and vcenter runs on 2008 R2. I'm loading the powercli stuff at the start of the ps script.

I found a script that seems to fit my needs exactly but it looks like it has a few problems. Unsure whether I can post links to the original so here goes.

Here's my attempt to get it working. Powershell is used so it can be kicked off by monitoring tools when a certain state is detected.

In this state it gives an assigment expression error at line 12 character 44. Still, there seems to be much more than that wrong with the script and trying different things has given various error including "The term import-csv is not recognised as a cmdlet".

I'm grateful for any pointers - Sorry about the dodgy code insertion!

# Adds the base cmdlets needed to use powershell directly. Note the location of the powercli environment ps1 script - this may change in future releases of vsphere
Add-PSSnapin VMware.VimAutomation.Core
Add-PSSnapin VMware.VumAutomation
. "C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1"
#The next line is only relevant and needs running once if you get an invalid ssl related message
#Set-PowerCLIConfiguration -InvalidCertificateAction Ignore
connect-viserver vc5
#Get the names of VMs from a .csv then shut down gracefully - assumes we have vmware to be effective on all
Import-Csv C:\CLIout\listofcriticalvms.csv |  
    foreach {  
        $strNewVMName = $_.name             #Generate a view for each vm to determine power state  
        $vm = Get-View -ViewType VirtualMachine -Filter @{"Name" = $strNewVMName}  
        #If vm is powered on then VMware Tools status is checked  
               if ($vm.Runtime.PowerState -ne "PoweredOff") {  
                   if ($vm.config.Tools.ToolsVersion -ne 0) {  
                   Write-Host "VMware tools installed. Graceful OS shutdown ++++++++ $strNewVMName ----"  
                   Shutdown-VMGuest $strNewVMName -Confirm:$false 
    
 #For generating email  
                           $Report += $strNewVMName + " --- VMware tools installed. Graceful OS shutdown `r`n"  
               }  
               else {  
                      Write-Host "VMware tools not installed. Force VM shutdown ++++++++ $strNewVMName ----"  
                      Stop-VM $strNewVMName -Confirm:$false 
                   $Report += $strNewVMName + " --- VMware tools not installed. Force VM shutdown `r`n"  
               }  
           }  
}  
$Report | export-csv "C:\CLIout\GraceorHardfromcriticaloutput.csv"    

Solution

  • Did you try deleting that line and typing it in manually? Because as far as I can see everything is fine with line 12 (here's code from VMware official help for this command:

    $vm = Get-View -ViewType VirtualMachine -Filter @{"Name" = "VM"}
    

    so maybe there's some copy\paste issues? Also, did you try debugging it in ISE? What error do you get?