Recently I received a request to shut down computers by room. Specifically, it was about a school where students often forget to shut down their computers. Of course this is possible with PowerShell, what else?
The Goal
First we have to retrieve all organizational units. Not only the parent ones, but also the child ones. I like it user-friendly with Out-GridView.
$room=Get-ADOrganizationalUnit -LDAPFilter '(name=*)' -SearchBase 'OU=Clients,OU=Workstations,DC=sid-500,DC=com' -SearchScope OneLevel | Select-Object DistinguishedName | Out-GridView -PassThru -Title 'Select Computers OU to restart all Computers in that OU'
Note the Out-GridView command after the pipe. This gives us the possibility to select the OU in a kind of graphical interface.
After selecting the OU, click ok. All computers in that OU will be shutdown.
The Code
Note that you have to adapt line 1 with your OU structure as you see fit.
$room=Get-ADOrganizationalUnit -LDAPFilter '(name=*)' -SearchBase 'OU=Workstations,DC=sid-500,DC=com' -SearchScope OneLevel | Select-Object DistinguishedName | Out-GridView -PassThru -Title 'Select Computers OU to restart all Computers in that OU' $comp=(Get-ADComputer -SearchBase $room.distinguishedname -Filter *).Name Foreach ($c in $comp) {Restart-Computer -ComputerName $c -ErrorAction SilentlyContinue -Force -Verbose}
That’s it. Automation complete.
Categories: PowerShell, Windows 10, Windows Server
This’ll be sitting on my desktop as a kill switch if I ever wind up doing IT for a capitol building.
Easily said in retrospect, but this is a great tool for any possibly overrun public building.
LikeLike
Thank you!
LikeLike
Hi Patrick, to be honest, I’ve cannibalized your script a bit. But thank you very much for posting it!
Here is what I’ve used:
$name = Get-ADComputer -Filter * -SearchBase “OU=ComputersOU,DC=Adatum,DC=com” | Select-Object Name
Foreach ($i in $name) {Stop-Computer -ComputerName $name -Verbose}
I am getting an RPC error, exception from HRESULT: 0x800706BA
However, when I just issue Stop-Computer LON-SVR3 -Verbose, it works fine.
More information:
I can change the $name variable to only include LON-SVR3 such as:
$name = Get-ADComputer -Identity LON-SVR3 | Select-Object Name
Still doesn’t work.
I tried to feed the DN to the Stop-Computer command and I couldn’t get it to work. I thought maybe I could feed the ComputerName variable into the Stop-Computer command, but still no luck.
I haven’t tried to take the output from Get-ADComputer and fill in an array, then fill the variable into the Stop-Computer cmdlet. That seems far too complicated for what I want to do.
I would appreciate any thoughts that you’d have. I know that the RPC service and the WMI service are up and running. I have also shut off the firewalls on all of the computers. Note: I’m attempting to shut off Windows Server 2012R2 systems as a test in a lab environment for 20-412.
Thanks!
-CS
LikeLike
Hi, do you have some good reasons why Firewall is off? The Firewall is a security Feature turning it off is not recommended and not useful, Firewall has an impact on almost all os functionalities.
Use Select-Object -ExpandProperty Name to get the computername.
Let me know it it works now.
Best,
P
LikeLike
Patrick do you explain me your script. I don`t understand. Thanks
LikeLike
Hi Patrick
Adding the -ExpandProperty option worked perfectly! Thank you!
Regarding the firewall, I had shut the firewall down to ensure that it wasn’t blocking a port. If the firewall had been a problem, it would have been a (relatively) easy fix for the issue.
Thank you again for your help!
LikeLiked by 1 person
Hi Chris,
I am newbie on Powershell. Can you share with me the whole thing like where you add “Select-Object -ExpandProperty Name” Like Patrick said?
LikeLike
Patrick please help me. I must tell you that I do not understand your ahead script.Thank
LikeLike
with stop-computer you will shutdown computer, I would use restart-computer instead
LikeLike
Thank you! Was my mistake! Now fixed!
LikeLike