First two entries in basic hash table are duplicates

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Karson Van Meeteren Karson Van Meeteren 2 years ago.

  • Author
    Posts
  • #27817
    Profile photo of Karson Van Meeteren
    Karson Van Meeteren
    Participant

    I'm getting computers that are part of a security group and outputting them to an hash table. However, the first two of the three entries are duplicates, even if I "Write-Host" foreach loop to show the status of what computer the scripts is processing.

    Does anyone know if there is a gotcha I need to be handling somewhere?

    The green text is each unique computer being handled by the foreach, with the yellow being the hash table output that's in question.

    function Get-IAAMComputers
    {
    	$hash = $null
    	
    	$psObjectCollection = @()
    	Import-Module ActiveDirectory
    	$targetGroupOU = "CN=dummy,DC=dummy,DC=net"
    	$computers = Get-ADGroupMember -Identity $targetGroupOU | Where-Object { ($_.name -like "asdfasdf*") -or ($_.name -like "asdf1234*") } | Select-Object -ExpandProperty name -First 3
    	foreach ($computer in $computers)
    	{
    
    		$computerADInfo = Get-ADComputer -Identity $computer -Properties * | select name, description
    		
    		#if ($computer.description -eq $null)
    		#{
    		$connectionTest = Test-Connection $computer -Quiet
    		if ($connectionTest -ne $false)
    		{
    			$folderList = Get-Childitem -Path \\$computer\c$\Users -Directory
    			if ($folderList -ne $null)
    			{
    				$lastUser = $folderList | Sort-Object LastWriteTime | Select-Object -Last 1
    				$userInfo = Get-ADUser $lastUser.name -Properties * | select name, officephone
    				$hash = @{
    					ComputerName = $computerADInfo.Name
    					ComputerDescription = $computerADInfo.Description
    					LastLoggedOnUser = $userInfo.name
    					UserPhone = $userInfo.officephone
    				}
    				#Set-ADComputer -Identity $computer.Name -Description $userInfo.Name
    				#Get-ADComputer -Identity $computer.Name -Properties Description | select Name, Description
    				#}
    				#}
    			}
    			
    		}
    		$PSObject = New-Object -TypeName System.Management.Automation.PSObject -Property $hash
    		$PSObjectCollection += $PSObject
    	}
    	
    	Write-Output $PSObjectCollection
    	
    
    Get-IAAMComputers
    
  • #27818
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    You're creating a psobject based on $hash, but $hash is only being populated if two conditions are met (the computer responds to pings, and you were able to fetch the folders under \\computer\c$\users).

    Depending on what you want to happen when computers who do NOT respond to pings or whose Users folders can't be accessed, you would need to revise the code slightly. The simplest fix would be to put the New-Object call directly after the assignment to $hash. Something like this:

    function Get-IAAMComputers
    {
    	$hash = $null
    	
    	$psObjectCollection = @()
    	Import-Module ActiveDirectory
    	$targetGroupOU = "CN=dummy,DC=dummy,DC=net"
    	$computers = Get-ADGroupMember -Identity $targetGroupOU | Where-Object { ($_.name -like "asdfasdf*") -or ($_.name -like "asdf1234*") } | Select-Object -ExpandProperty name -First 3
    	foreach ($computer in $computers)
    	{
    
    		$computerADInfo = Get-ADComputer -Identity $computer -Properties * | select name, description
    		
    		$connectionTest = Test-Connection $computer -Quiet
    		if ($connectionTest -ne $false)
    		{
    			$folderList = Get-Childitem -Path \\$computer\c$\Users -Directory
    			if ($folderList -ne $null)
    			{
    				$lastUser = $folderList | Sort-Object LastWriteTime | Select-Object -Last 1
    				$userInfo = Get-ADUser $lastUser.name -Properties * | select name, officephone
    				$hash = @{
    					ComputerName = $computerADInfo.Name
    					ComputerDescription = $computerADInfo.Description
    					LastLoggedOnUser = $userInfo.name
    					UserPhone = $userInfo.officephone
    				}
    		        $PSObject = New-Object -TypeName System.Management.Automation.PSObject -Property $hash
    		        $PSObjectCollection += $PSObject
    			}
    			
    		}
    	}
    	
    	Write-Output $PSObjectCollection
    }
    
    Get-IAAMComputers
    
  • #27819
    Profile photo of Karson Van Meeteren
    Karson Van Meeteren
    Participant

    Dave, thank you! I took your advice and added some custom entries within an "else" and had that generate a different hash entry indicating remote connectivity failure.

    I have the PSObject created in the right spot thanks to your advice.

    Sending you a virtual fist bump!

You must be logged in to reply to this topic.