【问题标题】:Adding additional IP addresses on windows server在 Windows 服务器上添加额外的 IP 地址
【发布时间】:2018-03-17 04:21:30
【问题描述】:

是否可以使用 powershell 或使用C:\Windows\addExtraIP.bat 下的 bat 脚本,该脚本使用多个参数将 IP 地址添加到接口/连接。

这个想法是使用类似的东西:

addExtraIP.bat 192.168.1.2 192.168.1.3 192.168.1.4 and so on..

这个addExtraIP.bat 脚本应该从远程调用/调用。我读过一些关于PowerShell remoting 的文章,但不知道从哪里开始或做什么。

有什么想法吗?提前致谢!

编辑:

function Set-IPAddress {
        param(  [string]$networkinterface = "Local Area Connection",
            [string]$ip, 
            [string]$mask,
            [string]$gateway, 
            [string]$dns1 = "8.8.8.8",
            [string]$dns2 = "8.8.4.4",
            [string]$registerDns = "TRUE"
     )


     #Start writing code here
     $dns = $dns1
     if($dns2){$dns =$dns1,$dns2}
     $index = (gwmi Win32_NetworkAdapter | where {$_.netconnectionid -eq $networkinterface}).InterfaceIndex
     $NetInterface = Get-WmiObject Win32_NetworkAdapterConfiguration | where {$_.InterfaceIndex -eq $index}
     $NetInterface.EnableStatic($ip, $mask)
     $NetInterface.SetGateways($gateway)
     $NetInterface.SetDNSServerSearchOrder($dns)
     $NetInterface.SetDynamicDNSRegistration($registerDns)

 }

 Set-IPAddress

这个脚本是我想要使用的。我该如何调用上面提到的脚本?

编辑 2:

function Set-IPAddress {
        param(
            [string]$networkinterface = "Local Area Connection",
            [string[]]$ip, 
            [string[]]$mask,
            [string]$gateway, 
            [string]$dns = @("10.210.1.101", "10.210.1.130"),
            [bool]$registerDns = $true
     )


     $index = (gwmi Win32_NetworkAdapter | where {$_.netconnectionid -eq $networkinterface}).InterfaceIndex
     $NetInterface = Get-WmiObject Win32_NetworkAdapterConfiguration | where {$_.InterfaceIndex -eq $index}
     $NetInterface.EnableStatic($ip, $mask)
     $NetInterface.SetGateways($gateway)
     $NetInterface.SetDNSServerSearchOrder($dns)
     $NetInterface.SetDynamicDNSRegistration($registerDns)

 }

 Set-IPAddress -IP '1.2.3.4', '2.3.4.5' -Mask '255.255.255.0', '255.255.255.0'

那么我将如何在脚本中设置它并运行,而不管传递给它的参数有多少?

【问题讨论】:

  • 是的,但最好使用包含 IP 地址的文本文件。如果您没有发布需要帮助的当前脚本,那么您的问题将是直接的代码请求,并且不在此处。
  • 您的问题非常模糊,不包含实际代码,因此我们不知道您要做什么或如何做。您确实需要阅读How to Ask,因为这将帮助您弄清楚您的问题缺少什么以及为什么它被否决。
  • 我已经对这个问题进行了更新。我理解反对票,但整个 Windows 对我来说都是新事物。很长一段时间我需要使用Windows机器。同样,这个想法是向接口添加多个 IP,就像 Linux 使用 eth0:1, eth0:2... 等等一样。
  • @JamesC。我看到New-NetIPAddress -InterfaceIndex 12 -IPAddress 192.168.0.1 -PrefixLength 24 -DefaultGateway 192.168.0.5,但我将如何使用for loop 并替换192.168.0.1 作为参数?这个ps1 脚本也应该被远程调用。

标签: windows powershell batch-file scripting


【解决方案1】:

使用New-NetIPAddress的基本foreach循环:

param([array]$IPs)

Foreach ($IP in $IPs)
{
    New-NetIPAddress -InterfaceIndex 12 -IPAddress $IP -PrefixLength 24 -DefaultGateway 192.168.0.5
}

然后像这样调用:

ScriptName.ps1 -IPs 192.168.0.1,192.168.0.2

【讨论】:

  • 谢谢!先去当地看看。我终于需要使用chef-knife 从远程机器调用脚本
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-23
  • 1970-01-01
  • 2016-09-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多