PowerCLI: Windows Disk Alignment on VMware

Searching for disk alignments off the windows 2003 servers on VMware ESX i found a CLI script which can automate the search. The original site: http://ict-freak.nl/2009/12/15/powercli-check-partition-alignment-windows-vms-only/

Script:

$myCol = @()
$vms = get-vm | where {$_.PowerState -eq "PoweredOn" -and `
$_.Guest.OSFullName -match "Microsoft Windows*" } | Sort Name 

foreach($vm in $vms){
$wmi = get-wmiobject -class "Win32_DiskPartition" `
-namespace "root\CIMV2" -ComputerName $vm            

    foreach ($objItem in $wmi){
        $Details = "" | Select-Object VMName, Partition, Status
            if ($objItem.StartingOffset -eq "65536"){
                $Details.VMName = $objItem.SystemName
                   $Details.Partition = $objItem.Name
                $Details.Status = "Partition aligned"
            }
            else{
                $Details.VMName = $objItem.SystemName
                   $Details.Partition = $objItem.Name
                $Details.Status = "Partition NOT aligned"
            }
    $myCol += $Details
    }
}
$myCol | Export-Csv -NoTypeInformation "C:\PartitionAlignment.csv"
#$myCol | Export-Clixml "C:\PartitionAlignment.xml"

PowerCLI: VMware Backup Script

Searching for way’s to create easy backup’s of a few virtual machines, i came on the website gestalt it (http://gestaltit.com/all/tech/virtualization/simon/vmware-backup-powercli-script/).

On this website there is a script which will create a snapshot of a VM, then clone it (thin provisioned) and finally remove the snapshot off the original VM.

Backup Script

# Import Backup CSV
$backupinfo =  Import-Csv C:\scripts\mybackups.csv

#Set Date format for clone names
$date = Get-Date -Format "yyyyMMdd"

#Set Date format for emails
$time = (Get-Date -f "HH:MM")

#Connect to vCenter
Connect-VIServer ""

foreach ($customer in $backupinfo)
{
	$vm = Get-VM $customer.MasterVM

	#Send Start Email
	C:\scripts\backupstartedemail.ps1

	# Create new snapshot for clone
	$cloneSnap = $vm | New-Snapshot -Name "Clone Snapshot"

	# Get managed object view
	$vmView = $vm | Get-View

	# Get folder managed object reference
	$cloneFolder = $vmView.parent

	# Build clone specification
	$cloneSpec = new-object Vmware.Vim.VirtualMachineCloneSpec
	$cloneSpec.Snapshot = $vmView.Snapshot.CurrentSnapshot

	# Make linked disk specification
	$cloneSpec.Location = new-object Vmware.Vim.VirtualMachineRelocateSpec
	$cloneSpec.Location.Datastore = (Get-Datastore -Name $customer.BackupDS | Get-View).MoRef
	$cloneSpec.Location.Transform =  [Vmware.Vim.VirtualMachineRelocateTransformation]::sparse

	$cloneName = "$vm-$date"

	# Create clone
	$vmView.CloneVM( $cloneFolder, $cloneName, $cloneSpec )

	# Write newly created VM to stdout as confirmation
	Get-VM $cloneName

	# Remove Snapshot created for clone
	Get-Snapshot -VM (Get-VM -Name $customer.MasterVM) -Name $cloneSnap | Remove-Snapshot -confirm:$False

	#Send Complete Email
	C:\scripts\backupcompletedemail.ps1
}
#Disconnect from vCentre
Disconnect-VIServer -Confirm:$false

Send Started Email Script

#Set Date format for emails
$timestart = (Get-Date -f "HH:MM")

$emailFrom = ""
$emailTo = ""
$subject = "[$vm - Backup Started]"
$body = "Backup Details
-------------
VM Name:",$vm,"
Clone Name:","$vm-$date","
Target Datastore:", $customer.BackupDS,"
Time Started:", $timestart

$smtpServer = ""
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($emailFrom,$emailTo,$subject,$body)

Send Completed Email Script

#Set Date format for emails
$timecomplete = (Get-Date -f "HH:MM")

$emailFrom = ""
$emailTo = ""
$subject = "[$vm - Backup Complete]"
$body = "Backup Details
-------------
VM Name:",$vm,"
Clone Name:","$vm-$date","
Target Datastore:", $customer.BackupDS,"
Time Started:", $timestart,"
Time Completed:", $timecomplete
$smtpServer = ""
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($emailFrom,$emailTo,$subject,$body)

CSV File

The content of the csv file is very simple. This is what mine looks like:

MasterVM,BackupDS
VM1,BackupDataStore
VM2,BackupDataStore

ESX Host’s disconnected

ESX communicates with virtual center through what is loosely described as, “management agents”. When you hear TSE’s talk about “management agents”, we’re really talking about 3 things: vpxa, hostd, and vpxd 

 Vpxa lives on the ESX host (on the service console), it communicates with hostd. It’s mostly a listener service, and is very rarely an issue. Hostd lives on the ESX host (on the service console). This is the lion, the vast majority of “disconnects” indicate a problem with hostd, and vpxd lives on your virtual center server. These 3 services form a communications chain, and failure of one or more of these services, tends to produce “disconnects”.

Restarting the Management agents on an ESX or ESXi Server

To restart the management agents on ESX host:
  1. Log in to your ESX Server as root from either an SSH session or directly from the console of the server.
  2. Type “service mgmt-vmware restart”.
    Caution: Ensure Automatic Startup/Shutdown of virtual machines is disabled before running this command or you risk rebooting the virtual machines.
  3. Press Enter.
  4. Type “service vmware-vpxa restart”.
  5. Press Enter.
  6. Type “logout” and press Enter to disconnect from the ESX host.
To restart the management agents on ESXi:
  1. Connect to the console of your ESXi Server.
  2. Press F2 to customize the system.
  3. Login as “root”.
  4. Using the Up/Down arrows navigate to Restart Management Agents.
  5. Press Enter.
  6. Press F11 to restart the services.
  7. When the service has been restarted, press Enter.
  8. Press Esc to logout of the system.

Links:

Automating VMware with PowerShell

LAB Summary
Welcome to the Automating VMware with PowerShell: Hands-On Lab. This lab will provide you hands-on experience in writing PowerShell scripts for managing VMware using the VI Toolkit (for Windows). Each participant will create scripts that will show you how to more effectively manage your virtual infrastructure. Some familiarity with PowerShell is helpful, but is not required. 
 
LAB Objective
The objective of this lab is to introduce you to the VI Toolkit (for Windows) and show you how easy automating VMware Virtual Infrastructure management is when you use PowerShell. We will cover a wide range of topics, including things like provisioning, storage, networking and monitoring. 

 Link: http://blogs.vmware.com/vipowershell/files/vmworld_europe_2008_powershell_lab_7_manual.pdf

Or local download: [wpdm_file id=”15″]

ESX 3.5 Logfile Retrieval

vLogView is a Windows® based application which manages VMware® ESX Server log files.

The product is designed to be installed on an administrators PC, and provides the ability to store, view and search ESX Server log files from multiple ESX Servers, without requiring individual login, navigation and manual file transfer.

http://xtravirt.com/xd10132