Tag Archives: Oneliner

TOP 10 Commandlets in a Oneliner

“It’s the time for lists”, I read somewhere today. Sure, that is true. But blogging little lists isn’t much fun.

So, I will let you generate your very own TOP 10 list. Use the following Powershell Oneliner to get a TOP 10 list of your most frequently used cmdlets in your scripts. (Remember to modify the path to your script storage location.)

Happy Holidays!

Get-ChildItem D:Scripts -Filter *.ps1 -Recurse |
 Select-String (Get-Command|%{$_.Name}) |
 Group Pattern |
 Sort Count -Descending |
 Select -First 10 |
 Format-Table Name, Count -AutoSize

VMware Stats Oneliner

Today’s oneliner is a nifty little function that reports the average CPU and Memory usage for one or more of your VMs, calculated over the last x hours:

function Get-VMStat
{
param( $VM,[Int32]$Hours = 1 )
 $VM | Sort Name |
 Select Name, @{N=”CPU”;E={[Math]::Round((($_ |
 Get-Stat -Stat cpu.usage.average -Start (Get-Date).AddHours(-$Hours) -IntervalMins 5 -MaxSamples ($Hours*12) |
 Measure-Object Value -Average).Average),2)}}, @{N=”MEM”;E={[Math]::Round((($_ |
 Get-Stat -Stat mem.usage.average -Start (Get-Date).AddHours(-$Hours) -IntervalMins 5 -MaxSamples ($Hours*12) |
 Measure-Object Value -Average).Average),2)}}
}

A great way to use it is like this:

Get-VMStat (Get-VM) -Hours 4 | Where {$_.CPU -gt 50 -or $_.MEM -gt 75}

I’m sure you can figure out what that does. Be the first to give the correct answer in the comments to win everlasting glory! (or at least until the next oneliner…)

Enjoy!

Oneliner of the day

Do your fellow VMware Admins sometimes forget to fill in the Notes field for a new vm they create? Or would you like to change those inconsistent notes to something more accurate? Todays oneliner simply copies the Active Directory description field into your Virtual Infrastructure. It’s quite blunt about it. Besides asking if you really want to do so, it shows a lot of errors for servers that are powered off or cannot be found in AD. But it pretty much does the trick.

Get-VM | ForEach { Set-VM -VM $_ -Description (Get-QADComputer ($_ | Get-View).Guest.HostName.Split(“.”)[0]).Description }

If your vm’s always have the same name as defined in the VM Guest OS, you can suffice with:

Get-VM | ForEach { Set-VM -VM $_ -Description (Get-QADComputer $_.Name).Description }

If you want more advanced description synching, check out the full script.

Enjoy!

Oneliner for the road

Okay, so I’ve just announced I won’t  post anything for the coming month. You must all feel very sad.

Well, before leaving you sobbing over an empty feed reader, here’s one more oneliner. One for the road.

Get-VMHost | Sort Name | Get-View | Select Name, @{N=”MemoryGB”;E={[math]::Round((($_.Summary.Hardware.MemorySize)/1GB),0)}}, @{N=”MemoryUsageGB”;E={[math]::Round((($_.summary.quickstats.overallmemoryusage)*1MB/1GB),2)}}, @{N=”Percentage”;E={[Math]::Round(100*(($_.Summary.QuickStats.OverallMemoryUsage*1MB/1GB)/($_.Summary.Hardware.MemorySize/1GB)),0)}}

What the *bleep* does that do? I’ll explain it to you:

  • It grabs your VMware Servers (Get-VMHost)
  • Sorts them by Name (Sort Name)
  • Gets the advanced properties (Get-View)
  • Selects some properties to show you (Select), being:
    • The server Name (Name)
    • Something called MemoryMB, which is the value of the Summary.Hardware.MemorySize property devided by 1 GB and rounded to zero digits.
    • Something called MemoryUsageGB, which is the value of the Summary.QuickStats.OverallMemoryUsage property (which is already in MBs), converted to GBs and rounded to two digits.
    • Something called Percentage, which is 100 times the MemoryUsage in GBs divided by the Memory in GBs, rounded to zero digits.

Can’t you do something like that with Get-Stat?
Sure, but wouldn’t be near as much fun.

Adios!
Hugo

Posting Pause (plus bonus oneliner)

I’m sorry to announce that I won’t be able to post anything until the beginning of October. But I will be back, so please keep me in your RSS feed or check back here in a month. I’ll be back full-force, with more awesome one-liners, helpful scripts and fantastic functions!

Here’s a one-liner to show off to your colleagues. What do you do when somebody asks you for a printed overview of the top-twenty volumes on your virtual servers with the least free disk space?

You fire up your trusty Powershell of course! You type a single line of code, press enter and tell them to check the printer. How cool is that?

Get-VM | Where { $_.PowerState -eq “PoweredOn” } | Get-VMGuest | Select VmName -ExpandProperty Disks | Select VmName, Path, @{N=”MBFree”;E={[math]::Round((($_.FreeSpace)/1MB),2)}} | Sort MBFree | Select -First 20 | Format-Table -AutoSize | Out-Printer

Very cool indeed!

See you next month!

Hugo

Quick VI Toolkit Oneliners

Today, I have some quick-and-easy VI Toolkit ‘Get-VIEvent’ one-liners for you. Let’s get started right away:

To get all events relating to a certain Virtual Machine, try this:

Get-VM “MyVMName” | Get-VIEvent | Format-Table CreatedTime, FullFormattedMessage -AutoSize

To get all errors from the past 24 hours, enter:

Get-VIEvent -Start (Get-Date).AddHours(-24) -Type Error | Format-Table CreatedTime, FullFormattedMessage -AutoSize

To get all errors related to a cluster, type:

Get-Cluster “MyClusterName” | Get-VIEvent -Type Error | Format-Table CreatedTime, FullFormattedMessage -AutoSize

Finally, to get all events related to DRS:

Get-VIEvent | Where {$_.FulFormattedMessage -match “DRS”} | Format-Table CreatedTime, FullFormattedMessage -AutoSize

As you can see, the possabilities are endless!
Enjoy!

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.