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
Solved

Shutdown Specific Virtual Machine in a csv

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

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 36

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 36

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

In this article, I am going to show you how to simulate a multi-site Lab environment on a single Hyper-V host. I use this method successfully in my own lab to simulate three fully routed global AD Sites on a Windows 10 Hyper-V host.
Giving access to ESXi shell console is always an issue for IT departments to other Teams, or Projects. We need to find a way so that teams can use ESXTOP for their POCs, or tests without giving them the access to ESXi host shell console with a root …
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…

840 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