Powershell Oneliner #6

Today’s oneliner is an incredibly fast way to check the usage of your VMware datastores. You should first connect to Virtual Center in the following way:

$VC = Connect-VIServer “YourVCServerName”

Here comes the oneliner:

Get-Datastore | Sort-Object Name | %{Get-View $_.Id} | Format-Table @{Label=”Name”;Expression={$_.info.name}}, @{Label=”NumVMs”;Expression={$_.vm.length}}

Only interested in datastores that are not used? Use the Where-Object cmdlet:

Get-Datastore | Sort-Object Name | %{Get-View $_.Id} | Where {$_.vm.length -eq 0} | Format-Table @{Label=”Name”;Expression={$_.info.name}}, @{Label=”NumVMs”;Expression={$_.vm.length}}

But watch out! Consider a vm with a disk in datastore A and the vmx in datastore B. When you create a snapshot of the vm, the delta files of all disks will be stored with the vmx (in datastore B). The script (as does the VI Client) will show the vm to be not connected to datastore A!



Powershell Oneliner #5

Why I love Powershell:

Yesterday I needed to create an overview of people, their department name and office location. I wanted to have this list in Excel. But all I had was a textfile with their names. A tedious task to fill in all this information, one might think. Luckily, I know Powershell:

Get-Content "D:scriptspeople.txt" | % { Get-QADUser -Name $_ } | Select-Object Name, Department, Office | Export-Csv -Path "D:scriptsoverview.csv" -NoTypeInformation

This simple oneliner generated the entire overview for me in the blink of an eye! My colleagues were flabbergasted ūüôā

Gotta love it.

Powershell Oneliner #4

Yesterday, alanrenouf asked the following question on the VMware Community VI Toolkit forums:

Is there a way (preferably a one-liner) to get a list of vm’s and the number of snapshots per vm?

Here’s a script that will get that info:

$VC = Connect-VIServer $VCServerName
$vms = Get-VM
$myCol = @()
ForEach ($vm in $vms)
  $snapshots = Get-Snapshot -VM $vm
  $myObj = "" | Select-Object VM, NumSnapshots
  $myObj.VM = $vm.name
  $myObj.NumSnapshots = ($snapshots | measure-object).count
  $myCol += $myObj
$myCol | Where-Object{$_.NumSnapshots -gt 0} | Sort-Object VM | Format-Table -AutoSize

And here’s a one-liner that does the same thing:¬†

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

Powershell Oneliner #3

Today’s Powershell¬†Oneliner is one to make your manager happy:

Get-QADComputer -sizeLimit 0 |
  Group-Object -Property OSName,OSServicePack -noelement |
  Sort-Object -Property Count -Descending |
  Format-Table -Property Count,Name

 All this simple oneliner does, is query Active Directory for computer objects and group them by OS Name and Service Pack. The output looks something like this:

                      Count Name
¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬† —–¬†¬†¬†¬†¬† —-
                      1111   Windows XP Professional, Service Pack 2
                        197   Windows Server 2003, Service Pack 2
                        136   Windows Server 2003, Service Pack 1
                        117   Windows 2000 Server, Service Pack 4
                          15   Windows XP Professional, Service Pack 1
                            6   Windows 2000 Server, Service Pack 3

¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬† 1¬†¬† Windows Server¬ģ 2008 Standard, Service Pack 1

Don’t you just love the ease¬†with which you can create such a handy little overview? I know you manager will!

Powershell Oneliner #2

A quick way to check if any of your ESX server’s CPU load reached “red” levels recently:

Host CPU oneliner

PowerShell Oneliner #1

One of the great things about Windows PowerShell, is that it allows us IT Administrators to write relatively simple, single-line commands to retrieve specific information about our servers and present it just the way we want. I will be posting my own PowerShell Oneliners frequently and explain how they work.

Check the size of type 3, logical volumes in GB, the free space in MB and calculate the percentage free space:

Get-WmiObject Win32_logicaldisk |

Where-Object {$_.drivetype -eq 3} |

Format-Table -Property Name,VolumeName,`

@{label=”Size (GB)”;expression={[math]::truncate($_.size/1GB)}},`

@{label=”Free Space (MB)”;expression={[math]::truncate($_.freespace/1MB)}},`

@{label=”Percent Free”;expression={[math]::truncate((($_.freespace/1GB)/($_.size/1GB))*100)}}

First, I use Windows Management Instrumentation (WMI) to get all information available about the logical disks in my computer.

The result is piped to Where-Object, which filters out the objects that represent a volume.

Then I specify the way I want the output to be formatted: which is in a table, with columns showing the Driveletter, Volume Name, Size, Free Space and Percent Free. I am using a trick here to do some calculations on the properties before I display them. You can use the format @{label=”label”;expression={expression}} to achieve this.

By the way: For readability I have truncated the command at the pipeline character and used the line break character ` (backtick) in several other places. You can however put the entire command in a single line and execute it from the PowerShell Command Prompt.