[英]Setting Static IP on NICs - Powershell
I am creating a script which will run through a lot of the basic tasks when commissioning a new server. 我正在创建一个脚本,它将在调试新服务器时执行许多基本任务。 Now most servers have multiple NICs these days and I need to question the user (using the script) what NIC they want to assign the IP to. 现在,如今大多数服务器都具有多个NIC,我需要(使用脚本)询问用户要将IP分配给哪个NIC。
At the moment I have: 目前,我有:
$NICs = Get-WMIObject Win32_NetworkAdapterConfiguration -ComputerName $env:COMPUTERNAME | where{$_.IPEnabled -eq $true -and $_.DHCPEnabled -eq $true}
Which will put the NICs into $NICs
I believe. 我相信这会将NIC变成$NICs
。 However from here I want to print them out to the user, and then somehow assign a value to each NIC, like an index, so the user can type in, "1" or "2" so tell the script what NIC to apply the Ip configuration to which will be done like: 但是从这里我想将它们打印出来给用户,然后以某种方式为每个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")
Any ideas? 有任何想法吗?
If you ensure $NICS is always an array, you can use the array index to specify each NIC. 如果确保$ NICS始终是一个阵列,则可以使用阵列索引来指定每个NIC。 To ensure it is always an array do this: 为了确保它总是一个数组,请执行以下操作:
$NICs = @(Get-WMIObject Win32_NetworkAdapterConfiguration -ComputerName $env:COMPUTERNAME | where{$_.IPEnabled -eq $true -and $_.DHCPEnabled -eq $true})
Then print out the info like so: 然后像这样打印出信息:
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
Then access each NIC like so: 然后像这样访问每个NIC:
$NICS[$selectedIndex]
$NICs = @(Get-WMIObject Win32_NetworkAdapterConfiguration ...)
will make $NICs
an array, which can be accessed by (zero-based) index: 将使$NICs
成为一个数组,可以通过(从零开始的)索引进行访问:
$NICs[0] # <-- first interface
$NICs[1] # <-- second interface
...
The way I would do it. 我会做的方式。 If you have a look to the network connexions panel in the internet connexions. 如果您要查看Internet连接中的“网络连接”面板。 You can see the string the user know for his devices : 您可以看到用户为他的设备知道的字符串:
So in a dialog with the user I would give this name retreiving it with win32_NetworkAdapter
joinning Win32_NetworkAdapterConfiguration
with Index
. 因此,在与用户的对话中,我将使用win32_NetworkAdapter
Win32_NetworkAdapterConfiguration
与Index
结合使用来win32_NetworkAdapter
该名称。
$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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.