PowerCLI GitHub Repo – March 2014 Updates

In March, I created my PowerCLI GitHub Repo with just two cmdlets. By the end of March, I had many more cmdlets in the repo. Here are the updates:


Via http://tenthirtyam.org/per-cluster-cpu-and-memory-utilization-and-capacity-metrics-with-powercli/ (@medavamshi):

  • Get-ClusterStats – A very detailed report on current Cluster resources and rough estimates of resources available after 1 or 2 cluster member failures. Useful in predicting failure scenarios as well as an eyeball view of capacity management.

Via http://hostilecoding.blogspot.com/ (@hostilecoding):

  • Edit-v10VMs – An alternative GUI to vCenter that can edit VMs with vHW 10. Useful for those without vCenter or who do not like the vSphere Web Client.

Via http://myvirtualcloud.net/?p=5924 (@StevenPoitras and @andreleibovici) are a pair of cmdlets useful for stress-testing your storage (and vCenter, if you’re not careful):

  • Clone-VM – Spin up a specified number of clones of the named VM, using VAAI by default.
  • Unclone-VM – Provide the name of the cloned VM and stop/delete all the clones.

Via http://pelicanohintsandtips.wordpress.com/2014/03/13/creating-multiple-virtual-machines-with-powercli/ is a single cmdlet for Template deployments

  • Deploy-Template – Use an existing Template and OSCustomizationSpec to deploy multiple instances of a Template into a specified Datacenter/Folder with sequential IPs.


Via http://www.shogan.co.uk/vmware/three-powercli-scripts-for-information-gathering-vms-hosts-etc/ (@shogan85) come three cmdlets that all have the option to output to CSV as well:

  • Get-VMHostBIOSInfo – Report on the Model and BIOS of all VMHosts attached to your connected vCenter.
  • Get-VMHostESXInfo – Report on the ESX(i) version and build of all VMHosts attached to your connected vCenter.
  • Get-VMHardwareInfo – Report on the vHW version of all VMs in a specified datacenter.

Via http://hostilecoding.blogspot.com/ (@hostilecoding):

PowerShell Trick via @alanrenouf

Alan Renouf gave a vBrownBag presentation on Advanced PowerCLI 5.5R2 last night. During the show, he showed an interesting bit of code:

$output = "" | Select VmName, PgName
$output.VmName = "value1"
$output.PgName = "value2"

This was intriguing to me. The “proper” way to create an object with attributes is to use New-Object and pipeline it through some Add-Member commands.

$output = New-Object PSObject |
Add-Member -PassThru NoteProperty VmName "value1" |
Add-Member -PassThru NoteProperty PgName "value2"

That creates an excessive, and somewhat unreadable, pipeline for objects with a long list of members, especially if a member’s name is long. It’s a pretty neat trick to make your code look pretty neat.

Continue reading

PowerCLI GitHub Repo

Over the past couple of weeks, I’ve done a few PowerCLI posts, including creating some modules. To reduce the change of typos or older versions of files making it into my posts, I created a GitHub repo, https://github.com/rnelson0/powercli-modules/, that you can feel free to use. There are three modules:

  • Copy-Module – Based off the Hey Scripting Guy! module, this enhanced module allows us to load modules in the Global Modulepath (when run with admin privs) and overwrite existing modules.
  • PowerCLI-Administrator-Cmdlets – Cmdlets for an administrator. Generally speaking, these require read/write access. Example: Clone-VDPortgroup
  • PowerCLI-User-Cmdlets – Cmdlets for users. Ideal for read-only users. Example: Get-VMConsoles

Please use GitHub to send me pull requests for any bug fixes or report bugs. You can of course use comments as well.

Something I forgot to mention in my post on creating modules: If you use the snippets and fill out all the comments, Get-Help Your-Cmdlet will use that information and generate full help files for you. Very helpful!

PowerCLI GUI for VM Consoles

A few months ago, Dimitar Barfonchovski created a blog post on accessing the VM Console via PowerCLI, hosted on the PowerCLI Blog. Over the next few days, some cool enhancements came out focusing on a GUI for the featureset – which appear to have been lost to the great bitbucket in the sky, or I’d give some credit to them (if you know what I’m talking about, drop a link in the comments and I’ll update the article). I added a few enhancements of my own and ended up with a quick a dirty pastebin that would allow anyone to authenticate to any vCenter server and get a list of VMs they have access to.

I finally went back and combined this with last Friday’s post on creating a PowerCLI module. With a few tweaks we end up with an auto-import module and the cmdlet Get-VMConsoles. Any user can run this cmdlet. Upon connection to the specified vCenter server, you are prompted for authentication. After successfully authenticating, click Open VM Consoles and you are presented with a list of consoles available to your user. You can add a few filters, as I have done below. Ctrl-click to select the VMs you want to view and hit OK in the bottom right. Your consoles will open in your system’s browser in separate tabs.

Continue reading

Creating a PowerCLI Module

A recent discussion on twitter about creating awesome PowerCLI output reminded me that the joy – and ease! – of creating your own PowerShell module needs to be spread.

If you do things the “right way”, by default you cannot click on a .ps1 file to open it. You need to launch PowerCLI (or launch PowerShell and import the PCLI snapins) and then import the file in whole or in part. Even if you create a new function that you want to use all the time, like a favorite “Get-SnapshotsIStupidlyLeftAroundForTooLong” function, it’s not there just by launching a PCLI/PoSH window. If you then want to modularize your PoSH into discrete parts and re-use them with other functions, you quickly end up with a very large .ps1 file that you import every time to so that all your functions are there. But they’re still functions, not cmdlets.

You don’t need to be a developer to create your own modules. Microsoft has some VERY in-depth documentation on modules that everyone should use. We’ll stick to the highlights as this is PowerShell Modules For Sysadmins. We’ll skip all the stuff about signing your code and making binary modules and focus on what most of us need every day – a way to modularize our PoSH code and streamline our ability to use it. I do encourage you to come back and look at code signing later if you plan to get serious about PoSH.

Continue reading

Migrating non-VM data between two ESXi datastores

Sometimes in the course of your work you’ll find that you need to move data between two datastores on your ESXi host or vCenter server. In my case, this occured twice, once when adding shared storage to my home lab and again when migrating some hosts from VC 5.1 to 5.5 at work. VM data is best moved via storage vMotions, but this may still leave a few files on your datastore, such as the ubiquitous ISOs folder. You could download the files from the datastore, save them on your client, and then upload them to the datastore. That’s fairly clunky, especially with the Web Client, but more importantly, it’s slow and tedious. There’s another way.

The answer is to use PowerCLI and a PSDrive. If you are not familiar with PSDrives, check out this article at the Hey, Scripting Guy! Blog for a primer. The most important command to remember is Get-PSDrive, as that will give you all the information you need to do this again in the future without having to reference this article. If you have PowerCLI installed, you will have a shortcut on your desktop for it. This will launch a window that looks like the command prompt. If you want a little more help as you drive (and you will!), I suggest using PowerShell ISE for tab completion and IntelliType. You can add the PowerCLI snapins with the following commands from the ISE:

Continue reading

Creating consistent Distributed Port Groups with PowerCLI

I recently had to create a new vDS to replicate a standard vSwitch from another vCenter install. I wanted to create my vDS Distribute Port Groups (DPG) simply, but consistently. As I have a low number of DPGs to create, I could probably have done this manually, but scripting the creation ensures consistency. Plus, it’s a subset of PowerCLI that I wanted to familiarize myself with.

First, I created a vDS and a reference DPG through the vSphere Web Client. You can do this with PowerCLI, but you have to go down the rabbit hole of Views to touch some of the advanced settings, something that’s not well documented and would have been very time consuming for me to explore. I also didn’t mind creating the initial vDS and DPG as the visual view of the Web Client made it easy for me to verify the settings whereas a long string of PowerShell (PoSH) would have been a little more difficult to interpret.

Continue reading

Creating VMs and Templates folders with PowerCLI

As part of a migration from one vCenter to another, I wanted to recreate the same VMs and Templates look which meant recreating the folders. This is VERY slow in the vSphere Web Client and only slightly less tedious in the C# client, so I thought I’d use PowerCLI to do the trick. Here is the folder structure I wished to recreate:

-Pending DeComm
  -Active Directory Domain Controllers
  -Linux Servers
  -RDP Servers
  -Template VMs

PowerCLI has a cmdlet called New-Folder. Unfortunately, it only creates folders of the Hosts and Clusters type. To create a new VMs and Templates folder, we have to use the Get-View cmdlet. By viewing a datacenter or folder and filtering on the proper location, we can call methods to create a folder with the right context:

Continue reading

PowerCLI One-Liner – Mark all VM optical drives as Client

I’ve seen a number of articles offering PowerCLI one-liners to find all VMs with connected optical drives, or set to use a datastore, or a number of other possibilities, and mark the drives as disconnected. This is helpful, but may still cause a problem with vMotions if the optical drive is pointing to a non-shared datastore ISO. vMotion does not care if the optical drive is connected, but it does care that it’s pointing to a datastore that not all hosts can see. This one-liner will find all VMs with an ISOPath set for the optical drive and reset it to Client.

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

This looks for all CDDrive objects where the ISOPath value is not null and calls Set-CDDrive with the flag -NoMedia. This should take care of all the VMs where someone has set it to use an ISO on the datastore. It will NOT fix the mapping for VMs connected to the Host Device or Client Device – however, those are more rare and do not usually stand between you and a vMotion. On those rare instances, you may still need to manually dismount the optical drive on the VM or use a PowerCLI one-liner targeting those settings.