Hi Scripting Guys,
I've got a script which works perfectly on Win8 but fails on Win7 which must be down to the PS version, I am using V2.0 on Win 7
The error I get is as follows
Method invocation failed because [System.String] doesn't contain a method named 'G
etAddressBytes'.
At line:169 char:29+ $SubnetMask.GetAddressBytes <<<< () | ForEach-Object { [Convert]::ToString($_,
2) } + CategoryInfo : InvalidOperation: (GetAddressBytes:String) [], Runt
imeException+ FullyQualifiedErrorId : MethodNotFoundMy script is as follows..
function ConvertTo-MaskLength {<#
.Synopsis
Returns the length of a subnet mask.
.Description
ConvertTo-MaskLength accepts any IPv4 address as input, however the output value
only makes sense when using a subnet mask.
.Parameter SubnetMask
A subnet mask to convert into length
#>
[CmdLetBinding()]
param(
[Parameter(Mandatory = $True, Position = 0, ValueFromPipeline = $True)]
[Alias("Mask")]
[Net.IPAddress]$SubnetMask
)
process {
$Bits = "$( $SubnetMask.GetAddressBytes() | ForEach-Object { [Convert]::ToString($_, 2) } )" -replace '[\s0]'
return $Bits.Length
}
}
if(Test-Connection -ComputerName $Computer -Count 1 -ea 0) {
$Networks = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $Computer | ? {$_.IPEnabled}
foreach ($Network in $Networks) {
$IPAddress = $Network.IpAddress[0]
$SubnetMask = $Network.IPSubnet[0]
$DefaultGateway = $Network.DefaultIPGateway
$DNSServers = $Network.DNSServerSearchOrder
$IsDHCPEnabled = $false
If($network.DHCPEnabled) {
$IsDHCPEnabled = $true
}
$Bits = "$( $SubnetMask.GetAddressBytes() | ForEach-Object { [Convert]::ToString($_, 2) } )" -replace '[\s0]'
$slash= $Bits.Length
$ipslash = "$IPAddress\$slash"
$MACAddress = $Network.MACAddress
Write-Host "Computer Name : $Computer"
Write-Host "IP address : $IPAddress"
Write-Host "IP address/slash : $ipslash"
Write-Host "Subnetmask : $SubnetMask"
Write-Host "default gateway : $DefaultGateway "
Write-Host "DNS Servers : $DNSServers"
Write-Host "MAC ADDRESS : $MACAddress"
}
}Do you know how I could get this to work on PS2.0?
Thanks in advance
Joe