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.”
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.
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:
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.