VMware VSphere Power CLI Basic Scripts (Part – 1)

Taking one step ahead from simple one liner of VSphere Power CLI. We have to save the scripts with .ps1 extension.

Note : My scripts were not running because of the execution policy I have changed that policy to unrestricted in my test lab you should be login as administrator to do that.

PS C:\Windows\system32> Set-ExecutionPolicy Unrestricted

Execution Policy Change
The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose you to the
security risks described in the about_Execution_Policies help topic. Do you want to change the execution policy?
[Y] Yes [N] No [S] Suspend [?] Help (default is “Y”): Y
PS C:\Windows\system32> Get-ExecutionPolicy


Scenario 1 :

I created on script in folder PowerShellscripts in C drive of windows machine with name vm information and save with extension of ps1

Below is the small script.

get-cluster Shiv | get-vm | select-object Name, NumCPU, VMHost | export-csv C:\output\vms.csv -Notypeinformation

PS C:\Windows\system32> & ‘C:\PowerShellscripts\vm information.ps1’

After executing that script I can see the csv file is created in output folder with name vms.csv

Scenario 2 :

After that I created on more Power Shell script to read the input from file generated in previous step.

$vms=Import-CSV C:\output\vms.csv
foreach ($vm in $vms) {
get-vm $vm.name

Below is the output after running that script.

PS C:\Windows\system32> C:\PowerShellscripts\Reading_csv.ps1

Name PowerState Num CPUs MemoryGB
—- ———- ——– ——–
ttylinux-1 PoweredOff 1 0.016
Red-Linux2 PoweredOff 1 2.000
Red-Linux1 PoweredOn 1 1.000

Scenario 3 :

Adding some spice by putting the output with some meaning full text.

$vms=Get-VMHost | get-vm
foreach ($vm in $vms) {
Write-Host “VM name is $vm.name “

PS C:\Windows\system32> C:\PowerShellscripts\vm_name.ps1
VM name is ttylinux-1.name
VM name is Red-Linux2.name
VM name is Red-Linux1.name

Scenario 4 :

While working on the above scenario I came across very useful Grid view.

PS C:\Windows\system32> get-vm | Out-GridView

It will take some to process it. Showing the snippet of Grid View we can add filter and sort the data as per our requirement.

Grid View

A lot more to come. 

Thanks for reading 🙂


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s