在NIC上设置静态IP - Powershell

时间:2013-07-19 21:13:37

标签: powershell wmi wmi-query

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

目前我有:

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

我相信会将NIC放入$NICs。但是从这里我想将它们打印给用户,然后以某种方式为每个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") 

有什么想法吗?

3 个答案:

答案 0 :(得分: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

然后像这样访问每个网卡:

$NICS[$selectedIndex]

答案 1 :(得分:0)

$NICs = @(Get-WMIObject Win32_NetworkAdapterConfiguration ...)

将使$NICs成为一个数组,可以通过(从零开始)索引访问:

$NICs[0]  # <-- first interface
$NICs[1]  # <-- second interface 
...

答案 2 :(得分:0)

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

enter image description here

因此,在与用户的对话框中,我会使用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