Powershell为适配器分配多个IP地址

发布于 2024-12-06 11:03:15 字数 958 浏览 0 评论 0原文

我想编写一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文