Solved

Shutdown Specific Virtual Machine in a csv

Posted on 2015-02-05
5
292 Views
Last Modified: 2015-02-07
The following script works below but goes thru the entire vcenter virtual machines asking If I want to shutdown .   How would I change the Script to import a list of  Virtual machines in   servers.csv ?
import-csv plus what syntax ?
# Shutdown VM
$vmservers=Get-VM | Where-Object {$_.powerstate -eq ‘PoweredOn’}

$vmservers | select Name | export-csv C:\pscripts\list\servers.csv -NoTypeInformation

$vmservers| Shutdown-VMGuest
0
Comment
Question by:newbie2239
  • 3
  • 2
5 Comments
 
LVL 35

Expert Comment

by:Mahesh
ID: 40593129
I have modified your code little bit to add VM guest shutdown without prompt

Also I have added code to import csv and shutdown VM Guests if powered on without prompt

# Shutdown VM
 $vmservers=Get-VM | Where-Object {$_.powerstate -eq ‘PoweredOn’}
 $vmservers | select Name | export-csv C:\pscripts\list\servers.csv -NoTypeInformation
 $vmservers| Shutdown-VMGuest -Confirm:$false

OR

If you have list of all machines on a host, you can find out Powered on VMs and shut them down

$VMList = Import-CSV -Path C:\pscripts\list\servers.csv
foreach ($vm in $VMList)
{ Get-VM | Where-Object {$_.powerstate -eq ‘PoweredOn’} | Shutdown-VMGuest -Confirm:$false}

Open in new window


Both code should work, Do let me know if you are looking for something else
0
 

Author Comment

by:newbie2239
ID: 40594375
Actually, yes I am looking for something else. I only want to Shutdown Virtual machines in my .csv  file .

For example If I  have 500 Virtual machines  in my environment and only want to shutdown 25 of those  virtual machines  .

The Script should call my .csv file  ( import-csv)  that has the list of my 25 server names .
0
 
LVL 35

Accepted Solution

by:
Mahesh earned 500 total points
ID: 40594574
In that case either you need to prepare csv file in advance and then you can call PowerShell to shut down those VMs
$VMList = Import-CSV -Path C:\pscripts\list\servers.csv
foreach ($vm in $VMList)
{ Get-VM $vm.Name| Shutdown-VMGuest -Confirm:$false}

This simple code will turn off all machines in CSV file, you need to prepare csv file in advance

Open in new window

0
 

Author Closing Comment

by:newbie2239
ID: 40594665
Thanks ! That is what I am looking for .  By the if we were to add a verbose  which line would we place it ?

   Write-Verbose "  Shutdown Virtual Machines in CSV"
0
 
LVL 35

Expert Comment

by:Mahesh
ID: 40595473
In that case script need to be modified further as below

$i = 0
$VMList = Import-CSV -Path C:\pscripts\list\servers.csv
foreach ($vm in $VMList)
{ Get-VM $vm.Name| Shutdown-VMGuest -Confirm:$false
$i++
Write-Progress -Activity "Stopping Virtual Machines" -Status "stopped: $i of $($VMList.count)"
}

Open in new window


Check how it works
0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

#Citrix #XenApp #Citrix Scout #Citrix Insight Services #Microsoft VMMAP #Microsoft ADEXPLORE #Microsoft RAMMAP #Microsoft TCPVIEW #Microsoft AUTORUNS #Microsoft PROCESS EXPLORER #Microsoft PROCESS MONITOR
This is similar to the error described in my previous Article, but with a different source problem and a different solution. When trying to scan and stage the ESXi 6.0 updates using VMware Update Manager, we can get "error code 90"
How to install and configure Citrix XenApp 6.5 - Part 1. In this video tutorial we have explained step by step installation of Citrix XenApp 6.5 Server on Windows Server 2008 R2 is explained in this video. We have explained the difference between…
In this video tutorial I show you the main steps to install and configure  a VMware ESXi6.0 server. The video has my comments as text on the screen and you can pause anytime when needed. Hope this will be helpful. Verify that your hardware and BIO…

706 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

17 Experts available now in Live!

Get 1:1 Help Now