我是 Powershell 脚本的相对新手,因此非常感谢您的每一点帮助。我整理了一个脚本(主要来自 Parts 和我认为可行的脚本)来从多项选择菜单中更改 IP 地址,它似乎有点工作,但在选择选项时会引发多个错误。
function Find-Adapter() {
return Get-NetAdapter -Physical | Where-Object {$_.Status -eq "up"}
}
function Set-ManualIP($IP, $MaskBits, $Gateway, $Dns, $IPType) {
# Retrieve the network adapter that you want to configure
$adapter = Find-Adapter
# Remove any existing IP, gateway from our ipv4 adapter
If (($adapter | Get-NetIPConfiguration).IPv4Address.IPAddress) {
$adapter | Remove-NetIPAddress -AddressFamily $IPType -Confirm:$false
}
If (($adapter | Get-NetIPConfiguration).Ipv4DefaultGateway) {
$adapter | Remove-NetRoute -AddressFamily $IPType -Confirm:$false
}
# Configure the IP address and default gateway
$adapter | New-NetIPAddress `
-AddressFamily $IPType `
-IPAddress $IP `
-PrefixLength $MaskBits `
-DefaultGateway $Gateway
# Configure the DNS client server IP addresses
$adapter | Set-DnsClientServerAddress -ServerAddresses $DNS
}
do {
do {
write-host "================ IP Adresswahl ================"
write-host ""
write-host "A - Automatik (DHCP)"
write-host ""
write-host "===================== GAMS ===================="
write-host "B - GAMS Adresse 171"
write-host "C - GAMS Adresse 172"
write-host "D - GAMS Adresse 173"
write-host "E - GAMS Adresse 174"
write-host ""
write-host "================ Andere Systeme ==============="
write-host "F - i.Search"
write-host "G - BeLiMo"
write-host "H - LOGO!"
write-host "i - Cisco Setup"
write-host "J - Cisco BMA"
write-host "K - Cisco GAMS"
write-host ""
write-host ""
write-host "X - Exit - Beenden"
write-host ""
write-host -nonewline "Auswahl eingeben und mit Enter bestaetigen: "
$choice = read-host
write-host ""
$ok = $choice -match '^[A-Z]+$'
if ( -not $ok) {
write-host "Invalid selection"
}
} until ( $ok )
switch -Regex ( $choice ) {
"A"
{
write-host "Automatische Adresse wird gesetzt"
$IPType = "IPv4"
$adapter = Find-Adapter
$interface = $adapter | Get-NetIPInterface -AddressFamily $IPType
If ($interface.Dhcp -eq "Disabled")
{
# Remove existing gateway
If (($interface | Get-NetIPConfiguration).Ipv4DefaultGateway) {
$interface | Remove-NetRoute -Confirm:$false
}
# Enable DHCP
$interface | Set-NetIPInterface -DHCP Enabled
# Configure the DNS Servers automatically
$interface | Set-DnsClientServerAddress -ResetServerAddresses
}
}
"B"
{
write-host "Adresse GAMS 171 wird gesetzt"
Set-ManualIP `
-IP "192.168.0.171" `
-MaskBits 24 ` # This means subnet mask = 255.255.255.0
-Gateway "192.168.0.2" `
-Dns "192.168.0.2" `
-IPType "IPv4"
}
"C"
{
write-host "Adresse GAMS 172 wird gesetzt"
Set-ManualIP `
-IP "192.168.0.172" `
-MaskBits 24 ` # This means subnet mask = 255.255.255.0
-Gateway "192.168.0.2" `
-Dns "192.168.0.2" `
-IPType "IPv4"
}
"D"
{
write-host "Adresse GAMS 173 wird gesetzt"
Set-ManualIP `
-IP "192.168.0.173" `
-MaskBits 24 ` # This means subnet mask = 255.255.255.0
-Gateway "192.168.0.2" `
-Dns "192.168.0.2" `
-IPType "IPv4"
}
"E"
{
write-host "Adresse GAMS 174 wird gesetzt"
Set-ManualIP `
-IP "192.168.0.174" `
-MaskBits 24 ` # This means subnet mask = 255.255.255.0
-Gateway "192.168.0.2" `
-Dns "192.168.0.2" `
-IPType "IPv4"
}
"F"
{
write-host "Adresse i.Search wird gesetzt"
Set-ManualIP `
-IP "192.168.1.250" `
-MaskBits 24 ` # This means subnet mask = 255.255.255.0
-Gateway "192.168.1.2" `
-Dns "192.168.1.2" `
-IPType "IPv4"
}
"G"
{
write-host "Adresse BeLiMo wird gesetzt"
Set-ManualIP `
-IP "192.168.0.10" `
-MaskBits 24 ` # This means subnet mask = 255.255.255.0
-Gateway "192.168.0.2" `
-Dns "192.168.0.2" `
-IPType "IPv4"
}
"H"
{
write-host "Adresse LOGO! wird gesetzt"
Set-ManualIP `
-IP "192.168.0.210" `
-MaskBits 24 ` # This means subnet mask = 255.255.255.0
-Gateway "192.168.0.2" `
-Dns "192.168.0.2" `
-IPType "IPv4"
}
"i"
{
write-host "Adresse Cisco Setup wird gesetzt"
Set-ManualIP `
-IP "192.168.1.250" `
-MaskBits 24 ` # This means subnet mask = 255.255.255.0
-Gateway "192.168.1.2" `
-Dns "192.168.1.2" `
-IPType "IPv4"
}
"J"
{
write-host "Adresse Cisco BMA wird gesetzt"
Set-ManualIP `
-IP "10.10.10.250" `
-MaskBits 24 ` # This means subnet mask = 255.255.255.0
-Gateway "10.10.10.2" `
-Dns "10.10.10.2" `
-IPType "IPv4"
}
"K"
{
write-host "Adresse Cisco GAMS wird gesetzt"
Set-ManualIP `
-IP "172.16.30.250" `
-MaskBits 24 ` # This means subnet mask = 255.255.255.0
-Gateway "172.16.30.2" `
-Dns "172.16.30.2" `
-IPType "IPv4"
}
}
} until ( $choice -match "X" )