Quicktip: Powershell script to enable all computers on a domain

This is a very specific script I had to write, so it probably won’t ever apply to you in any situation, but it had some cool stuff in it so I thought I’d post it.

For a DR exercise I had to write a script that would go through the entire AD domain and enable any computer object that was in the input CSV file. It’s a long story for why this was necessary, but suffice it to say that part of our deployment exercise would randomly disable some computer objects (mainly Windows 2003 servers).

Most of the script is error-checking and doing some handling around if it couldn’t find the computer object at all, and then if the object was already enabled we didn’t want it to touch it at all. The real meat is in the line:

 set-adcomputer $combobj.name -enabled $true

Essentially what this line does is read the array pulled in from the CSV and set it to enabled. It’s actually a super easy command that took me a while to find. But here it is for you!

The rest of the code:

import-module activedirectory
$erroractionpreference = "SilentlyContinue"
$computerobjects = import-csv c:\file.csv


foreach ($compobj in $computerobjects){
                $adcompobj = $null
                $adcompobj = get-adcomputer $compobj.name
                if ($adcompobj) {
                                if (!$adcompobj.enabled) {
                                                set-adcomputer $compobj.name -enabled $true
                                                write-host $compobj.name " was disabled. Set to enabled." -foregroundcolor yellow
                                }
                                else {
                                                write-host $compobj.name " was already enabled. Ignoring." -foregroundcolor green
                                }              
                }
                else {
                                write-host "There was an error connecting to " $compobj.name " or it doesn't exist on the domain." -foregroundcolor red
                }
}
Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s