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?
kwharrisit, if you consider any comment as helpful please award points.
---
MCITP: SA, MCTS Hyper-V, VCP 3/4, VMware vExpert
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
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
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
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
$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?
>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
kwharrisit, if you consider any comment as helpful please award points.
---
MCITP: SA, MCTS Hyper-V, VCP 3/4, VMware vExpert