Archive

Archive for the ‘PowerCLI’ Category

Installing HP Agents on ESX with PowerCLI and Putty/Plink

March 7th, 2011 2 comments

Anyone who needs to build multiple ESX(i) hosts naturally looks to scripting to automate the process. Scripting allows for faster deployment once you have developed the script but equally important reduces human error. It’s far to easy to mistype a port group name, vlan number or IP address. Scripting removes this element of humen error and allows you to build ESX(i) hosts preditably and quickly.

Unfortunately there are just some things that PowerCLI cannot natively automate such as installing HP agents on ESX as this requires console / SSH access to the ESX host and running the install “locally”.

Hopefully hardware vendors will see the benefits of integration with VMware Update Manager and allow hardware monitoring agents to be installed and updated with Update Manager but until then we have to make another plan.

It is always painful to have developed a fantastic PowerCLI script to automate your build and at the end still have to manually SSH into your ESX host to install a hardware agent.

Read more…

Scripting Flex-10 ESX design with PowerCLI

February 18th, 2011 No comments

My Flex-10 ESX design with simplicity and scalability posts detail the configuration steps required to deploy ESX with Flex-10. There’s a lot of clicking around in the vSphere client that can be automated with PowerCLI.

The steps below will set up the ESX networking component as described in the design posts so can be incorporated into the rest of your PowerCLI build process.

Read more…

Getting Nic firmware versions with PowerCLI

January 27th, 2011 3 comments

Sometimes you need to delve into the innards of ESX to get information out that just isn’t available through the VMware SDK and so not directly available to PowerCLI.

If you’ve been doing any work recently with HP Flex-10 and its firmware requirements, you may need to find out the network card firmware version which is only available from within the ESX console by typing ethtool -i vmnic0

So, PowerCLI to the rescue, but actually using PowerCLI to call Plink.exe which is Putty’s command-line connection tool. You can use this same process to pull anything that you can get from the ESX console as long as you can parse the results of the command and find the information you need.

Read more…

Updating Broadcom bnx2x Nic Drivers with PowerCLI

November 15th, 2010 3 comments

Now that a new bnx2x driver has been released as detailed in my previous post you will obviously need to deploy it and add it as part of your hopefully scripted build.

I wish VMware Update Manager would be able to apply these driver updates so we can have a single depoyment mechanism but this doesn’t seem to be the case yet.  We live in hope!

So, without Update Manager, I always prefer PowerCLI rather than esxupdate for these kinds of deployments as it fits nicely into a PowerCLI scripted build, you don’t have to have SSH access to install and you can also roll it out easily to multiple ESX hosts.

Read more…

Snapshot information from PowerCLI

September 6th, 2010 1 comment

Here are some PowerCLI single line scripts for getting different snapshot information:

Display VM Name and Snapshot Name

Get-VM | Get-Snapshot | select @{name="VM Name"; Expression={$_.vm.name}},name

Display VM Name, PowerState, Host and Snapshot Name

Get-VM | Get-Snapshot | select @{name="VM Name"; Expression={$_.vm.name}},@{name="Power State"; Expression={$_.vm.Powerstate}},@{name="Host"; Expression={$_.vm.Host}},name

Display VM Name and Number of Snapshots

Get-VM | Where{(Get-SnapShot -VM $_ | Measure-Object).Count -gt 0} | Format-Table Name, @{Label="NumSnapshots";Expression={(Get-Snapshot -VM $_ | Measure-Object).Count}}

All snapshots older than a particular date, say 30 days?

Get-VM | Get-Snapshot | Where { $_.Created -lt (Get-Date).AddDays(-0)} | select @{name="VM Name"; Expression={$_.vm.name}},@{name="Power State"; Expression={$_.vm.Powerstate}},name, Created

Datastore and storage information from PowerCLI

September 3rd, 2010 No comments

Here are some PowerCLI single line scripts for getting some storage and datastore information:
Find the Number of VMs on all Datastores

Get-Datastore | Sort-Object Name | Get-View | Format-Table @{Label="Datastore Name";Expression={$_.Info.Name}}, @{Label="Number of VMs";Expression={$_.VM.Length}}

Get VM Guest Disk Free Information

Get-VM | Where { $_.PowerState -eq "PoweredOn" } | Get-VMGuest | Select VMName -ExpandProperty Disks | Select VMName, Path, @{Name="DiskFreeMB";Expression={[math]::Round((($_.FreeSpace)/1MB),2)}}

Get the 10 VM Guest disks with the least amount of free space

Get-VM | Where { $_.PowerState -eq "PoweredOn" } | Get-VMGuest | Select VMName -ExpandProperty Disks | Select VMName, Path, @{Name="DiskFreeMB";E={[math]::Round((($_.FreeSpace)/1MB),2)}} | Sort DiskFreeMB | Select -First 10

Get combined size of each VMs Disks

Get-VM | select Name, @{ Name="Disks"; Expression={ ($_ | get-harddisk | measure-object -property CapacityKB -sum).Sum }}

sorted by size

Get-VM | select Name, @{ Name="Disks"; Expression={ ($_ | get-harddisk | measure-object -property CapacityKB -sum).Sum }} | sort Disks -descending

Get Disk Path, Capacity, Free and Used space

Get-VM | Where { $_.PowerState -eq "PoweredOn" } | Get-VMGuest | Select VMName -ExpandProperty Disks | Select VMName, Path, @{Name="DiskCapacityGB";Expression={[math]::Round((($_.Capacity)/1GB),2)}},@{Name="DiskUsedMB";Expression={[math]::Round((($_.Capacity - $_.FreeSpace)/1MB),2)}},@{Name="DiskFreeMB";Expression={[math]::Round((($_.FreeSpace)/1MB),2)}}

Disk timeout settings in registry for VMs using PowerCLI

September 1st, 2010 3 comments

One of NetApp’s best practices is to increase the disk timeout settings for VMs to 190 seconds so VMs don’t blue screen if they temporary lose connectivity to their disks say during a cluster failover.

This is done by amending the Disk Timeout registry value:
HKLM\SYSTEM\CurrentControlSet\Services\Disk\TimeoutValue to DWord 190.

I’m sure you’d love to go through all your VMs and load regedit manually so why not grab a list of VMs using PowerCLI and set the registry value.

The VMs will obviously need to be powered on and you will need permissions to be able to write to the registry.

The procedure is a 2 step process:

The first is to get all the VMs you want to change.
You can use any Get-VM statement you want:

All VMs in a particular Resource Pool

$VMs = Get-VM -Location (Get-ResourcePool "Resource_Pool_Prod") | Where { $_.PowerState -eq "PoweredOn" }

or

$VMs = Get-ResourcePool "Resource_Pool_Prod" | Get-VM | Where { $_.PowerState -eq "PoweredOn" }

All VMs in a particular Cluster

$VMs = Get-Cluster "LON_PROD1" | Get-VM | Where { $_.PowerState -eq "PoweredOn" }

All VM names starting with WEBSERVER

$VMs = Get-VM "WEBSERVER*" | Where { $_.PowerState -eq "PoweredOn" }

The second stage is to work some registry magic for the VMs you have.

You can then read the current registry value:

ForEach ($VM in $VMs) {
	$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $VM.Guest.Hostname)
	Write-Host "Registry Value for: "$VM.Guest.Hostname ": " $reg.OpenSubKey("SYSTEM\CurrentControlSet\Services\Disk\").GetValue("TimeoutValue")
}

To write the registry value:

ForEach ($VM in $VMs) {
	Write-Host "Setting Registry Value for: "$VM.Guest.Hostname
	$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $VM.Guest.Hostname)
	$regKey= $reg.OpenSubKey("SYSTEM\CurrentControlSet\Services\Disk",$true)
	$regkey.SetValue('TimeoutValue',200,'DWord')
}

Or you can combine a read, set and verify.

ForEach ($VM in $VMs) {
	$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $VM.Guest.Hostname)
	Write-Host "Registry Value Before: "$VM.Guest.HostName "-" $reg.OpenSubKey("SYSTEM\CurrentControlSet\Services\Disk\").GetValue("TimeoutValue")

	$regKey= $reg.OpenSubKey("SYSTEM\CurrentControlSet\Services\Disk",$true)
	$regkey.SetValue('TimeoutValue',190,'DWord')

	Write-Host "Registry Value After:  "$VM.Guest.HostName "-" $reg.OpenSubKey("SYSTEM\CurrentControlSet\Services\Disk\").GetValue("TimeoutValue")
}

or be bold and set it in a one liner:

Get-VM "WEBSERVER*" | Where { $_.PowerState -eq "PoweredOn" } | %{[Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $_.Guest.Hostname).OpenSubKey("SYSTEM\CurrentControlSet\Services\Disk",$true).SetValue('TimeoutValue',190,'DWord')}

Powershell…you gotta love it!

Amending NTP Servers with PowerCLI

August 27th, 2010 No comments
$NTPServers = "10.1.1.1","10.1.1.2"
Get-Cluster "MyCluster*" | Get-VMHost | Sort Name | %{
$_ | Remove-VMHostNtpServer -NtpServer ($_ | Get-VMHostNtpServer) -Confirm:$false
$_ | Add-VMHostNtpServer -NtpServer $NTPServers
$_ | Get-VMHostService | Where-Object {$_.key -eq "ntpd"} | Restart-VMHostService -Confirm:$false
}
Categories: PowerCLI Tags: , ,

Getting VM Time Zone settings with PowerCLI and WMI

August 23rd, 2010 No comments

VM performance troubleshooting is something every VM Admin needs to do.

Often these can be related to agents running within a VM and often these are set to run at a particular local time.

VDI deployments often have people from different time zones working different shifts connecting to VMs in a central site with the idea that a particular agent task will always run out of hours from when people work. This would obviously not happen if the guest local time zone was not set correctly.

PowerCLI to the rescue and a one-liner that connects to a particular set of VMs and uses WMI to find out what time zone is set.

Get-VM -Location (Get-Folder "Workstations-Call Center") | Where { $_.PowerState -eq "PoweredOn" } | select Name, @{ Name="Time Zone"; Expression={(Get-WmiObject -Class win32_TimeZone -Comp $_.Guest.HostName).Caption}}
Categories: PowerCLI, Windows Tags: , ,

Logged on users with Powershell

August 20th, 2010 1 comment

Hugo Peeters has a great function for using WMI to get Logged on Users:
http://www.peetersonline.nl/index.php/powershell/oneliner-get-logged-on-users-with-powershell/

This can be easily used in PowerCLI.  This can be very useful when you need to do some VM maintenance in a VDI environment and need to find out who is currently logged on and know which VMs are not in use and can be worked on.

$VMs = Get-Cluster "LON_PROD1" | Get-VM | Where { $_.PowerState -eq "PoweredOn" }
ForEach ($VM in $VMs) {
	Write-Host "Logged On Users for: " $VM.Guest.HostName
	Get-WmiObject Win32_LoggedOnUser -ComputerName $VM.Guest.HostName | Select Antecedent -Unique | %{"{0}\{1}" -f $_.Antecedent.ToString().Split(’"‘)[1], $_.Antecedent.ToString().Split(’"‘)[3]}
}
Categories: PowerCLI, Powershell Tags: , , ,