VMware Cloud Community
kwharrisit
Contributor
Contributor
Jump to solution

Restart machines listed in folder

I have groups of VM's in various folders that need to have an orderly reboot. Is it possible to use the PowerCLI to reboot all of the machines in a particular folder structure? for example something that would ask which folder you want, then resturn the list of machines in the fodlers ( and subfolders) and then restart them once you acknowledge the list?

0 Kudos
1 Solution

Accepted Solutions
AntonVZhbankov
Immortal
Immortal
Jump to solution

kwharrisit, if you consider any comment as helpful please award points.


---

MCITP: SA, MCTS Hyper-V, VCP 3/4, VMware vExpert

http://blog.vadmin.ru

EMCCAe, HPE ASE, MCITP: SA+VA, VCP 3/4/5, VMware vExpert XO (14 stars)
VMUG Russia Leader
http://t.me/beerpanda

View solution in original post

0 Kudos
7 Replies
AntonVZhbankov
Immortal
Immortal
Jump to solution

This script will restart all VMs from folder "Root Folder \ Subfolder 1 \ Subfolder 2" and all its subfolders

Get-Folder "Root Folder" | Get-Folder "Subfolder 1" | Get-Folder "Subfolder 2" | Get-VM | Where-Object {$_.PowerState -eq "PoweredOff"} | Restart-VMGuest


---

MCITP: SA, MCTS Hyper-V, VCP 3/4, VMware vExpert

http://blog.vadmin.ru

EMCCAe, HPE ASE, MCITP: SA+VA, VCP 3/4/5, VMware vExpert XO (14 stars)
VMUG Russia Leader
http://t.me/beerpanda
0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

The next script will prompt you for a foldername, list all the virtual machines in the folder and subfolders and will ask if you want to restart all the VM's in the list. If you answer yes, all the PoweredOn VM's in the list will be restarted:

$FolderName = Read-Host -Prompt "Type the foldername containing the VM's you want to restart"
$VMs = Get-Folder $FolderName | Get-VM
""
"This is the list of VM's in folder $FolderName"
$VMs
""
$Answer = Read-Host -Prompt "Are you sure you want to restart all the VM's in the list? (Y/[N])"
if ($Answer -eq "Y") {
  ""
  "Rebooting all the VM's"
  $VMs | Where-Object {$_.PowerState -eq "PoweredOn"} | Restart-VMGuest
}

Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
AntonVZhbankov
Immortal
Immortal
Jump to solution

Robert, your example will work good only if there unique folder names. But if there several subfolders with the same name then your script will return VMs from all the folders with specified name and restart all the VMs from there.

Live example from my installation - I have Test cluster and Production cluster, separated physically and logically. Access to production VMs is very limited, while whole Test folder is open for several people. And there are Infrastructure folders for DNS, file servers etc in both Test and Production folders = clusters.

Let's suppose I want to reboot all my test infrastructure and I forgot that I'm currently working under superuser. Whole infrastructure including production will go down.


---

MCITP: SA, MCTS Hyper-V, VCP 3/4, VMware vExpert

http://blog.vadmin.ru

EMCCAe, HPE ASE, MCITP: SA+VA, VCP 3/4/5, VMware vExpert XO (14 stars)
VMUG Russia Leader
http://t.me/beerpanda
0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

Hi Anton,

I know that my previous script will only work correct if you have unique folder names. I think that the Get-Folder cmdlet lacks the possibility to specify a folder path. You should be able to do something like Get-Folder "/Amsterdam/sales". And that should not give you the vm's in "/Moscow/sales". I will try to solve this problem with a new function Get-VMFolderPath. With this function I enhanced my script so that you now have to type a folder path like /Amsterdam/sales:

function Get-VMFolderPath {
  param([string] $Path)
  
  $Folder = Get-Folder -Name vm
  $FolderNames = $Path.TrimStart("/").TrimEnd("/").Split("/")
  $i = 0
  while ($i -lt $Foldernames.Length -and $Folder) {
    $Folder = Get-Folder -Name $FolderNames[$i++] -Location $Folder -NoRecursion
  }
  $Folder
}

$FolderPath = Read-Host -Prompt "Type the path to the folder containing the VM's you want to restart (e.g. /Amsterdam/sales)"
$Folder = Get-VMFolderPath $FolderPath
If ($Folder) {
  $VMs = $Folder | Get-VM
  ""
  "This is the list of VM's in folder $FolderPath"
  $VMs
  ""
  $Answer = Read-Host -Prompt "Are you sure you want to restart all the VM's in the list? (Y/[N])"
  if ($Answer -eq "Y") {
    ""
    "Rebooting all the VM's"
    $VMs | Where-Object {$_.PowerState -eq "PoweredOn"} | Restart-VMGuest
  }
}
else {
  Write-Error "Folder $FolderPath not found"
}

Regards, Robert

Message was edited by: RvdNieuwendijk

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
BrianGordon84
Contributor
Contributor
Jump to solution

$folder_name = Read-Host "Enter folder name:"

$VMs = Get-VM -Location $folder_name

foreach($vm in $vms){Restart-VM -VM $vm -Confirm:$true -RunAsync}

Do you have unique folder names anywhere? Like, Test and Production and then the same folders under those? Isn't it confusing to use the same folder names?

0 Kudos
AntonVZhbankov
Immortal
Immortal
Jump to solution

>Like, Test and Production and then the same folders under those? Isn't it confusing to use the same folder names?

No.

For ex. I have such folders:

/Production/Billing

/Production/Infrastructure

/Production/Billing

/Production/Templates

/Production/Test

/Test/Billing

/Test/Infrastructure

/Test/Test

/Test/Templates

/Office/Templates

/Office/Test

What is confusing you in such foder structure?


---

MCITP: SA, MCTS Hyper-V, VCP 3/4, VMware vExpert

http://blog.vadmin.ru

EMCCAe, HPE ASE, MCITP: SA+VA, VCP 3/4/5, VMware vExpert XO (14 stars)
VMUG Russia Leader
http://t.me/beerpanda
0 Kudos
AntonVZhbankov
Immortal
Immortal
Jump to solution

kwharrisit, if you consider any comment as helpful please award points.


---

MCITP: SA, MCTS Hyper-V, VCP 3/4, VMware vExpert

http://blog.vadmin.ru

EMCCAe, HPE ASE, MCITP: SA+VA, VCP 3/4/5, VMware vExpert XO (14 stars)
VMUG Russia Leader
http://t.me/beerpanda
0 Kudos