Multiple Errors in Powershell Script (for changing NIC Settings)

251 Views Asked by At

i'm a relative newcomer to Powershell scripting so every bit of Help is very much appreciated. I put together a Script (mostly from Parts and from what i think would work) to change IP Addresses from a multiple Choice Menu and it seems to be kinda working but throws multiple Errors when selecting a Option.

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

1

There are 1 best solutions below

2
On BEST ANSWER

remove the comments # This means subnet mask = 255.255.255.0 this seems to be the problem as gateway, dns and iptype are not passed on to the function Set-ManualIP.