So, you want to use PowerCLI to do some things with VMware vSphere and you are just getting started. Maybe it’s to run some basic cmdlets and poke around vSphere, for maybe you want to search for attached ISO’s on VM’s, or maybe it’s to run a script to count cores and TiBs for VCF like in William Lam’s blog article. Whatever it is you want to do, this is a great place to start.
Let’s begin with installation. But first…check out the Official VMware Developer Documentation which will provide a guide on any OS (Windows, MAC OIS or Linux) for both online and offline install, plus there’s a handy link for cmdlet reference.
Also, check out my GitHub for a small collection of PowerCLI Cmdlets, most of which will be used and referenced below.
Ok let’s get to it. This will cover the Windows flavor of installation, if you have Linux or MacOS, my apologies…this blog article wont’ cover the install on those OS’s, but take a look at the official documentation link for help.
First, be sure to open PowerShell as Administrator – then run the cmdlet:
Install-Module VMware.PowerCLI -Scope CurrentUser
Note, this will install PowerCLI as the current Windows user – if this install is wanted for all users of the PC, then remove the -Scope CurrentUser parameter.
Also, there may be a prompt to accept and install modules from an untrusted repository. In this case I trust PSGallery repo, so I will type in “A” for “Yes to All”:
PowerShell will then install the PowerCLI packages and progress will be displayed:
And that’s it, PowerCLI is now installed.
Let’s run a few scripts to verify what was installed. This cmdlet can also be used to see what other modules have been installed.
Get-PowerCLIVersion
( this is deprecated but will still run as of PowerCLI 13.2.1)
or the more recent version:
Get-Module -Name VMware.* | Select-Object -Property Name,Version
If it’s installed, this is what will output:
And here we can see PowerCLI 13.2.0 build 22643734 is installed and the other installed modules are also displayed.
One other extra thing to check is the the PowerShell module path. I like to just verify the module path is set to the default, and not to some other path, like a user directory or something else. Run the following:
$env:PSModulePath
I see the default directories/paths…looks good to me.
Also, after PowerCLI is installed, choose to participate in the VMware Customer Experience Improvement Program (CIEP). Set the CEIP to true (or false, your choice) – I selected “A” for “Yes to All” in my example below:
Set-PowerCLIConfiguration -Scope User -ParticipateInCEIP $true
One other item I have run across is the PS Execution Policy, which will give errors if there are attempts to run local scripts. The history here is that for security reasons, PowerShell supports an execution policy feature – by default, the execution policy is set to Restricted, which is the most secure policy.
If running local scripts, you may run into an error like this:
File C:\GetVMDeviceHotplug.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.
If this is the case, then it’s possible to change the execution policy by using the Set-ExecutionPolicy cmdlet.
I do have local scripts I plan in running, so I set policy to this to bypass. I also set the -Scope to CurrentUser so others are not affected by the config change. I recommend to change this per you organizations security policy.
Also, for more information about the execution policy and script digital signing in PowerShell, run Get-Help About_Signing
(Note: this will prompt to run Update-Help).
Set-ExecutionPolicy Bypass -Scope CurrentUser
And, if PowerCLI is already installed, but the module(s) is/are stale, it may be needed to update PowerCLI. Simply run the update cmdlet:
Update-Module -Name VMware.PowerCLI
Ok that’s all for the installation! Now that PowerCLI is installed, it’s possible to run the cmdlets and manage vSphere. Let’s give it a try.
First, I need to connect to a vCenter server:
Connect-VIServer -Server <yourserverIPorFQDN>
A credential window will open, enter vSphere/vCenter credentials:
Once connected successfully, the connection will be verified in the PS/PCLI window:
Let’s try an easy cmdlet…let’s see how many hosts are connected and get some details:
Get-VMHost
Nice!! How about another cmdlet to see all my VM’s…
Get-VM
Sweet! There are a ton of cmdlets that are super powerful and useful. There’s lots to do there and there’s all sorts of ways to manage/export data as well…so more to come on that.
One last item is to disconnect from my vCenter – I see this alot where there are connected PowerCLI sessions, and it’s always best practice to disconnect when finished.
There’s a couple of ways to do this, one is to disconnect from the single server- sub in your hostname below
Disconnect-VIServer -Server <yourserverIPorFQDN>
Or disconnect from all vCenter server(s) – this will force ALL active connections to disconnect:
Disconnect-VIServer -Force
Ok so that’s it for now…start there with PowerCLI and let me know if helped you by commenting below!
Leave a Reply