我想让这个powershell在Windows7中工作(用一个适当的替代Get-NetConnectionProfile)

8nuwlpux  于 2023-05-07  发布在  Shell
关注(0)|答案(2)|浏览(91)

我有这个脚本,在Windows 10+中工作,但我需要它也在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"}

目标是测试我是否有一个DomainAuthenticated网络连接,然后返回机器的主机名,如果还有一个活动的PUBLIC网络连接。问题是Get-NetworkConnectionProfile仅在W7以上的操作系统中可用。
W10前的等效值是多少?

ryevplcw

ryevplcw1#

这可能不起作用,我没有Win7来测试这个,但这是我发现的。
我相信此下载将在Windows 7上获得PowerShell版本5.1:
https://www.microsoft.com/en-us/download/details.aspx?id=54616
不知何故,ChatGPT找到了Windows 7 Service Pack 1(SP1)的远程服务器管理工具的旧链接,我相信它包含Get-NetConnectionProfile cmdlet,但是,链接已失效:
https://www.microsoft.com/en-us/download/details.aspx?id=7887
去了Wayback Machine,给了它一个死链接:
https://archive.org/web/
在浏览了Wayback Machine的历史后,我发现了这个2020年2月21日的页面:
https://web.archive.org/web/20200221043944/https://www.microsoft.com/en-us/download/details.aspx?id=7887
页面为:

Windows 7 Service Pack 1(SP1)远程服务器管理工具

成功下载了64位版本的文件(不让我下载32位,不知道为什么。):
Windows6.1-KB958830-x64-RefreshPkg.msu------* 大小:*245,285 KB
如果您成功下载并安装了以上内容,则应该安装了带有Get-NetConnectionProfile cmdlet的PowerShell 5.1。

rta7y2nd

rta7y2nd2#

这似乎是我一直在寻找的解决方案。如果有人看到什么明显的错误,就大声喊出来!

$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"
    }
}

相关问题