Solved

Shutdown Specific Virtual Machine in a csv

Posted on 2015-02-05
5
338 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

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

Create your own, high-performance VM backup appliance by installing NAKIVO Backup & Replication directly onto a Synology NAS!
If your vDisk VHD file gets deleted from the image store accidentally or on purpose, you won't be able to remove the vDisk from the PVS console. There is a known workaround that is solid.
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…

910 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

22 Experts available now in Live!

Get 1:1 Help Now