【问题标题】:Multiple Errors in Powershell Script (for changing NIC Settings)Powershell 脚本中的多个错误(用于更改 NIC 设置)
【发布时间】:2020-09-11 05:56:34
【问题描述】:

我是 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" )

Errors when Script runs on a VM

【问题讨论】:

  • 要么代码在论坛中的格式错误,要么你实际上没有正确定义你的函数。他们需要花括号。函数测试 ($param){ 这是函数体 }

标签: windows powershell networking ipconfig


【解决方案1】:

删除 cmets # This means subnet mask = 255.255.255.0 这似乎是网关、dns 和 iptype 未传递给函数 Set-ManualIP 的问题。

【讨论】:

  • 非常感谢,现在它可以完美运行了。我需要改变什么来通过 Set-ManualIP 的网关或者我需要使用什么功能?
  • 请创建一个新问题,并尽可能具体地提供代码,描述您的期望并显示脚本的结果(错误消息)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-10-02
  • 2016-10-15
  • 1970-01-01
  • 2020-07-03
  • 2010-09-23
  • 2012-09-20
  • 1970-01-01
相关资源
最近更新 更多