Posts tagged PowerCLI

Unmount CD-Drives using PowerCLI

0

In some cases it would be good to know that your VMs don´t have an ISO-File or Host device configured e.g. if you want to vMotion some VMs to another Host. Here´s how you could solve this problem:

 
[vSphere PowerCLI] C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> Get-VM | Get-CDDrive | 
Set-CDDrive -NoMedia -Connected:$false -StartConnected:$false -Confirm:$false

PowerCLI 5.1 Poster

0

Some of you guys are knowing there are posters for reference for the VMware products.

I´ve attached a link to the updated PowerCLI 5.1 reference poster for you.

http://blogs.vmware.com/vipowershell/files/2012/09/PowerCLI_5_1_Poster.pdf

Shutdown and Recovery script in PowerShell

0

A few months ago I needed a script to shutdown my VMs and the underlying hosts in case of a power outage in one of our datacenters.

For this reason I´ve created some scripts that are shutting down our VMs or in case of recovery abort this process.

Because of security reasons I´ve also implemented the VICredentialStoreItem. So I didn´t had to put the username and password in clear-text into the scripts.

If you´re more interested in how to implement this Feature you can visit one of the following links:

http://www.vmware.com/support/developer/windowstoolkit/wintk40u1/html/New-VICredentialStoreItem.html

http://www.vmware.com/support/developer/windowstoolkit/wintk40u1/html/Get-VICredentialStoreItem.html

 

Shutdown script:

# Declaring the variables
$enddate = (Get-Date).tostring("yyyyMMdd")
$filename = 'C:\ShutDown' + $enddate + '.log'
$creds = Get-VICredentialStoreItem -file "C:\credentials.xml"
$scriptwaittime = 3600 #Seconds
$shutdownwaittime = 240 #Seconds
$Cluster = '"<Clustername>"'
$ESXSRV1 = Get-VMhost <ESXHost1>
$ESXSRV2 = Get-VMhost <ESXHost2>

# Main script
Start-Transcript $filename
Write-Host "Waiting for about $scriptwaittime seconds in case the power is back" -ForegroundColor Green Write-Host "and the VMs and ESX Host do not have to shut down" -ForegroundColor Green
Start-Sleep $scriptwaittime

# Importing the VMware PowerCLI Snapin into Powershell
if (((Get-PSSnapin -Name "VMware.VimAutomation.Core" -ErrorAction SilentlyContinue) -eq $null ) -and ((Get-PSSnapin –registered -Name "VMware.VimAutomation.Core") -ne $null)) {     Write-Host "Adding VMware vSphere PowerCLI" -ForegroundColor Green     Add-PSSnapin -name VMware.VimAutomation.Core     ."C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-VIToolkitEnvironment.ps1" }

# Connecting to vCenter Server
Connect-VIServer -Server $creds.Host -user $creds.User -password $creds.Password

# Set DRS to manual
Set-Cluster -Cluster $Cluster -DrsMode manual -Confirm:$false

# For each of the VMs on esxsrv1
# - Shutdown the guest cleanly
Foreach ($VM in ($ESXSRV1 | Get-VM | Where-Object {$_.Folder -notmatch “vCenter”})){$VM | Shutdown-VMGuest -Confirm:$false}

# For each of the VMs on esxsrv2
# - Shutdown the guest cleanly
Foreach ($VM in ($ESXSRV2 | Get-VM | Where-Object {$_.Folder -notmatch “vCenter”})){$VM | Shutdown-VMGuest -Confirm:$false}

# Set the amount of time to wait before assuming the remaining powered on guests are stuck
Write-Host "Sleep $shutdownwaittime seconds" -ForegroundColor Green Start-Sleep $Waittime

# Set Hoststate to Maintenance for esxsrv1
Set-VMHost -VMhost $ESXSRV1 -State “Maintenance”

# Shutdown of esxsrv1
$ESXSRV1 | Foreach {Get-View $_.ID} | Foreach {$_.ShutdownHost_Task($TRUE)} Write-Host "Shutdown $ESXSRV1 Complete" -ForegroundColor Green

# Set Hoststate to Maintenance for esxsrv2
Set-VMHost -VMhost $ESXSRV2 -State “Maintenance”

# Shutdown of esxsrv2
$ESXSRV2 | Foreach {Get-View $_.ID} | Foreach {$_.ShutdownHost_Task($TRUE)} Write-Host "Shutdown $ESXSRV2 Complete" -ForegroundColor Green
Stop-Transcript

Recovery script:

$enddate = (Get-Date).tostring("yyyyMMdd") $filename = 'C:\Recovery' + $enddate + '.log'
Start-Transcript $filename
Write-Host "Received recovery signal from ups" -ForegroundColor Green
Write-Host "Going to kill Powershell process" -ForegroundColor Green
Stop-Process -processname powershell*
Write-Host "Powershell process was killed" -ForegroundColor Green
Stop-Transcript
Go to Top