Mittwoch, 1. Juni 2016

Powershell reporting and fixing up IPv4 Subnet masks in a Windows Enviroment

Here have some Powershell for fixing up a "wrong" static set Subnetmask, like you have after resizing a Subnet - or got the wrong mask reported from the network guys.

So you heard about some wrong set up Subnets, uups this can happen. So first i would like to report all my windows servers in my ActiveDirectory and their setup Subnet mask on the IP address which is in DNS. Observing the report before fixing maybe nice, thatswhy DO NOT just copy&paste this code in one Script and run, maybe put it in ISE and run just selected code.

#How to Report 

Import-Module ActiveDirectory

$reportfilename = c:\Report-SRVs_SubNets.csv

Function Get-DnsEntry($iphost)
{
 If($ipHost -match "^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$")
  {
    [System.Net.Dns]::GetHostEntry($iphost).HostName
  }
 ElseIf( $ipHost )#-match "^.*\.\.*")
   {
    [System.Net.Dns]::GetHostEntry($iphost).AddressList[0].IPAddressToString
   } 
 ELSE { Throw "Specify either an IP V4 address or a hostname" }
}


$Servers = Get-ADComputer -Server $DCSRV -Filter {OperatingSystem -Like "*Server*"} -Properties *

$OutputReport = @()
[int64]$IntCounter=0
foreach ($Server in $Servers){
    $IntCounter+=1
    Write-Host $Server.Name " :  $IntCounter von " $Servers.Count
    $MYSRVInfo = '' |Select DNSHostname,DHCP,IPAddress,IPSubnet,Pingable,PWAge,OperatingSystemVersion,
    $MYSRVInfo.DNSHostname = $Server.DNSHostName
    if ($Server.DNSHostName) {$MYSRVInfo.IPAddress = Get-DnsEntry($Server.DNSHostName)}
    $MYSRVInfo.PWAge = $(New-TimeSpan -Start $([datetime]::FromFileTime($Server.pwdLastSet)) -End (get-date)).Days
    $MYSRVInfo.OperatingSystemVersion = $Server.OperatingSystemVersion
    $MYSRVInfo.Service_Pack = $Server.OperatingSystemServicePack
    try {
$IPCFG=Get-WmiObject -ComputerName $MYSRVInfo.IPAddress Win32_NetworkAdapterConfiguration -Properties IPAddress,IPSubnet,DHCPEnabled -ErrorAction Stop|?{$_.IPAddress -match $MYSRVInfo.IPAddress}
$MYSRVInfo.IPSubnet=$IPCFG.IPSubnet[$IPCFG.IPAddress.IndexOf($MYSRVInfo.IPAddress)]
$MYSRVInfo.DHCP=$IPCFG.DHCPEnabled
} catch {
$MYSRVInfo.IPSubnet='NOWMI'
$MYSRVInfo.DHCP='NOWMI'
}
    $MYSRVInfo.IPSubnet = 
    $OutputReport +=$MYSRVInfo
    if ($IntCounter % 20 -eq 0) {
        $OutputReport|Export-Csv -NoTypeInformation -Delimiter ';' -Path $reportfilename
        }
}

$OutputReport|Export-Csv -NoTypeInformation -Delimiter ';' -Path $reportfilename

# How To Fix now a certain Subnet from the Report

$ipscopetofixregex='10.1.2.*'
$rightsubnet='255.255.254.0'

# Filter now your Report 

$srvkaputt=Import-Csv -Delimiter ';' $reportfilename|?{($.IPAddress -match $ipscopetofixregex) -and ($_.IPSubnet -ne $rightsubnet) -and ([int]$_.DHCP -ne 1) -and ($_.DHCP -ne 'NOWMI')}

# Use WMI to fix it up - but be aware this code kills fixed IPv6 addresses and interrupt network traffic "slightly"

$srvkaputt|%{
    $ip=$_.IPAddress
    start-job -Args $_.IPAddress,$rightsubnet -scriptblock {
        param($ip,$sub)
        $IPCFG=(Get-WmiObject -ComputerName $ip Win32_NetworkAdapterConfiguration|?{$_.IPAddress -match $ip})
        $IPSUBNET=[string[]]($IPCFG.IPSubnet.Clone()|?{$_ -match "^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$"})
        $IPADDRES=[string[]]($IPCFG.IPAddress.Clone()|?{$_ -match "^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$"})
        $IPSUBNET[$IPADDRES.IndexOf($ip)]='$sub'
        $IPCFG.EnableStatic($IPADDRES,$IPSUBNET)
        }
    }

# Check with Get-Job maybe hunged jobs after a while and Kill them Get-Job|Remove-Job -f