POWERSHELL: Query Network Settings On Remote Systems

#------------begin script--------------------#------------begin script-----------------------------

$TimeStamp  = Get-Date -UFormat "%Y%m%d-%H%M"               # Get date/time stamp of script runtime
$ScriptPath = Split-Path $MyInvocation.MyCommand.Path       # Path of the script
$List       = $ScriptPath + "\servers.txt"                  # Input File containing the list of servers
$log        = New-Item -ItemType file -Path $ScriptPath -name query_DNS_$TimeStamp.csv -Force            # Log File
$dnsexists  = "empty"

Add-Content -Path $log -Value "Servers,DNSHostName,MacAddress,IpAddress,IpSubnet,DefaultIpGateway,DNSServerSearchOrder,FullDNSREgistrationEnabled,DHCPEnabled"

[array]$List = get-content $List 
for($i=0; $i -lt $List.Length; $i++) 
{
    $Server = $List[$i]
    Write-Progress -Activity "Querying DNS for " -Status "Server: $Server"
    if (Test-Connection -ComputerName $Server -Quiet) 
    {
        $colItems = GWMI -cl "Win32_NetworkAdapterConfiguration" -name "root\CimV2" -comp $Server -filter "IpEnabled = TRUE"
            foreach ($objItem in $colItems)
            {
                $strDNSServerSearchOrder = $objItem.DNSServerSearchOrder
                $dnsexists = $strDNSServerSearchOrder
                if ($dnsexists -ne "empty")
                    {
                        Add-Content -Path $log -Value `
                        "$($Server),$($objItem.DNSHostName),$($objItem.MacAddress),`
                        $($objItem.IpAddress),$($objItem.IpSubnet),$($objItem.DefaultIpGateway),`
                        $($objItem.DNSServerSearchOrder),$($objItem.FullDNSREgistrationEnabled),`
                        $($objItem.DHCPEnabled)"
                    }
                else 
                    {
                        Add-Content -Path $log -Value "$($Server),No comments"
                    }
                $dnsexists = "empty"
            }
    }
    else 
    {
        Add-Content -Path $log -Value "$($Server),Offline"
    }
}

#------------End script--------------------------

No comments:

Post a Comment