Issue with If Statement Powershell

This topic contains 1 reply, has 2 voices, and was last updated by  Curtis Smith 1 week, 1 day ago.

  • Author
    Posts
  • #79942

    Kert
    Participant

    Hi all,
    I am not so much familiar with scripting, currently Im trying a script to check the ssid that you are connected to. If it is secured it will display you are secure and if you are connected to an open wifi it will tell you that you are not secure.I had uploaded both script..When I connect with secure SSID the part of IF Statement "you are secure" is working, but when I connect to open wifi it is still stating that "you are secure", where in it should state "You are not Secure".
    Another issue that is being noted is that if Invoke-expressions is not used, the script will not call test1.0.
    Such feedback is appreciated.
    Thanks

    test1.0
    #Function WifiList{

    $interface_name_text = "Interface name";
    $ssid_text = "SSID";
    $network_type_text = "Network type";
    $Authentication_text = "Authentication";
    $encryption_text = "Encryption";
    $bssid_text = "BSSID";
    $signal_text = "Signal";
    $radio_type_text = "Radio type";
    $channel_text = "Channel";
    $basic_rates_text = "Basic rates";
    $other_rates_text = "Other rates";

    # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

    function getValueByName ( $inputText, $nameString ) {
    $value = "";
    if ([regex]::IsMatch($inputText,"\b$nameString\b","IgnoreCase")) {
    $value = ([regex]::Replace($inputText,"^[^:]*: ",""));
    }
    return $value.Trim();
    }

    if ([int](gwmi win32_operatingsystem).Version.Split(".")[0] -lt 6) {
    throw "This script works on Windows Vista or higher.";
    }

    if ((gsv "wlansvc").Status -ne "Running" ) {
    throw "WLAN AutoConfig must be running.";
    }

    $activeNetworks = @();
    $rowNumber = -1;
    $interfaceName = "";

    netsh wlan show network mode=bssid | % {

    $ifName = getValueByName $_ $interface_name_text;
    if ($ifName.Length -gt 0) {
    $interfaceName = $ifname;
    return;
    }

    $ssid = getValueByName $_ $ssid_text;
    if ($ssid.Length -gt 0) {
    $row = New-Object PSObject -Property @{
    InterfaceName = $interfaceName;
    SSID=$ssid
    NetworkType=""
    Authentication=""
    Encryption=""
    BSSID=""
    Signal=""
    Channel=""
    RadioType=""
    BasicRates=""
    OtherRates=""
    }
    $rowNumber+=1;
    $ActiveNetworks += $row;
    return;
    }
    $bssid = getValueByName $_ $bssid_text;
    if ($bssid.Length -gt 0) {
    $ActiveNetworks[$rowNumber].BSSID = $bssid;
    return;
    }
    $network_type = getValueByName $_ $network_type_text;
    if ($network_type.Length -gt 0) {
    $ActiveNetworks[$rowNumber].NetworkType = $network_type;
    return;
    }
    $Authentication = getValueByName $_ $authentication_text;
    if ($Authentication.Length -gt 0) {
    $ActiveNetworks[$rowNumber].Authentication = $Authentication;
    return;
    }
    $Encryption = getValueByName $_ $encryption_text;
    if ($Encryption.Length -gt 0) {
    $ActiveNetworks[$rowNumber].Encryption = $Encryption;
    return;
    }
    $Signal = getValueByName $_ $signal_text;
    if ($Signal.Length -gt 0) {
    $prc = [regex]::Replace($Signal,"[%]","");
    $result = 0;
    [int]::TryParse($prc,[ref]$result) | Out-Null;
    $ActiveNetworks[$rowNumber].Signal = $result;
    return;
    }
    $Channel = getValueByName $_ $channel_text;
    if ($Channel.Length -gt 0) {
    $result = 0;
    [int]::TryParse($Channel,[ref]$result) | Out-Null;
    $ActiveNetworks[$rowNumber].Channel = $result;
    return;
    }
    $RadioType = getValueByName $_ $radio_type_text;
    if ($RadioType.Length -gt 0) {
    $ActiveNetworks[$rowNumber].RadioType = $RadioType;
    return;
    }
    $BasicRates = getValueByName $_ $basic_rates_text;
    if ($BasicRates.Length -gt 0) {
    $ActiveNetworks[$rowNumber].BasicRates = $BasicRates;
    return;
    }
    $OtherRates = getValueByName $_ $other_rates_text;
    if ($OtherRates.Length -gt 0) {
    $ActiveNetworks[$rowNumber].OtherRates = $OtherRates;
    return;
    }
    };
    if ($ActiveNetworks.Count -gt 0) {
    return $activeNetworks | Select-Object BSSID,
    Channel,
    Signal,
    Encryption,
    Authentication,
    SSID,
    RadioType,
    InterfaceName | Sort-Object Signal -Descending
    } else {
    Write-Warning "`n No active networks.`n";
    }
    #}

    
    Script test1.1
    $openwifi =  Invoke-expression 'C:\Users\profile\Desktop\test1.0.ps1' |Where-Object { $_.Authentication -eq "Open" } | Select-Object -ExpandProperty SSID
    
    $connection= netsh wlan show interfaces | select-string '\sSSID'
    
    ForEach($wifi in $openwifi){
     
            if($connection -eq $wifi){Write-host "YOU Are not secure"}
            Else {Write-Host "you are secure"}
          
           
        }
    
  • #79951

    Curtis Smith
    Participant

    Well, without running the code you found, the issue with your if statement is what you are comparing.

    Your $wifi will contain the SSID value returned from the object generated by the script.
    IE. MySSID

    but your $connection variable contains a MatchInfo object that is generated by the Select-String command

    IE.

    PS C:\> netsh wlan show interfaces | select-string '\sSSID' | Select *
    
    
    IgnoreCase : True
    LineNumber : 9
    Line       :     SSID                   : MySSID
    Filename   : InputStream
    Path       : InputStream
    Pattern    : \sSSID
    Context    :
    Matches    : { SSID}

    Now when you do the string compare of the SSID string in $wifi to the MatchInfo object in $connection, it compares the "MySSID" string value in $wifi to the .tostring() value of the MatchInfo object in $connection.

    IE.

    $wifi -eq $connection

    is the same as

    "MySSID" -eq "    SSID                   : MySSID"

    As you can see from the above this will always be false, which is why you get "you are secure" every time.

    You either need to get just the SSID you want from the MatchInfo object, or use something that can use wildcards in your if statement. It would be best to pull just the SSID from your MatchInfo rather than using wild cards to prevent false positives on similarly named SSIDs.

You must be logged in to reply to this topic.