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
