1

So I'm trying to write a script that sets the DNS forwarders to 2 preset IP's but if the user wants to choose other IP's he just needs to give them in the prompt.

Write-Host " "
Write-Host "DNS Forwarders are set on -192.168.20.3 & 168.192.24.3- want to choose these?"

$Antw = Read-Host -Prompt 'y/n'

If ($Antw.ToLower() = "n")
{
    $ip1 = Read-Host -Prompt 'DNS Forwarder 1: '
    $ip2 = Read-Host -Prompt 'DNS Forwarder 2: '

    C:\Windows\System32\dnscmd.exe $hostname /resetforwarders $ip1, $ip2
}


        Elseif ($Antw.ToLower() = "y")
        {

            C:\Windows\System32\dnscmd.exe $hostname /resetforwarders 192.168.20.3, 168.192.24.3

        }


#Write-Host $Antw

My If/ElseIf doesn't seem to be working however, If I press 'y', it still asks for the 2 ip's?? what's wrong with my code?

Thanks

2 Answers 2

2

This is a common error among those not completely comfortable with PowerShell. Comparisons in PowerShell are not done with the classical operator symbols; you must use the "FORTRAN-style" operators:

 Write-Host " "
 Write-Host "DNS Forwarders are set on -192.168.20.3 & 168.192.24.3- want to choose these?"

 $Antw = Read-Host -Prompt 'y/n'

 If ($Antw.ToLower() -eq "n")
 {
     $ip1 = Read-Host -Prompt 'DNS Forwarder 1: '
     $ip2 = Read-Host -Prompt 'DNS Forwarder 2: '

     C:\Windows\System32\dnscmd.exe $hostname /resetforwarders $ip1, $ip2
 }


         Elseif ($Antw.ToLower() -eq "y")
         {

             C:\Windows\System32\dnscmd.exe $hostname /resetforwarders 192.168.20.3, 168.192.24.3

         }


 #Write-Host $Antw
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much Jeff!! I didn't know that -eq was used as a comparison method in PowerShell.
@KahnKah - You'll find that PowerShell's own help files are quite useful - in an elevated Powershell session, execute the Update-Help command, then in any Powershell session, execute Get-Help about_Comparison_Operators. Get-Help is one of the most useful cmdlets available.
2

Comparison operators

-eq             Equal
-ne             Not equal
-ge             Greater than or equal
-gt             Greater than
-lt             Less than
-le             Less than or equal
-like           Wildcard comparison
-notlike        Wildcard comparison
-match          Regular expression comparison
-notmatch       Regular expression comparison
-replace        Replace operator
-contains       Containment operator
-notcontains    Containment operator
-shl            Shift bits left (PowerShell 3.0)
-shr            Shift bits right – preserves sign for signed values. (PowerShell   3.0)
-in             Like –contains, but with the operands reversed.(PowerShell 3.0)
-notin          Like –notcontains, but with the operands reversed.(PowerShell 3.0)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.