PowerShell Profile Tricks for Better VMware Management

As an IT pro, we rely on scripts to manage our VMware environment, which helps us be more efficient throughout our work day. In this article, I’d like to share some PowerShell profile tricks that are specific to VMware. These are tricks that I use on a daily basis, which I think you’ll find helpful.

If you’re unfamiliar with the PowerShell profile, you can become familiar with profiles and their uses in the Petri IT Knowledgebase article, “Getting Started with PowerShell Profiles.”

Using Transcripts in Your Windows PowerShell Profile

You can do a number of things with the command-line interface (CLI), and you probably rely on it to rerun useful tasks and commands to retrieve specific information about virtual machine performance and more. Because you’ve likely spent a decent amount of time tinkering and delving into the specific details that you needed, you probably don’t want to repeat the process all over again.

To make life easier for us, we can use a transcript.

Stop-Transcript | out-null

Start-Transcript ("C:\Users\maishsk\Documents\scripts\log\" + (Get-Date -Format dd-MM-yyyy_HH-mm) + ".log")

A transcript creates a record of all or part of a Windows PowerShell session and saves it in a text file. That means anything that you run or anything that you receive as output in the session will be recorded to a file.

The above two lines do something very simple.

  1. If you already have a session running with a transcript, then it will stop running that transcript.
  2. The code also starts a new transcript and saves the file to a file, which will be named according to a timestamp.

Using a PowerShell Profile to Connect to vCenter

What’s the first thing you usually want to do when you open up PowerCLI? Connect to a vCenter and get things done. Why do we want to waste time by performing an extra step to connect?

In the following lines of code, I can define the connection in my PowerShell $PROFILE to connect automatically.

Add-PSSnapin VMware.VimAutomation.Core

#Variables
$vcenter = "vc.maishsk.local"
#Credentials 
$vicred = New-Object System.Management.Automation.PsCredential "MAISHSK\Maish", (Get-Content "C:\Users\maishsk\Documents\scripts\maish.cred" | ConvertTo-SecureString)

Write-host "Connecting to MSAIDELK-LAB1 ...." 
Connect-VIServer $vcenter -Credential $vicred  | Out-Null

if ( $? ) {
   Write-Host -ForegroundColor Green "-------------------------------------"
   Write-Host "Connected to msaidelk-lab1 Successfully"
   Write-host "-------------------------------------"
   ""
} else {
   Write-Warning "Connection to MSAIDELK-LAB1 Not Established. Check What is wrong!!!"
}

I’m now ready to go as soon as my prompt becomes available. Of course, I also want to know that the connection was successful, where the command lets me know if the code executed successfully or not.

I’ve taken several different steps for instantiating my $PROFILE. Let’s take a closer look at what the code is doing here:

  1. In this first line of code, I’m going to import the VMware snap-in by using Add-PSSnapin, which allows me to interact with vCenter.
  2. In line four, I’ve outlined the name of my vCenter Server, which is vc.maishsk.local.
  3. In line six, I reference a credential file that I use with a pre-stored password and matching username.
  4. I’m going to assume that you’re using Active Directory for authentication to your vCenter server, so this next piece of information might not be necessary. Because I’m using a vCenter server that’s not part of the production domain, I loaded the credentials from a secure string and now have that stored in a variable that’s ready for use. This can also be useful if you need to manage multiple vCenters with different credentials. By using this method, you can store them all as variables and not have to worry about having to enter your credentials each time you connect.
  5. A quick note about the if-else statement on line 11: In this clause, I check to see if the last command was successful and put a conditional clause that allows for two scenarios. The first scenario accounts for a successful connection that will provide a “Connected to msaidelk-lab1 Successfully.” If the connection does not work, then you’ll be prompted by a warning.

To go a step further, you can also commit your changes to GitHub. You can find my $PROFILE located on Github.

I hope this was informative, and please feel free to leave your feedback in the article comments below.