PowerShell

Restart all Computers in a specific OU

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.

Unbenannt.PNG

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.

8 replies »

  1. 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

    Like

    • 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

      Like

    • 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!

      Liked 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?

        Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.