Solved

Vmware script

Posted on 2015-01-08
4
170 Views
Last Modified: 2015-01-31
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
0
Comment
Question by:heze54
  • 2
  • 2
4 Comments
 
LVL 40

Expert Comment

by:Subsun
Comment Utility
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/
0
 

Author Comment

by:heze54
Comment Utility
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
0
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
Comment Utility
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..
VMName
VM1
VM2
VM3

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 : https://www.vmware.com/support/developer/windowstoolkit/wintk40u1/html/Stop-VMHost.html
0
 

Author Closing Comment

by:heze54
Comment Utility
A++
0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Suggested Solutions

When we have a dead host and we lose all connections to the ESXi, and we need to find a way to move all VMs from that dead ESXi host.
This article will help you understand what HashTables are and how to use them in PowerShell.
Teach the user how to convert virtaul disk file formats and how to rename virtual machine files on datastores. Open vSphere Web Client: Review VM disk settings: Migrate VM to new datastore with a thick provisioned (lazy zeroed) disk format: Rename a…
This Micro Tutorial walks you through using a remote console to access a server and install ESXi 5.1. This example is showing remote access and installation using a Dell server. The hypervisor is the very first component of your virtual infrastructu…

763 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

6 Experts available now in Live!

Get 1:1 Help Now