{"id":1889,"date":"2013-05-19T15:15:53","date_gmt":"2013-05-19T15:15:53","guid":{"rendered":"https:\/\/pipe2text.com\/?page_id=1889"},"modified":"2014-02-01T21:36:28","modified_gmt":"2014-02-01T21:36:28","slug":"using-powershell-to-shut-down-multiple-vmware-guests-operating-systems","status":"publish","type":"page","link":"https:\/\/pipe2text.com\/?page_id=1889","title":{"rendered":"Using PowerShell to Gracefully Shut Down Multiple Virtual Machines in VMware"},"content":{"rendered":"<p>Hi All,<\/p>\n<p>There may be times when you have the need to shut down multiple VMware Guest such as when you need to perform maintenance. Rather than go and visit each virtual machine individually, you can use the following few lines to shut down the guest operating systems gracefully as long as VMware Tools is installed. To do anything with PowerShell in VMware you will need to install \u201cVMware vSphere PowerCLI\u201d. You will then need to open PowerCLI connect to your VMware vCenter server by typing \u201cConnect-VIServer <em>vCenterServerName<\/em>\u201d.<\/p>\n<p>To shutdown all VM guest managed by vCenter you can type the following 3 lines:<\/p>\n<p>$vmservers=Get-VM | Where-Object {$_.powerstate -eq &#8216;PoweredOn&#8217;}<\/p>\n<p>$vmservers | select Name | export-csv c:\\MyScripts\\servers.csv -NoTypeInformation<\/p>\n<p>$vmservers| Shutdown-VMGuest<\/p>\n<p>The script above will do 3 things. The first line will get all VMs that are currently powered on. The second line will log all the powered on VMs in line 1 and log them to a csv file servers.csv located in c:\\MyScripts. The reason I log these machine names to a csv file is so when it\u2019s time to turn the machines back on, I don\u2019t inadvertently turn on any VMs that were turned off for whatever reason before I began the shutdown. The third\u00a0line will shutdown the Vmware guests. The\u00a0third line will ask you for confirmation before doing anything. If you don\u2019t want to be asked for confirmation, you can simply add \u201c-Confirm:$false\u201d as shown below:<\/p>\n<p>$vmservers | Shutdown-VMGuest -Confirm:$false<\/p>\n<p>Now let\u2019s say you only want to shut down only a subset of machines. If you have your VMs organized by folders in vCenter this will be easy. All you need to do is add the location parameter to the first line\u00a0with the name of the\u00a0folder. In the example below, I&#8217;ve changed the\u00a0first line to get all of the VMs located in the \u201cMy Lab\u201d folder.<\/p>\n<p>$vmservers=get-vm -location &#8220;My Lab&#8221; | Where-Object {$_.powerstate -eq &#8216;PoweredOn&#8217;}<\/p>\n<p>So basically, if you wanted to run a shutdown on all VMware guest in the \u201cMy Lab\u201d folder only and not receive any confirmation before the shutdown begins, you would run the 3 lines as shown below.<\/p>\n<p>$vmservers=get-vm -location &#8220;My Lab&#8221; | Where-Object {$_.powerstate -eq &#8216;PoweredOn&#8217;}<\/p>\n<p>$vmservers |select Name | export-csv c:\\MyScripts\\servers.csv -NoTypeInformation<\/p>\n<p>$vmservers | Shutdown-VMGuest -Confirm:$false<\/p>\n<p>If you needed a way to bring all the VMware guest that you shutdown, you could use the server.csv file created previously using the following 2 lines:<\/p>\n<p><strong>IMPORTANT <\/strong><strong>NOTE: You need to be careful with this. Depending on your environment, bringing up too many VMs at one time could wreak havoc on your storage or on your environment in general. You would be best off only limiting the amount of servers in the <\/strong><strong>csv<\/strong><strong> file to just a few so you don\u2019t have any problems.<\/strong><\/p>\n<p>$servers = import-csv C:\\MyScripts\\servers.csv | select -ExpandProperty name<\/p>\n<p>Start-VM -VM $servers<\/p>\n<p>I hope this helps. This will give you a quick and easy way to shutdown multiple virtual machines. If you have any questions or feedback, please leave a comment.<\/p>\n<p>Related Links:<\/p>\n<p><strong><a title=\"Using PowerShell to get a list Virtual Machine Snapshots in VMware ESXi 4.1\" href=\"https:\/\/pipe2text.com\/?page_id=251\">Using PowerShell to get a list Virtual Machine Snapshots in VMware ESXi 4.1<\/a><\/strong><\/p>\n<p><a title=\"Using PowerShell to Remove Virtual Machine Snapshots in VMware ESXi 4.1\" href=\"https:\/\/pipe2text.com\/?page_id=1953\"><b>Using PowerShell to Remove Virtual Machine Snapshots in VMware ESXi 4.1<\/b><\/a><\/p>\n<p><strong><a title=\"Using PowerShell to create a Virtual Machine Inventory in VMware and Export it to a CSV File\" href=\"https:\/\/pipe2text.com\/?page_id=2018\">Using PowerShell to create a Virtual Machine Inventory in VMware and Export it to a CSV File<\/a><strong><\/strong><\/strong><\/p>\n<p><strong><strong><a title=\"Using PowerShell to View  Virtual Machine Disks (VMDK) Information in VMware and Export to a CSV File\" href=\"https:\/\/pipe2text.com\/?page_id=2635\">Using PowerShell to View\u00a0 Virtual Machine Disks (VMDK) Information in VMware and Export to a CSV File<\/a><\/strong><\/strong><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi All, There may be times when you have the need to shut down multiple VMware Guest such as when you need to perform maintenance. Rather than go and visit each virtual machine individually, you can use the following few &hellip; <a href=\"https:\/\/pipe2text.com\/?page_id=1889\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"open","template":"","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","footnotes":""},"class_list":["post-1889","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/pipe2text.com\/index.php?rest_route=\/wp\/v2\/pages\/1889","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pipe2text.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/pipe2text.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/pipe2text.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/pipe2text.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1889"}],"version-history":[{"count":17,"href":"https:\/\/pipe2text.com\/index.php?rest_route=\/wp\/v2\/pages\/1889\/revisions"}],"predecessor-version":[{"id":2652,"href":"https:\/\/pipe2text.com\/index.php?rest_route=\/wp\/v2\/pages\/1889\/revisions\/2652"}],"wp:attachment":[{"href":"https:\/\/pipe2text.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1889"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}