Code: Select all
function get-netinfo ([string]$ipaddress=$null, [int]$NetClass=$null, [string]$SubNetMask=$null, $inputObject=$null) {
Begin{
$output= @()
Write-Host "Begin:"
if ($ipaddress -ne $null){
$T ="" |Select-Object IPAddress, network, subnetmask, NetClass;
$t.ipaddress = $ipaddress
$t.netclass = $NetClass
$t.subnetmask = $SubNetMask
$inputObject += $t
}
}
Process{
if (($inputObject -ne $null)){
$f = "Processing:" + ($_.ipaddress) + " '" + $_.netclass +"' '" + $_.subnetmask + "'"
Write-Host $f
$T ="" |Select-Object IPAddress, network, subnetmask, NetClass;
$Ntemp=""
$Mtemp=""
$nctemp=$_.netclass
ForEach ($ele in $_.ipaddress.split(".")){$Ntemp += [convert]::tostring($ele, 2).padleft(8, "0")}
if ($_.subnetmask -ne $null ) {
ForEach ($ele in $_.subnetmask.split(".")){
$Mtemp += [convert]::tostring($ele, 2).padleft(8, "0")
$NCtemp = $Mtemp.indexof("0")
}
}
Else {
$Mtemp= (("").padright($nctemp,"1")).padright(32,"0")
}
$Ntemp = ($Ntemp.substring(0,$nctemp)).padright(32,"0")
$t.ipaddress = $_.ipaddress
$o1 = [convert]::toint32($Ntemp.substring(00,8),2)
$o2 = [convert]::toint32($Ntemp.substring(08,8),2)
$o3 = [convert]::toint32($Ntemp.substring(16,8),2)
$o4 = [convert]::toint32($Ntemp.substring(24,8),2)
$t.network = "$o1.$o2.$o3.$o4"
$o1 = [convert]::toint32($Mtemp.substring(00,8),2)
$o2 = [convert]::toint32($Mtemp.substring(08,8),2)
$o3 = [convert]::toint32($Mtemp.substring(16,8),2)
$o4 = [convert]::toint32($Mtemp.substring(24,8),2)
$t.subnetmask = "$o1.$o2.$o3.$o4"
$t.netclass = $nctemp
$output += $t
}
}
End{
return $output
}
}
Code: Select all
$to=@()
$t = "" |Select-Object ipaddress, netclass, subnetmask;
$t.ipaddress = "10.136.10.192"
$t.netclass = 24
$to += $t
$t = "" |Select-Object ipaddress, netclass, subnetmask;
$t.ipaddress = "10.136.106.50"
$t.subnetmask = "255.255.252.0"
$to += $t
$to | get-netinfo -ipaddress 11.128.9.15 -NetClass 8 | ft -autosize
Output:
Begin:
Processing:10.136.10.192 '24' ''
Processing:10.136.106.50 '' '255.255.252.0'
IPAddress network subnetmask NetClass
--------- ------- ---------- --------
10.136.10.192 10.136.10.0 255.255.255.0 24
10.136.106.50 10.136.104.0 255.255.252.0 22
Code: Select all
get-netinfo -ipaddress 10.194.7.255 -netclass 22 | ft -autosize
Output:
Begin:
Processing: '' ''
You cannot call a method on a null-valued expression.
At :line:26 char:38
+ ForEach ($ele in $_.ipaddress.split <<<< (".")){$Ntemp += [convert]::tostring($ele, 2).padleft(8, "0")}