Script will shutdown all vms (Shutdown or Power Off depending on VMWare Tools) then shutdown ESX. The script configure automatic start VM with a defined order for critical VMs
Don't forget to put "profile.ps1" in your directory "c:\windows\system32\windowspowershell"
Can we get an English translation of the #notes in the Shutdown_All.ps1 please.
Thanks.
Here is a translation (sorry for my poor english
)
#—————— Start Script ————
$Name = “*” # Change if needed
$Location = “*” # Change if needed
$WhatIf = $false # "$false" For a "real" execution of this script
$VMPriority1= "MUTBGDC01" # First server to start (5 min delay)
$VMPriority2= "" # Second server to start
$VMPriority3= "" # Third server to start
#Other VMs will be started without order with a default delay (120s)
#Connect to vCenter
Connect-VIServer -Server localhost >$null
Write-Progress “Checking VM Guest Status” “Working…”
#Get started VMs
$vmGuest = Get-VM $Name -Location $Location | ? { $_.PowerState -ne “PoweredOff” } | Get-VMGuest
#Loop for configuration/shutdown of virtual machines
for ( $i = 0; $i -lt $vmGuest.Count; $i++ ) {
Write-Progress “Shutting down VMs” “Virtual machine: $($vmGuest[$i].VmName)” -percentComplete ( $i / $vmGuest.Count * 100)
#Change of startup policy for each VM
$VMpolicy=Get-VMStartPolicy -VM $vmGuest[$i].vmname
#Startup configuration
switch ($vmGuest[$i].vmname) {
$VMPriority1 {Set-VMStartpolicy -startpolicy $VMpolicy -startAction PowerOn -Startorder 1 -StartDelay 300}
$VMPriority2 {Set-VMStartpolicy -startpolicy $VMpolicy -startAction PowerOn -Startorder 2 -InheritStartDelayFromHost}
$VMPriority3 {Set-VMStartpolicy -startpolicy $VMpolicy -startAction PowerOn -Startorder 3 -InheritStartDelayFromHost}
default {Set-VMStartpolicy -startpolicy $VMpolicy -startAction PowerOn -UnspecifiedStartOrder -InheritStartDelayFromHost}
}
#OS Shutdown or Power Off (if no WMWare Tools)
if ( $vmGuest[$i].State -eq “Running” ) {
Shutdown-VMGuest -Guest $vmGuest[$i] -Confirm:$false -WhatIf:$WhatIf
} else {
Stop-VM -VM $vmGuest[$i].VmName -Confirm:$false -RunAsync -WhatIf:$WhatIf
}
}
#Shutdown of ESX
Get-VMHost | Get-VMHostStartPolicy | Set-VMHostStartPolicy -Enabled:$true -StopAction PowerOff
get-vmhost * | shutdown-vmhost -force
#—————- End Script ————
Thank you, your English is better than my French, I'd got half way there from the code itself, just wanted to double check. ![]()
I have implemented the solution with mixed results. When launching the .bat file manually, everything works as designed. However, our need is to automate this for use in a power outage situation. We have a datacenter running on a Liebert UPS. I have installed the Liebert Multilink client on the server where Virtual Center Server is installed and then configured it to launch the .bat file when the UPS is running on battery. The problem is that when the Multilink client initiates the .bat file an authentication prompt pops up.
What OS is the server the Liebert Multilink client installed on?
Is the .bat file located on the server itself or a remote location?
What is the exact message? Is it asking for a user/password or is it 'Are you sure you want to run this file?'
Check to make sure you trust the file and/or location in GP.
Thank you for the prompt response. The OS of the server is Server 2003 R2. I have multiple sites, some 32 bit installs and some 64 bit. The same server hosts Virtual Center Server 4.0 update2. The .bat is in a scripts folder at the root of c: on the same server. The pop up is an actual window with a user name field and a password field. If the file is local and being called locally, I wouldn't think that trust is relevant in this case.
Does the UPS software have the ability to run the .bat as the same user you manually run it as or does it run as system? I'm guessing the point at which it asking for credentials is the connect to vCenter server.
I have attempted to change the Multilink service to run as a different user but then the software loses its communication to the UPS (SNMP). Even after changing it back to the system account, the software still has no communication. I have to reinstall the software to get it working again. The point at which the authentication prompt occurs is just prior to the warning of the problem with the server certificate. This would seem to indicate that the authentication prompt is for the connection to vCenter server and not to Powershell. Assuming this is the case, how would I authenticate to the vCenter server within the Powershell script without compromising security (ie. plain text pw)?
I'm not sure how to pass a user/password to vCenter securly. I know with windows and powershell you can create a pscredential type variable. However i don't think you can pass these to vCenter via this method. Sorry.
