Solved

Vmware script

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
Teach the user how to configure vSphere clusters to support the VMware FT feature Open vSphere Web Client: Verify vSphere HA is enabled: Verify netowrking for vMotion and FT Logging is in place or create it: Turn On FT for a virtual machine: Verify …
Teach the user how to use create log bundles for vCenter Server or ESXi hosts Open vSphere Web Client: Generate vCenter Server and ESXi host log bundle:  Open vCenter Server Appliance Web Management interface and generate log bundle: Open vCenter Se…

734 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