powershell need to pipe hash values to command that disables the computers

Goal: Compare 2 lists of computers pulled from 2 different domains.  Find the duplcates and pipe them to be disabled in one of the domains.

Problem: When I find the duplicates, they are not simply a computer name, they are like this.. "@{Name=computername}".  

Any help is greatly appreciated, I am getting brain fried.

$Properties = @(
	"name"
	"Operatingsystem"
    )

$Select = @(
	@{n="Name";e={$_."name"}}
	#@{n="OperatingSystem";e={$_."OperatingSystem"}}
    )
    
echo "`nSearching for Domain1 Windows 7 workstations"
$Complist1= get-adcomputer -filter * -Properties $properties -SearchBase "DC=domain,DC=com" -server "Server"|
Where {$_.OperatingSystem -like '*windows 7*'} | Select name
echo "`nSearching for Domain2 Windows 7 workstations"
$Complist2 = get-adcomputer -filter * -Properties $properties -SearchBase "DC=domain2,DC=com" -server "server2"|
Where {$_.OperatingSystem -like '*windows 7*'} | Select $select

$CombinedList = $Complist1+ $Complist2

echo "`nComparing the Domain1 list to Domain2, if a duplicate is found the domain2 workstation will be disabled."

#i need help finding the duplicates in the array AND being able to use them using the names to disable them in Domain2.
#Below i found online however, the output is in this format and I am at a loss on how to simply get a clean computer name.

#Output: "@{Name=computername}"
#I need the computername only so I can pipe it into another command to disable the computers.
$ht = @{}
$CombinedList | foreach {$ht["$_"] += 1}
$ht.keys | where {$ht["$_"] -gt 1} | foreach { write-host $_} #Here instead of the write-host, I want to pipe this to a command to disable the computers

Your help is apprecia		
July 22nd, 2015 3:56pm

I have used 'replace' to work around this problem before however, I would like to avoid replacing the unwanted text and just get the computer name alone.

Free Windows Admin Tool Kit Click here and download it now
July 22nd, 2015 5:52pm

Access the name by using $ht.name although I think you may need the SamAccountname to disable the account so get this initially:

$Complist1= get-adcomputer -filter * -Properties $properties -SearchBase "DC=domain,DC=com" -server "Server"| Where {$_.OperatingSystem -like '*windows 7*'} | Select name, SamAccountname

$Complist2 = get-adcomputer -filter * -Properties $properties -SearchBase "DC=domain2,DC=com" -server "server2"| Where {$_.OperatingSystem -like '*windows 7*'} | Select name, SamAccountname

 And then once you have your 2 lists compare them using:

Foreach ($Computer in $Complist2)
{
    If ($Complist1.Name.Contains($Computer.Name))
    {
        Write-Output "$($Computer.Name) also exists in list 1"
        Disable-ADAccount -Identity $Computer.SamAccountname -Server 'server2' -Confirm -WhatIf
    }
    Else {Write-Output "$($Computer.Name) is unique "}
}

Does that work?

July 23rd, 2015 9:27am

@Flagmanchris

Thank you for your post, it pointed me in the right direction on what to do with the lists.  See below, it works perfect now.  


foreach ($computer in $Domain1) 
{
    if ($CCwrks.name.Contains($computer.name))
    {
        $record."computer" = $computer.name
        $objRecord = New-Object PSObject -property $Record
        $mastertable += $objRecord
        
    }
    else{}
    
}
Write-host `n $Mastertable.count + " duplicate computers found`n"
if ($Mastertable.Count -gt 0){
            Foreach ($dup in $Mastertable)
            {
                $Status = get-adcomputer -filter * -Properties $properties -SearchBase "DC=Domain1,DC=com" -server "DC1" | where {$_.Name -eq $dup.Computer} |
                Select-Object name, Enabled }

These lines got me where I needed to go.  Your advice helped greatly!

Thank yo for your help!

Free Windows Admin Tool Kit Click here and download it now
July 23rd, 2015 6:08pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics