Vmware script

Posted on 2015-01-08
Last Modified: 2015-01-31

I´m building a vmware script to shutdown / power on virtual machines listed in a csv file.

After exported them into that csv file, I´ve a  code like this:

$vmservers=Get-VM | Where-Object {$_.powerstate -eq ‘PoweredOn’}

$vmservers | select Name | export-csv c:\MyScripts\servers.csv -NoTypeInformation

$vmservers| Shutdown-VMGuest

Open in new window

Then, how can I build a file and run it with a double click?

Question by:heze54
  • 2
  • 2
LVL 40

Expert Comment

ID: 40538155
To run the PowerShell script with double click, you can create a bat file..

#To add VMware snapin
Add-pssnapin VMware*

$vmservers=Get-VM | Where-Object {$_.powerstate -eq ‘PoweredOn’}

$vmservers | select Name | export-csv c:\MyScripts\servers.csv -NoTypeInformation

$vmservers| Shutdown-VMGuest

Open in new window

You can save the code in to a TestScript.ps1 (any name of your choice).

Then create a bat file with following command in it.. you need to replace the D:\script\TestScript.ps1 with your script path..
powershell -command "& 'D:\script\TestScript.ps1'"

Open in new window

You can also Compile a script and make .exe file.. Following article have an example..

Author Comment

ID: 40553895
Hi Subsun,

Great post!!!

Rigth now  I´ve the csv file and want to power off virtual machines listed by csv file.. For example:

file1.csv vm1  and file2.csv vm2

I´d like to power off file1 csv machines and, after x minutes, power off file2.csv.

And last question... could I power off and esxi server runing this same script.?

LVL 40

Accepted Solution

Subsun earned 500 total points
ID: 40562342
You can have a csv input to shut down the VM's,
For example.. if the
file1.csv and file2.csv is in following format..

Open in new window

You can try following code..
#To add VMware snapin
Add-pssnapin VMware*

$vmservers= Import-csv file1.csv | %{Get-VM $_.VMName | Where-Object {$_.powerstate -eq ‘PoweredOn’}}

$vmservers| Shutdown-VMGuest

#wait 10 minutes
Start-Sleep -Seconds 600

$vmservers= Import-csv file2.csv | %{Get-VM $_.VMName | Where-Object {$_.powerstate -eq ‘PoweredOn’}}

$vmservers| Shutdown-VMGuest

Open in new window

Shut down the esx you need a different command, I think you can use Stop-VMHost to power off the esx server..
Ref :

Author Closing Comment

ID: 40581634

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Computers reporting Windows patches applied 14 74
VM keeps being unresponsive and powerring off 4 57
Scripts to List File modified in the past week 10 31
Vsphere 6 Lab Setup 4 57
Utilizing an array to gracefully append to a list of EmailAddresses
In this article, I will show you HOW TO: Perform a Physical to Virtual (P2V) Conversion the easy way from a computer backup (image).
Teach the user how to join ESXi hosts to Active Directory domains Open vSphere Client: Join ESXi host to AD domain: Verify ESXi computer account in AD: Configure permissions for domain user in ESXi: Test domain user login to ESXi host:
Advanced tutorial on how to run the esxtop command to capture a batch file in csv format in order to export the file and use it for performance analysis. He demonstrates how to download the file using a vSphere web client (or vSphere client) and exp…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now