Powershell为适配器分配多个IP地址
我想编写一个 powershell 脚本来创建一个环回适配器并为其分配多个 IP 地址。您可以看到我从哪里获取适配器,然后有一个 if 语句根据它是否已经存在而起作用。在 if 语句中,我要创建并命名适配器。接下来我想给它分配多个IP地址。
我手动创建了适配器。我的代码正在尝试分配 IP 地址。但是,IP 地址似乎没有被分配。另外,如果您首先知道如何创建环回适配器,请告诉我。
cls
# Get-wmiobject win32_NetworkAdapter
$networkAdapter = Get-WMIObject win32_NetworkAdapter | where{$_.ServiceName -eq 'msloop'}
if(!$networkAdapter)
{
#"null"
}
"The following IP Addresses are already assigned:"
#Get-WMIObject win32_NetworkAdapterConfiguration -filter "IPEnabled = $true" | Foreach-Object { $_.IPAddress }
$adapterIndex = $networkAdapter.Index;
$adapterConfig = (Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter "Index = $adapterIndex AND ipenabled = 'true'");
$adapterConfig.IPAddress
$ip = @("192.168.200.1", "192.168.200.2", "192.168.200.3", "192.168.200.4", "192.168.200.5", "192.168.200.6")
$dns = "255.255.255.0"
$adapterConfig.EnableStatic($ip, $dns)
$adapterConfig.IPAddress
I want to write a powershell script to create a loopback adapter and assign multiple ip addresses to it. You can see where I am getting the adapter and then have an if statement that works based on whether or not it already exists. In the if statement I'll want to create and name the adapter. Following that I want to assign multiple IP addresses to it.
I manually created the adapter. My code is trying to assign ip addresses. However, the ip addresses don't seem to be getting assigned. Also, if you know how to create the loopback adapter in the first place let me know.
cls
# Get-wmiobject win32_NetworkAdapter
$networkAdapter = Get-WMIObject win32_NetworkAdapter | where{$_.ServiceName -eq 'msloop'}
if(!$networkAdapter)
{
#"null"
}
"The following IP Addresses are already assigned:"
#Get-WMIObject win32_NetworkAdapterConfiguration -filter "IPEnabled = $true" | Foreach-Object { $_.IPAddress }
$adapterIndex = $networkAdapter.Index;
$adapterConfig = (Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter "Index = $adapterIndex AND ipenabled = 'true'");
$adapterConfig.IPAddress
$ip = @("192.168.200.1", "192.168.200.2", "192.168.200.3", "192.168.200.4", "192.168.200.5", "192.168.200.6")
$dns = "255.255.255.0"
$adapterConfig.EnableStatic($ip, $dns)
$adapterConfig.IPAddress
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论