Kahn Kah
Kahn Kah

Reputation: 1453

Powershell simple syntax if condition not working

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

Upvotes: 1

Views: 773

Answers (2)

MahmutKarali
MahmutKarali

Reputation: 365

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)

Upvotes: 2

Jeff Zeitlin
Jeff Zeitlin

Reputation: 10754

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

Upvotes: 2

Related Questions