Solved

Vmware script

Posted on 2015-01-08
4
174 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
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 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..
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

This article will help you understand what HashTables are and how to use them in PowerShell.
In this article, I show you step by step with screenshots to assist you - HOW TO: Deploy and Install the VMware vCenter Server Appliance 6.5 (VCSA 6.5), with some helpful tips along the way.
Teach the user how to use configure the vCenter Server storage filters Open vSphere Web Client:  Navigate to vCenter Server Advanced Settings: Add the four vCenter Server storage filters: Review the advanced settings: Modify the values of the four v…
This tutorial will walk an individual through the steps necessary to enable the VMware\Hyper-V licensed feature of Backup Exec 2012. In addition, how to add a VMware server and configure a backup job. The first step is to acquire the necessary licen…

805 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