Looking to get two computer names from one variable using Get-ADComputer

by Mar 7, 2012

I am a beginner and finding it fun to use PowerShell in routine task.

Once a month our End User Services sends out a report of retired workstations.  From this report, we remove the workstations from our Active Directroy.  I have been able to create a simple PS script that takes the list of workstations (in csv form) and checks to see if they exist and then removes any that it needs too.  So far so good…

Some of these systems may run a dual boot and therefore have more than one entry in AD.

Example:  "Workstation" is a computer object in the Computer Container.  The need was there to create a dual boot system.  The second OS was joined to the domain and therefore the new object name became "WorkstationVM".  Now Workstation has been retired and the report shows that Workstation was removed from service.  End User Services scans a barcode on the system to mark it as removed.  This doesn't show that there were "two" systems on the one Workstation.

When I run my script, I successfully identify and remove "Workstation" and if I go back and check the Computer Container in AD, I see that I have not identified nor removed "WorkstationVM"

Since I am passing the computer object as a variable, how can I also have it detect that variable with something attached to the end like "VM"?

Script:

$idirectory = "C:POWERSHELLINPUTFILES"
$ldirectory = "C:POWERSHELLLOGS"
$outputFAIL = "SystemsNotFoundXXXX.txt"
$outputPASS = "SystemsDeletedXXXX.txt"
$input = "systemsdeleteXXXX.csv"

write "Failure – Object Not Found" | out-file $ldirectory$outputFAIL
write "Systems Deleted" | out-file $ldirectory$outputPASS

Import-module ActiveDirectory
Import-CSV $idirectory$input | % {

$system = $_.system

$computer = Get-ADComputer -Filter {name -eq $system}

&{trap {write $system | out-file $ldirectory$outputFAIL -append;continue}

write $system | out-file $ldirectory$outputPASS -append

<#– Remove-ADComputer $computer -Confirm:$false –#>

}}