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

Kahn Kah
  • 1,389
  • 7
  • 24
  • 49

2 Answers2

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
Jeff Zeitlin
  • 9,773
  • 2
  • 21
  • 33
  • Thank you very much Jeff!! I didn't know that -eq was used as a comparison method in PowerShell. – Kahn Kah Feb 15 '17 at 13:21
  • 1
    @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. – Jeff Zeitlin Feb 15 '17 at 13:24
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)
MahmutKarali
  • 339
  • 4
  • 8