Link to home
Start Free TrialLog in
Avatar of heze54
heze54Flag for Spain

asked on

Vmware script

HI,

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?

regards
Avatar of SubSun
SubSun
Flag of India image

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..
https://dmitrysotnikov.wordpress.com/2011/08/22/compile-powershell-scripts-to-exe/
Avatar of heze54

ASKER

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.?

Regards
ASKER CERTIFIED SOLUTION
Avatar of SubSun
SubSun
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of heze54

ASKER

A++