Solved

Shutdown Specific Virtual Machine in a csv

Posted on 2015-02-05
5
544 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
[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
  • 3
  • 2
5 Comments
 
LVL 37

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 37

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 37

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Last article we focus in how to VMware: How to create and use VMs TAGs – Part 1 so before follow this article and perform the next tasks, you should read the first article how to create the TAG before using them in Veeam Backup Jobs.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
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…

623 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