Last active
March 6, 2020 00:36
-
-
Save skyhoshi/c2938269a145322821a7111733397222 to your computer and use it in GitHub Desktop.
This Script parses through all computers found in the domain. using the Object Class of Computer. (regardless of OU) : the problem I'm having is that the script outputs a loop index in one of the forgoing loops and I'm not sure how to prevent it.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-ADComputersPropertiesAndIPAddresses { | |
[CmdletBinding(DefaultParameterSetName = 'Identification', | |
SupportsShouldProcess = $true, | |
PositionalBinding = $false, | |
HelpUri = 'https://www.github.com/skyhoshi/powershell', | |
ConfirmImpact = 'Medium')] | |
[Alias()] | |
[OutputType([String])] | |
Param | |
( | |
# Param1 help description | |
[Parameter(Mandatory = $true, | |
ValueFromPipeline = $true, | |
ValueFromPipelineByPropertyName = $true, | |
ValueFromRemainingArguments = $false, | |
Position = 0, | |
ParameterSetName = 'Identification')] | |
[ValidateNotNull()] | |
[ValidateNotNullOrEmpty()] | |
# [ValidateCount(0, 5)] | |
# [ValidateSet("sun", "moon", "earth")] | |
[Alias("AD Server Name or IP Address")] | |
$HostName | |
) | |
Begin { | |
if ([System.io.File]::Exists("$env:USERPROFILE\desktop\ComputerActiveDirectoryReport-Prelim.csv")) { | |
Remove-Item "$env:USERPROFILE\desktop\ComputerActiveDirectoryReport-Prelim.csv" -Force | |
} | |
} | |
Process { | |
if ($pscmdlet.ShouldProcess("Target", "Operation")) { | |
######################################################################################## | |
$AdHostSearch = [adsisearcher]("WinNt://$hostname") | |
$AdHostSearch.Filter = "objectclass=computer"; | |
$searchResults = $null | |
$searchResults = $AdHostSearch.FindAll() | |
######################################################################################## | |
$HostNameList = New-Object -TypeName System.Collections.ArrayList; | |
#$AdComputerPropertiesList = New-Object -TypeName System.Collections.ArrayList; | |
foreach ($result in $searchResults) { | |
#Write-Host $result.Properties | |
foreach ($unTypedProperty in $result.Properties) { | |
#Write-Host "UnTyped Properties" -ForegroundColor yellow; | |
foreach ($prop in $unTypedProperty) { | |
$AdComputerName = $prop["cn"]; | |
#Write-Host -ForegroundColor Green "Properties for computer $AdComputerName"; | |
$HostNameList.Add($AdComputerName); | |
} | |
} | |
} | |
#$AdComputerPropertiesList | Select-Object * | Export-Csv "$env:USERPROFILE\desktop\ComputerActiveDirectoryReport-Prelim.csv" -NoTypeInformation -Force | |
ForEach ($hostn in $hostNameList) { | |
#Write-Host $hostname | |
try { | |
$hostEntry = [System.Net.Dns]::GetHostByName($hostn) | |
$hostEntry; | |
} | |
catch { | |
Write-Host "$hostn :: $_" -ForegroundColor Yellow | |
} | |
} | |
} | |
} | |
End { | |
} | |
} | |
Get-ADComputersPropertiesAndIPAddresses -HostName "[Insert Your Local Domain controller host name here]" #HostName = Name of Domain Controller |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$hostname = "[Insert Your Local Domain controller host name here]" | |
######################################################################################## | |
$AdHostSearch = [adsisearcher]("WinNt://$hostname") | |
$AdHostSearch.Filter = "objectclass=computer"; | |
$searchResults = $null | |
$searchResults = $AdHostSearch.FindAll() | |
######################################################################################## | |
$HostNameList = New-Object -TypeName System.Collections.ArrayList; | |
#$AdComputerPropertiesList = New-Object -TypeName System.Collections.ArrayList; | |
$searchResultsCount = $searchResults.Count; | |
Write-Host "Search Results Count : $searchResultsCount"; | |
foreach ($result in $searchResults) { | |
$resultPropertiesCount = $result.Properties.Count; | |
Write-Host "Result Properties Count: $resultPropertiesCount"; | |
#Write-Host "UnTyped Properties" -ForegroundColor yellow; | |
foreach ($prop in $result.Properties) { | |
$AdComputerName = $prop["cn"]; | |
#Write-Host -ForegroundColor Green "Properties for computer $AdComputerName"; | |
$HostNameList.Add($AdComputerName); | |
} | |
} |
I added the second file to isolate and demonstrate the problem code.
A Friend on Slack helped (Thanks @coiscir : Twitter handle) with the answer :
$HostNameList.Add($AdComputerName); Returns an int.
This can be updated to the following
$HostNameList.Add($AdComputerName) | Out-Null; Sends the output to Null.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think that the problem lies in the third loop, that is a secondary loop of the same information from the data duplicated from the second loop.
Loop 1 : line 41
Loop 2 : line 43
Loop 3 : line 45
Loop 2 and Loop 3 data output is equal in that their are two references to the same information
But WHY is it outputting the following