Untitled
unknown
powershell
2 years ago
1.5 kB
15
Indexable
# Array for finished data
$finishedObjects = @()
# Function to ping Hostname
Function Ping-Device([string]$rxNumber) {
# Ping device
$devicePing = Test-Connection $rxNumber -Quiet
# Return if the device pinged or not
return $devicePing
}
# Resolve DNS name
Function Resolve-DNS([string]$rxNumber) {
# Check the name associated with RXNumber
$dnsName = Resolve-DnsName -Name $rxNumber -Type A
# Return value
return $dnsName
}
# Resolve IP address to a hostname
Function Resolve-IP([string]$ipAddress) {
# Resolve a IP to a hostname
$hostName = Resolve-DnsName $ipAddress
# Return the object
return $hostName
}
# Loop through all RXNumbers defined in TXT document
foreach ($rxNumber in Get-Content '.\DNS Test\DNSDevices.txt') {
# Get returned value (On / Off)
$deviceOn = Ping-Device $rxNumber
# Check if device is on
if ( $deviceOn ) {
# Resolve the IP attached to the RX Number
$dnsResolved = Resolve-DNS $rxNumber
# Resolve the Hostname attached the to IP
$ipResolved = Resolve-IP $dnsResolved.IPAddress
# Add as an object to an array to export to CSV
$finishedObjects += New-Object -TypeName psobject -Property @{ 'RX Number' = $rxNumber; 'Hostname Resolved' = $dnsResolved.IPAddress; 'IP Resolved' = $ipResolved.NameHost }
}
}
# Export final data to CSV
$finishedObjects | Export-Csv -Path '.\DNS Test\DNSExported.csv' -NoTypeInformationEditor is loading...