Category Archives: PowerCLI

Stuff related to PowerCLi

Migrate VMs to another ESXi host in another Vcenter

Can across situation where I needed to move VM from one Vcenter to another.

The following script will do the following:
Will prompt for a Source and Target Vcenter and then ask for user credentials.
Connect to the Source and Target Vcenter. When then cycle through VMs listed from csv file (VMList.csv). (Place the csv file in same directory as the script.)

The list of VMs in the One by one and Migrate the VM to new VC. If the VMs powered on will attempt to shutdown the Server (if VMtools installed) and t

The format of the VMList.csv file needs to be in format as below.

#ensure that not connected to any vCenters
Disconnect-VIServer -Server * -Force -Confirm:$false

#enter Vcenter
Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -Scope User -Confirm:$false
$vc1 = Read-Host "Enter Source Vcenter"
$vc2 = Read-Host "Enter Target Vcenter"

Write-Host "VC1 = " $vc1
Write-Host "VC2 = " $vc2

$vmlist = Import-CSV “$pwd\VMList.csv”

# Connect To Vcenter Server
$User = Read-Host -Prompt "Please enter your VC admin username"
$PW = Read-Host -Prompt "Please enter your VC admin password" -asSecureString
$PW = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($PW))

Connect-VIServer $vc1 -User $User -Password $PW -ErrorAction SilentlyContinue
Connect-VIServer $vc2 -User $User -Password $PW -ErrorAction SilentlyContinue

foreach ($item in $vmlist) {
# Map variables
$vm = $item.vmName
$destESXi = $item.targetesxihost
$destDatastore = $item.targetdatastore
$destVDSwitch = $item.targetDVSwtich
$destPortgroup= $item.targetPortGroup
$typeDisk = $item.Disktype

$vm = get-vm $vm
#Check to see if VM powered off if powered on will shutdown and check ever 5 sec til shutdown
if ($vm.powerstate -eq "PowerdOn"){
write-host "Shutting down" $vm
$vm | Shutdown-VMguest -Confirm:$false
do {
#wait 5 secs
start-Sleep -s 5
$vm = get-vm $vm
$status = $vm.powerstate
}until($status -eq "PoweredOff")
}

Write-Host "===== Connected vCenters ====="
$global:DefaultVIServers | ft -AutoSize

#Command to migrate the VM to new Host in new Vcenter
Write-Host "Moving VM" $vm
Move-VM -VM (Get-VM -Server $vc1 -Name $vm) -VMotionPriority High `
-Destination (Get-VMHost -Server $vc2 -Name $destESXi) `
-Datastore (Get-Datastore -Server $vc2 -Name $destDatastore) -DiskStorageFormat $typeDisk `
-PortGroup (Get-VDPortgroup -Server $vc2 -VDSwitch $destVDSwitch -Name $destPortgroup)

} #end of ForLoop

Disconnect-VIServer -Server * -Force -Confirm:$false

Rolling Reboot of VMware ESXi Cluster

Ran into situation where I needed to reboot a full cluster of ESXi hosts. In most cases when I need to reboot Cluster full of hosts I would utilize VUM (VMware Update Manager) to use VMware DRS to move VMs off the host, place the host in maintenance mode, reboot host and when host completes the reboot take server back out of maintenance mode, then move to next host and does for each host in cluster.

I did not need to patch the hosts this time. And Since the cluster had 32 hosts and several VMs I did not want to do this by hand. So used google and was found this script that I wanted to share. I wish I could give credit to the creator but the was on in an archived word press blog.

The script does the following:
Goes through the cluster one host at a time and puts ESXi server maintenance mode, reboots the server and the puts it back online. If VMs are running on the host DRS will need to be enabled in fully automated mode to allow VMs to VMotion off to other hosts (There should also be enough HA capacity in cluster to have 1 host taken offline at a time.

###################
## reboot-vmcluster.ps1 
## Supply the hostname/FQDN for you vcenter server and the name of the cluster you want rebooted
## Script reboots each ESXi server in the cluster one at a time
###################
##################
## Args
##################
# Check to make sure an argument was passed
if ($args.count -ne 2) {
Write-Host “Usage: reboot-vmcluster.ps1 ”
exit
}

# Set vCenter and Cluster name from Arg
$vCenterServer = $args[0]
$ClusterName = $args[1]

##################
## Connect to infrastructure
##################
Connect-VIServer -Server $vCenterServer | Out-Null

##################
## Get Server Objects from the cluster
##################
# Get VMware Server Object based on name passed as arg
$ESXiServers = @(get-cluster $ClusterName | get-vmhost)

##################
## Reboot ESXi Server Function
## Puts an ESXI server in maintenance mode, reboots the server and the puts it back online
## Requires fully automated DRS and enough HA capacity to take a host off line
##################
Function RebootESXiServer ($CurrentServer) {
# Get Server name
$ServerName = $CurrentServer.Name

# Put server in maintenance mode
Write-Host “#### Rebooting $ServerName ####”
Write-Host “Entering Maintenance Mode”
Set-VMhost $CurrentServer -State maintenance -Evacuate | Out-Null

$ServerState = (get-vmhost $ServerName).ConnectionState
if ($ServerState -ne “Maintenance”)
{
Write-Host “Server did not enter maintanenace mode. Cancelling remaining servers”
Disconnect-VIServer -Server $vCenterServer -Confirm:$False
Exit
}
Write-Host “$ServerName is in Maintenance Mode”

# Reboot blade
Write-Host “Rebooting”
Restart-VMHost $CurrentServer -confirm:$false | Out-Null

# Wait for Server to show as down
do {
sleep 15
$ServerState = (get-vmhost $ServerName).ConnectionState
}
while ($ServerState -ne “NotResponding”)
Write-Host “$ServerName is Down”

$j=1
# Wait for server to reboot
do {
sleep 120
$ServerState = (get-vmhost $ServerName).ConnectionState
Write-Host “… Waiting for reboot”
$j++
}
while ($ServerState -ne “Maintenance”)
$RebootTime=$j/2
Write-Host “$ServerName is back up. Took $RebootTime minutes”

# Exit maintenance mode
Write-Host “Exiting Maintenance mode”
Set-VMhost $CurrentServer -State Connected | Out-Null
Write-Host “#### Reboot Complete####”
Write-Host “”
}

##################
## MAIN
##################
foreach ($ESXiServer in $ESXiServers) {
RebootESXiServer ($ESXiServer)
}

##################
## Cleanup
##################
# Close vCenter connection
Disconnect-VIServer -Server $vCenterServer -Confirm:$False

Example of Script Output:

>.\reboot-vmcluster.ps1 vcenter.domain.com demo-cluster
#### Rebooting esxi06.domain.com ####
Entering Maintenance Mode
Rebooting
esxi06.domain.com is Down
Waiting for Reboot ...
Waiting for Reboot ...
Waiting for Reboot ...
esxi06.domain.com is back up
Exiting Maintenance mode
#### Reboot Complete####

#### Rebooting esxi05.domain.com ####
Entering Maintenance Mode
Rebooting
esxi05.domain.com is Down
Waiting for Reboot ...
Waiting for Reboot ...
Waiting for Reboot ...
Waiting for Reboot ...
esxi05.domain.com is back up
Exiting Maintenance mode
#### Reboot Complete####