As a VMware Administrator, we rely on scripts to manage VMware environment, which helps us be more efficient throughout our day. In this article I want to share some PowerShell profile tricks that are specific to VMware. These are tricks used on a daily basis, which I think you’ll find helpful.
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.
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.
- If you already have a session running with a transcript, then it will stop running that transcript.
- The code also starts a new transcript and saves the file to a file, which will be named according to a time stamp.
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, we can define the connection in my PowerShell $PROFILE to connect automatically.
We are now ready to go as soon as my prompt becomes available. Of course, we also want to know that the connection was successful, where the command lets you know if the code executed successfully or not.
We have taken several different steps for instantiating my $PROFILE. Let’s take a closer look at what the code is doing here:
- In this first line of code, we are going to import the VMware snap-in by using Add-PSSnapin, which allows us to interact with vCenter.
- In line four, we have outlined the name of my vCenter Server, which is vcenter.local (name changes as per your Infrastructure)
- In line six, we reference a credential file that can be used with a pre-stored password and matching username.
- We are 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 we are using a vCenter server that’s not part of the production domain, we 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 we need to manage multiple vCenters with different credentials. By using this method, we can store them all as variables and not have to worry about having to enter your credentials each time we connect.
- A quick note about the if-else statement on line 11: In this clause, we 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 LAB Successfully.” If the connection does not work, then you’ll be prompted by a warning.