Archive

Posts Tagged ‘powershell’

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: , , ,

Exporting all that useful VM information with PowerCLI

August 16th, 2010 14 comments

There are many occasions when you may need to produce a report showing some aspect of your VM environment.

Many companies have various types of inventory databases which grab information from various sources to provide tracking and reporting of the various IT assets.

An example of a database could be one which pulls information from the software inventory system, patch management system, anti-virus console showing latest file update, AD etc. which could show a dashboard view of the workstations and highlight where they are not in compliance to company standards.

As a virtual workstation environment grows there is plently of information from vCenter that would be very useful to gather.

Grouping VMs by VDI groups in vCenter folders so you know how many VMs x department has would be useful to have. For the dashboard view it may be usedful having the VM Tools status. It’s could also be useful knowing which VMs are in which cluster and datastore, what virtual CPU and RAM they have and how big their disks are which can be used for capacity planning, performance analysis and reporting on what is deployed where.

Read more…

Categories: PowerCLI, Powershell Tags: , ,

Tracking down those connected CD-ROMS with PowerCLI

August 13th, 2010 7 comments

Here are some handy script lines for working out what CDs you have connected which can cause Vmotion to fail.
You don’t always want to disconnect all CDs. If you have .ISOs on shared storage Vmotion will be fine as long as the hosts can both see the .ISO files.

Get the names of VMs with connected CD drives:

get-vm | where { $_ | get-cddrive | where { $_.ConnectionState.Connected -eq "true" } } | select Name

Get the names of VMs with connected .ISOs

get-vm | where { $_ | get-cddrive | where { $_.ConnectionState.Connected -eq "true" -and $_.ISOPath -like "*.ISO*"} } | select Name, @{Name=".ISO Path";Expression={(Get-CDDrive $_).isopath }}

Disconnect all VMs where the CD Drive is connected and it is not an .ISO

$VMs = Get-VM
$CDConnected = Get-CDDrive $VMs | where { ($_.ConnectionState.Connected -eq "true") -and ($_.ISOPath -notlike "*.ISO*")}
If ($CDConnected -ne $null) {Set-CDDrive -connected 0 -StartConnected 0 $CDConnected -Confirm:$false }

or another slightly quicker and dirtier way thanks to commenter José de Alonso

Get-VM | Get-CDDrive | Where {$_.ISOPath -ne $null} | Set-CDDrive -NoMedia -Confirm:$false

 

Categories: PowerCLI, Powershell Tags: , ,

Pimping your Powershell Profile

August 11th, 2010 1 comment

If you work on multiple systems and need to use powershell to manage them but they all have their own separate installation it can drive you mad having so many different console shortcuts to launch.

I have 3 at the moment, VMware vSphere PowerCLI, Citrix XenServer PowerShell SnapIn and NetApp’s Data ONTAP PowerShell Toolkit.

Each one installs slightly differently and then runs separately which is a pain.

VMware vSphere PowerCLI installs a PSSnapin and creates this shortcut:

C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -psc “C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1” -noe -c “. \”C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-VIToolkitEnvironment.ps1\””

Citrix XenServer PowerShell SnapIn installs a PSSnapin and creates this shortcut:

"C:\Program Files\Citrix\XenServerPSSnapIn\XenServerPSSnapIn.bat"

which runs

@C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
-PSConsoleFile XenServerPSSnapIn.psc1 -Noexit -Nologo

NetApp’s Data ONTAP PowerShell Toolkit requires you to copy files to the Module Path and then use Import-Module to load the snap-in.

So, Profiles to the rescue……powershell runs a startup script called your profile which is just a text file really.

What I really would like is to have a single profile that launches my session on some computers and starts all 3 snap-ins if they are installed.

That’s not a big ask, is it?

Well, lets start with setting up the profile on this particular computer to launch all 3 snap-ins but be clever and allow it to also be run from anywhere.

Read more…

Categories: Powershell Tags: ,

SRM for free with PowerCLI – Part 2: iSCSI

August 3rd, 2010 No comments

My previous post talked about the design of BR and how to script a simple and scalable recovery process for getting as many VMs as you need from a primary site to a BR site when using NFS storage.

When using iSCSI storage it is slightly more complicated as you can’t use one of the benefits of NFS in presenting a read only datastore to ESX hosts in your BR site and having your VMs in the inventory and ready to just power on.

The manual steps would be:

Break the storage mirror.
Rescan the storage on the ESX hosts in the recovery site to pick up the now-visible LUN.
Browse the LUN for all .vmx files and add them into the inventory
Power on the VMs and answer the UUID question that they have moved.

Thanks LucD who has two great versions of a function for importing .VMX files, much better than the old community extension method.
http://communities.vmware.com/message/1317271#1317271
http://communities.vmware.com/message/1270822#1270822

The function is very powerful so it’s worth keeping as a link for using elsewhere.

Read more…

SRM for free with PowerCLI – Part 1: NFS

August 3rd, 2010 No comments

Also see SRM for free with PowerCLI – Part 2: iSCSI

Working in an enterprise company DR, BR, BCP, whatever you want to call it, is everything.  With potentially many thousands of people around the globe, being able to get people working again as soon as possible after a disaster can mean the difference between being in business and going bust.

The best way to do business recovery is to design it from the ground up and make it part of every architecture design you put together.  Thinking about the what-if of a disaster after everything is up and running often means you have to start again.

One of the great things about virtual worlds is you can easily (not necessarily cheaply though!) have a separate virtual infrastructure in both locations with shared storage. Simply by mirroring the storage from Site A to Site B you can recover VMs from Site A in Site B very easily without having to have a separate VM in Site B that you need to keep up to date.

This is where VMware’s SRM comes into play.  SRM is a fairly powerful piece of software for running and testing your BR.  It talks to your back-end storage and can automate the different steps required such as breaking the storage mirror, presenting the storage to your ESX hosts, adding the VMs to your inventory and powering them on.

Sounds great but firstly it is a product you need to pay for and secondly when it came out it didn’t support NFS so PowerCLI (it wasn’t called PowerCLI when we did it) came to the rescue.

Read more…

Categories: PowerCLI, Powershell Tags: , , ,