Last active
May 1, 2019 16:30
-
-
Save jlzimmer/a93817f130e21a35f1a7e52db3d6b2d5 to your computer and use it in GitHub Desktop.
Runs ping.exe and an AD query against each member of a list of computer names to aid in SCCM client discovery
This file contains hidden or 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
Import-Module ActiveDirectory | |
$ErrorActionPreference = "Stop" | |
# Accepts user input for a filename, verify the path, and import the list of computer names | |
$Path = Read-Host -Prompt "Enter the filename containing device names to ping test" | |
$Test = Test-Path $Path | |
if (-not $Test) | |
{ | |
throw "Specified file path does not exist." | |
} | |
$InFile = Import-Csv -Path $Path -Header 'ComputerName' | |
# Defines the target destination for the output .csv file and create that file with predefined headers | |
$Path = Read-Host -Prompt "Enter a name for the output file (do not include extension)" | |
$Path = "C:\Users\$env:USERNAME\Desktop\$Path.csv" | |
"ComputerName", "ADPath", "IPv4" | ForEach-Object ` | |
-Begin { | |
$OutFile = New-Object psobject | |
} ` | |
-Process { | |
Add-Member -InputObject $OutFile -MemberType NoteProperty -Name $_ -Value "" | |
} ` | |
-End { | |
$OutFile | Export-Csv $Path -NoTypeInformation | |
} | |
# Creates an empty array to store the names of computers without an associated AD object | |
$Array = @() | |
$NoADObject = {$Array}.Invoke() | |
Write-Host "Pinging network for device connections..." | |
# Loops through each name from the input file | |
$InFile | ForEach-Object ` | |
-Process { | |
[string]$Cn = $_.ComputerName | |
# Attempts to find an associated AD object, then use its FQDN to ping the device and get its IP address | |
try { | |
$ADEntry = Get-ADComputer -Identity $_.ComputerName -Properties * | |
$OS = Select-Object -InputObject $ADEntry -ExpandProperty OperatingSystem | |
# Filters out all non-Windows objects and appends device information to the output .csv file | |
if ($OS -match "Windows*") { | |
$ADPath = Select-Object -InputObject $ADEntry -ExpandProperty CanonicalName | |
$Ping = Get-WmiObject -ClassName Win32_PingStatus -Filter "Address='$Cn.col.missouri.edu' AND Timeout=1000" | |
$IPv4 = Select-Object -InputObject $Ping -ExpandProperty ProtocolAddress | |
$Obj = @{ | |
"ComputerName" = $_.ComputerName | |
"ADPath" = $ADPath | |
"IPv4" = $IPv4 | |
} | |
New-Object psobject -Property $Obj | Export-Csv -Path $Path -Append -Force | |
} | |
} | |
catch { | |
$NoADObject.Add($Cn) | |
} | |
} ` | |
-End { | |
# Prints the array of objects that do not have an associated AD object | |
Write-Host "Filtering out objects without an AD entry..." | |
Start-Sleep -s 5 | |
$NoADObject | |
} | |
# Pronpts the user if they would like to print the table of resultant devices in the current session | |
$x = Read-Host -Prompt "Print table of scanned devices? (y/n)" | |
if (($x -eq "y") -or ($x -eq "Y")) { | |
$Path | Import-Csv | Format-Table | |
} | |
else { | |
return | |
} | |
Read-Host -Prompt "Press any key to exit..." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Edited line 15 to output the csv report to the desktop of the current user for compatibility with runas and other runtime environment variables.