I have this script that works in Windows 10+ but I need it to also work in Windows 7.
$Adapters=Get-NetAdapter -ErrorAction SilentlyContinue | ?{$_.Status -eq "Up"}
$IPAddresses=@()
$Profiles=@()
foreach ($Adapter in $Adapters)
{
$IPaddresses+=Get-NetIPAddress -InterfaceIndex $($Adapter.InterfaceIndex) -AddressFamily IPv4 -ErrorAction SilentlyContinue
$Profiles+=Get-NetConnectionProfile -InterfaceIndex $($Adapter.InterfaceIndex) -ErrorAction SilentlyContinue
}
$Domain = $IPAddresses | ? {$_.InterfaceIndex -eq ($Profiles | ? {$_.NetworkCategory -eq "DomainAuthenticated"}|select InterfaceIndex -ExpandProperty InterfaceIndex)}
$Public = $IPAddresses | ? {$_.InterfaceIndex -eq ($Profiles | ? {$_.NetworkCategory -eq "Public"}|select InterfaceIndex -ExpandProperty InterfaceIndex)}
if (($Domain -ne $null) -and ($Public -ne $null))
{
Write-Host "Public Interface found on $($env:COMPUTERNAME)"
}
else {Write-Host "None"}
The goal is to test if I have a DomainAuthenticated Network connection and then to return the hostname of the machine IF there is ALSO an active PUBLIC network connection. The problem is that Get-NetworkConnectionProfile is only available in OSs ABOVE W7.
What is the pre-W10 equivalent to this?
OK - this appears to be the solution that I was looking for. If anyone sees anything glaringly wrong, just shout out!
$OSVer = (Get-CimInstance Win32_OperatingSystem).version
if ($OSVer -like "10*") {
$Adapters = Get-NetAdapter -ErrorAction SilentlyContinue | ? { $_.Status -eq "Up" }
$IPAddresses = @()
$Profiles = @()
foreach ($Adapter in $Adapters) {
$IPaddresses += Get-NetIPAddress -InterfaceIndex $($Adapter.InterfaceIndex) -AddressFamily IPv4 -ErrorAction SilentlyContinue
$Profiles += Get-NetConnectionProfile -InterfaceIndex $($Adapter.InterfaceIndex) -ErrorAction SilentlyContinue
}
$Domain = $IPAddresses | ? { $_.InterfaceIndex -eq ($Profiles | ? { $_.NetworkCategory -eq "DomainAuthenticated" } | select InterfaceIndex -ExpandProperty InterfaceIndex) }
$Public = $IPAddresses | ? { $_.InterfaceIndex -eq ($Profiles | ? { $_.NetworkCategory -eq "Public" } | select InterfaceIndex -ExpandProperty InterfaceIndex) }
if (($Domain -ne $null) -and ($Public -ne $null)) {
Write-Host "Public Interace on $($env:COMPUTERNAME)"
}
else { Write-Host "None" }
}
else {
$Adapters = Get-WmiObject -Class Win32_NetworkAdapter -Filter "NetEnabled='True'"
$IPAddresses = @()
$Profiles = @()
foreach ($Adapter in $Adapters) {
$IPAddresses += Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter "Index='$($Adapter.Index)'" | Where-Object { $.IPAddress -ne $null -and $.IPSubnet -ne $null -and $.DefaultIPGateway -ne $null }
$Profiles += Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter "Index='$($Adapter.Index)'" | Select-Object -ExpandProperty Description
}
$Domain = $IPAddresses | ? { $.IPConnectionMetric -lt 50 -and $.DefaultIPGateway -ne $null -and $Profiles[$_.Index - 1] -match "domain" }
$Public = $IPAddresses | ? { $.IPConnectionMetric -lt 50 -and $.DefaultIPGateway -ne $null -and $Profiles[$_.Index - 1] -match "public" }
if (($Domain -ne $null) -and ($Public -ne $null)) {
Write-Host "Public Interace on $($env:COMPUTERNAME)"
}
else {
Write-Host "None"
}
}