Home Datacenter PowerCLI One-liners for Storage Problems

PowerCLI One-liners for Storage Problems

by Philip Sellers

PowerCLIAutomation has been a huge part of my recent learning curve.  I’ve spend a considerable amount of time working with tools to decrease my manual administration time within our VMware environment.  One tool is vCenter Orchestrator and the other is trusty old PowerCLI.

In the process, I’ve found myself increasingly spending more time with PowerCLI to do my daily job.  PowerCLI has really changed how I administer and maintain my VMware farms and how I script and easily address issues in the environments.  For any repeated task, there is usually a quick one or two line PowerCLI command to handle it.  The following are a list of my handy scripts and one-liners for storage issues.  I hope you find them useful.


Bulk Storage vMotions

You can certainly do storage vMotions in the GUI – but its so much more fun from the command line and more efficient in my opinion.  Say you’re moving everything from one LUN to another for maintenance or migration – script it, like below:

[sourcecode language=”powershell”]Get-VM -Datastore <SourceDatastore1> | Move-VM -Datastore <TargetDatastore> -runasync[/sourcecode]

It is way to easy to move all of your VMs in one line than manually move them one by one.  Maintain your sanity with this one-liner!

Find  RDMs in your environment

This process will find any RDMs in your environment. I’ve found it useful as we attempt to phase out use of RDMs.

[sourcecode language=”powershell”]Get-VM | Get-HardDisk -DiskType "RawPhysical","RawVirtual" | Select Parent,Name,ScsiCanonicalName,DiskType[/sourcecode]

Cleanup Backup Snapshots

A common problem in our environment are “Consolidate” snapshots that our backup process leaves behind.  Find and irradicate those with this quick command:

[sourcecode language=”powershell”]Get-VM | Sort Name | Get-Snapshot | Where { $_.Name.Contains(“Consolidate”) } | Remove-Snapshot[/sourcecode]

Find Lost or Unknown Snapshots

Our backup process also seems to leave behind orphaned snapshots, snapshots vCenter or ESX no longer knows about. vCenter sees the VM in linked-clone mode, where the VMX file points to a snapshot VMDK instead of the original parent.

VMs in linked clone mode prevents storage vMotion, so they can be very problematic.  The following one-liner compares a list of known VMware snapshots with a list of all VMs with VMDK files pointing to snapshot VMDKs.  The resulting list is a list of target VMs to be cleaned up.

[sourcecode language=”powershell”]Compare-Object $(Get-VM | SORT | Get-Harddisk | WHERE { $_.Filename -like "*0000*" } | SELECT @{Name="VM";Expression={$_.Parent}} | Get-Unique -asstring) $(Get-VM | Get-Snapshot | SELECT VM | Get-Unique -asstring) -Property VM[/sourcecode]

Repair Lost or Unknown Snapshots

Knowing is only half the battle. While technically not a one-liner, the following routine will take the list you created with the one-liner above and merge those linked clones back together.  Trolling the VMware Communities forums, I found that if you create a new snapshot and then remove all snapshots on VMs in this unknown snapshot mode, VMware rediscovers the ‘lost’ snapshot and will indeed remove them.  That’s the gist of what this routine does:

[sourcecode language=”powershell”]$TARGETS = Compare-Object $(Get-VM | SORT | Get-Harddisk | WHERE { $_.Filename -like "*0000*" } | SELECT @{Name="VM";Expression={$_.Parent}} | Get-Unique -asstring) $(Get-VM | Get-Snapshot | SELECT VM | Get-Unique -asstring) -Property VM
FOREACH ($TARGET in $TARGETS) {
new-snapshot -Name "SnapCleanup" -VM $TARGET.VM
}
FOREACH ($TARGET in $TARGETS) {
write $TARGET.VM.Name
get-snapshot -VM $TARGET.VM | remove-snapshot -Confirm:$false
}[/sourcecode]

Wallpaper

For anyone looking for wallpaper for their office or cube, I’d suggest a PowerCLI Cmdlet Poster – version 4.1 here or version 5.1 here.  I have one in my cube and I find it handy for a quick reference for cmdlets.

You may also like