I am a stickler about computer names. I love consistent and logical naming! For me, a name should easily describe the location or purpose of the computer and should not require regular updating. For example, the computer name BOE-HR01 tells me three things:
- The computer is in the Board Office building and OU
- The computer is a member of the Human Resources department
- The computer belongs to the HR Director because it ends in 01.
Not every computer is named this logically. Our Board Office has about 60 computers that have been setup by a variety of people. Some computers are named after the model (BOEE5400N03) and some are named after the person (BOESMITHPC).
Unfortunately, people change and models get updated. To address these problems, I needed a script that would show me missing computer names and would list the next available name in a logical naming convention. Here is that script:
$SearchComputers = read-host "What is the computer prefix?" $PrefixCount = $SearchComputers | Measure-Object -Character | Select-Object -ExpandProperty characters $Computers = Get-QADComputer *$SearchComputers* | Select-Object Name | Sort-Object Name foreach ($Computer in $Computers) $Computer = $Computer.Name [int]$CurrentNumber = $Computer.Substring($PrefixCount) if ($CurrentNumber.CompareTo($PreviousNumber+1).Equals( 1 ) ) { $MissingNumber = $PreviousNumber+1 Write-host "Missing Computer: $SearchComputers$MissingNumber" } [int]$PreviousNumber = $CurrentNumber }
This script starts, like most of my scripts, by asking for a user input. If you had a computer named BOE-HR01 and you wanted to find every missing computer/available computer name in that prefix, you would type BOE-HR.
Line 2 is where things get cool! The variable $PrefixCount looks at your user input and then measures the input by using the Measure-Object cmdlet. Finally, it uses the Select-Object cmdlet with the -ExpandProperty switch. You probably have seen scripts do something like this:
$PrefixCount = $SearchComputers | Measure-Object -Character | Select-Object characters $PrefixCount = $PrefixCount.Characters
This is done to get the value of the $PrefixCount.Characters member (which would be “6” in this case). By using the expand-characters switch, you can eliminate this extra line of code! I learned this trick in the Piping chapter of Learn Windows PowerShell 3 in a Month of Lunches.
Now that we have our list of computers, we need to a way to find a missing number. This is done with a few lines. First, we create the $CurrentNumber variable. This is the last two digits of our computer name (ex: 01, 07, 11, etc).
Next, we compare our CurrentNumber with the PreviousNumber by using .CompareTo. If our CurrentNumber (let’s say N05) is compared to our PreviousNumber (which is N04) + 1, then .CompareTo returns a Zero (equal) value.
If our CurrentNumber returns a higher value, our If statement will trigger and will report a missing computer. Here is a screenshot showing you the power of this script!
Open up Active Directory Users and Computers. Navigate to a group of computers and create a fake computer at the end. If your last computer name ends with N05, create a computer named N07. This is to simulate a missing computer (N06). \
Paste the script above into PowerShell and enter your Prefix. You should see your missing computer! If you don’t make sure that you have the Quest AD cmdlets loaded.
So the next time you are restructuring Active Directory or renaming your computers, let this script make your life a little easier!