首页 > 解决方案 > 需要确定以太网适配器的网络访问类型

问题描述

我需要尝试找出各种虚拟机上以太网连接的网络访问类型。

简而言之,我试图找到一个类比

   (Get-NetConnectionProfile).IPv4Connectivity

从 Windows 2012 开始效果很好,我希望针对 Windows 2008 R2 服务器运行此查询。

标签: windowspowershellwmiwindows-server-2008-r2

解决方案


我在 Windows 2008 服务器上使用 .NET 类,强制转换为 powershell 中的对象,示例代码如下:

    $nlm = [Activator]::CreateInstance([Type]::GetTypeFromCLSID([Guid]"{DCB00C01-570F-4A9B-8D69-199FDBA5723B}"))
    $nlm.GetNetworkConnections() | ForEach-Object { 
                                    [PSCustomObject]@{
                                    NetworkName = ($_.GetNetwork().GetName());
                                    isConnectedToInternet = $_.isConnectedToInternet;
                                   } 
    if ($nlm.IsConnectedToInternet)
            {
                $NLAState = 'Internet'
            }
            "Access type : $NLAState"

推荐阅读