Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Shutdown Specific Virtual Machine in a csv

Posted on 2015-02-05
5
Medium Priority
?
668 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 2000 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

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.
A look into Log Analysis and Effective Critical Alerting.
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…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…

722 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