[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Vmware script

Posted on 2015-01-08
4
Medium Priority
?
186 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 40

Expert Comment

by:Subsun
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..
https://dmitrysotnikov.wordpress.com/2011/08/22/compile-powershell-scripts-to-exe/
0
 

Author Comment

by:heze54
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.?

Regards
0
 
LVL 40

Accepted Solution

by:
Subsun earned 2000 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..
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
ID: 40581634
A++
0

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

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

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
This Micro Tutorial steps you through the configuration steps to configure your ESXi host Management Network settings and test the management network, ensure the host is recognized by the DNS Server, configure a new password, and the troubleshooting…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

650 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