none
powershell need to pipe hash values to command that disables the computers RRS feed

  • Question

  • 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 appreciated.

    Michael


    • Edited by Arkangel30 Wednesday, July 22, 2015 7:47 PM
    Wednesday, July 22, 2015 7:47 PM

Answers

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


    Michael

    • Marked as answer by Arkangel30 Thursday, July 23, 2015 10:07 PM
    Thursday, July 23, 2015 10:07 PM

All replies

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


    Michael

    Wednesday, July 22, 2015 9:43 PM
  • 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?

    Thursday, July 23, 2015 1:26 PM
  • @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!


    Michael

    • Marked as answer by Arkangel30 Thursday, July 23, 2015 10:07 PM
    Thursday, July 23, 2015 10:07 PM