0

我正在创建一个脚本,它将在调试新服务器时运行许多基本任务。现在大多数服务器都有多个 NIC,我需要询问用户(使用脚本)他们想要将 IP 分配给哪个 NIC。

目前我有:

$NICs = Get-WMIObject Win32_NetworkAdapterConfiguration -ComputerName $env:COMPUTERNAME | where{$_.IPEnabled -eq $true -and $_.DHCPEnabled -eq $true} 

$NICs我相信这会将 NIC 放入其中。但是从这里我想将它们打印给用户,然后以某种方式为每个 NIC 分配一个值,例如索引,因此用户可以输入“1”或“2”,以便告诉脚本应用哪个 NIC将完成的IP配置如下:

If($ipQuestion -eq "Y") {
    $ipAddr = Read-Host "Enter IP Address: "
    $subnet = Read-Host "Enter Subnet: "
    $dns = Read-Host "Enter DNS: "
    Write-Host "Multiple DNS servers?" -ForegroundColor Green
    $multipleDNSServersQuestion = Read-Host 
    If($multipleDNSServersQuestion -eq 'Y'){
        $dns2 = Read-Host "Enter Secondary DNS: "
    }
    $dGateway = Read-Host "Enter Default Gateway: "
}

$NIC.EnableStatic($ipAddr, $subnet) 
$NIC.SetGateways($dGateway) 
$NIC.SetDNSServerSearchOrder($dns, $dns2) 
$NIC.SetDynamicDNSRegistration("TRUE") 

有任何想法吗?

4

3 回答 3

1

如果您确保 $NICS 始终是一个数组,您可以使用数组索引来指定每个 NIC。为确保它始终是一个数组,请执行以下操作:

$NICs = @(Get-WMIObject Win32_NetworkAdapterConfiguration -ComputerName $env:COMPUTERNAME | where{$_.IPEnabled -eq $true -and $_.DHCPEnabled -eq $true})

然后像这样打印出信息:

PS> $NICS = @(Get-WMIObject Win32_NetworkAdapterConfiguration)
PS> $NICS | Foreach {$i=-1}{$i++;$_} | ft @{n='index';e={$i}},Description,ServiceName

 index Description                             ServiceName
 ----- -----------                             -----------
     0 WAN Miniport (L2TP)                     Rasl2tp
     1 WAN Miniport (SSTP)                     RasSstp
     2 WAN Miniport (IKEv2)                    RasAgileVpn
     3 WAN Miniport (PPTP)                     PptpMiniport
     4 WAN Miniport (PPPOE)                    RasPppoe
     5 WAN Miniport (IP)                       NdisWan
     6 WAN Miniport (IPv6)                     NdisWan
     7 WAN Miniport (Network Monitor)          NdisWan
     8 Microsoft Kernel Debug Network Adapter  kdnic
     9 RAS Async Adapter                       AsyncMac
    10 Broadcom NetXtreme Gigabit Ethernet     b57nd60a
    11 Microsoft ISATAP Adapter                tunnel
    12 Microsoft Teredo Tunneling Adapter      tunnel
    13 Microsoft 6to4 Adapter                  tunnel
    14 Microsoft ISATAP Adapter                tunnel

然后像这样访问每个 NIC:

$NICS[$selectedIndex]
于 2013-07-19T21:36:35.237 回答
0
$NICs = @(Get-WMIObject Win32_NetworkAdapterConfiguration ...)

将创建$NICs一个数组,可以通过(从零开始的)索引访问:

$NICs[0]  # <-- first interface
$NICs[1]  # <-- second interface 
...
于 2013-07-19T21:34:45.550 回答
0

我会这样做的方式。如果您查看互联网连接中的网络连接面板。您可以看到用户知道他的设备的字符串:

在此处输入图像描述

因此,在与用户的对话中,我会给出这个名称并通过加入来检索win32_NetworkAdapter它。Win32_NetworkAdapterConfigurationIndex

$NICs = Get-WMIObject Win32_NetworkAdapterConfiguration -ComputerName $env:COMPUTERNAME | where{$_.IPEnabled -eq $true -and $_.DHCPEnabled -eq $true}
$NICs | % {$i = (Get-WmiObject win32_NetworkAdapter -Filter "index=$($_.index)").NetConnectionID; $_} | ft @
{n='index';e={$i}},Description,ServiceName

index                                     Description                               ServiceName
-----                                     -----------                               -----------
NET1                                      Intel(R) 82567LM Gigabit Network Conne... e1yexpress
于 2013-07-20T11:07:01.063 回答