我需要一次性将几台机器的子网掩码从 255.255.255.0 更改为 255.255.0.0。
目标是使脚本仅在具有静态 ipv4 地址的网络适配器上执行此操作,并且仅更改子网掩码,而不影响预先分配的 ipv4 IP 地址。
脚本运行后没有返回任何错误,但当我查看测试目标机器上的适配器时,子网掩码保持不变。有人能指出脚本中哪里可能出错吗?
#Defining Variables
$Computers = Get-Content "C:\computers.txt"
$SubnetMask = "255.255.0.0"
$VerbosePreference = "Continue"
$Computers
#Loop Through Hostnames
foreach ($Computer in $Computers) {
#Creating a PSSession
$PSSession = New-PSSession -ComputerName $Computer
#Getting Network Adapters
Invoke-Command -Session $PSSession -ScriptBlock {
$NetAdapters = Get-NetAdapter -Physical | Where-Object {$_.InterfaceAlias -ne "Wi-Fi"}
}
#Loop Through Network Adapters
foreach ($NetAdapter in $NetAdapters) {
#Changing Subnet Mask
Set-NetIPAddress -InterfaceAlias $NetAdapter.InterfaceAlias -PrefixLength 16 -ComputerName $Computer -SubnetMask $SubnetMask
}
#Closing PSSession
Remove-PSSession -Session $PSSession
}