locked
Issue with array and variable RRS feed

  • Question

  • Unfortunately Im not so much familiar with scripting, currently Im doing a script using powershell and having an issue when check if a value declared in variable is listed in an array

    $openwifi = @()

    $openwifi +=  Invoke-expression 'C:\profile\Desktop\test1.0.ps1' |Where-Object { $_.Authentication -eq "Open" } | Select-Object SSID | ft -HideTableHeaders

    In the line above every SSID that has an Authentication as open is saved to an array. When running $openwifi they are listed as should be.

    The below line will insert the current SSID name that im connected to to variable $connection.

    $connection= netsh wlan show interfaces | select-string '\sSSID

    Here is the problem when comparing them. I tried -in,-like,-match,-contains but still is not working

    ForEach($wifi -in $openwifi){
     
            if($connection = $wifi){Write-host "YOU Are not secure"}

    Such assistance is appreciated.

    $openwifi = @()

    $openwifi +=  Invoke-expression 'C:\Users\attak026\Desktop\test1.0.ps1' |Where-Object { $_.Authentication -eq "Open" } | Select-Object SSID | ft -HideTableHeaders

    $connection= netsh wlan show interfaces | select-string '\sSSID'

    ForEach($wifi in $openwifi){
     
            if($connection = $wifi){Write-host "YOU Are not secure"}
          

    Many Thanks,


    • Edited by Kertatt Friday, September 15, 2017 5:52 AM
    Friday, September 15, 2017 5:51 AM

Answers

  • Hello,

    I have not tested that (since I don't have your test1.0.ps1), but I replaced the most obvious mistakes.

    Use comparisson operators -eq, -ne, -gt, -lt etc. Not "=". This is only for assigning values.

    Why are so many people using format-table (ft) in their code? It will destroy your data structure and is only made to produce screen output. Don't use it, if you want to work with the data later! Furthermore, it is absolutely redundant, since there are better ways to produce screen output. 

    It is not really wrong, but you don't have to declare an empty array at beginning.

    $openwifi =  Invoke-expression 'C:\Users\attak026\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"}
    }


    • Edited by hpotsirhc Friday, September 15, 2017 6:07 AM
    • Marked as answer by Kertatt Friday, September 15, 2017 5:21 PM
    Friday, September 15, 2017 6:06 AM
  • I tried the following, when I was connected to secure wifi the authentication displayed was WPA2-Personal and stated that Secure connection, but when I connected to unsecure wifi, Authentication was marked as Open and still showed Secure Connection.

    Im totally lost now :(

    function Test-WiFiSecured{
    	$props = netsh wlan show interfaces | 
    		Select-Object -skip 4 |
    		Where{$_.Trim()} |
    		ForEach-Object{ $_ -replace ':', '=' } |
    		Out-String |
    		ConvertFrom-StringData
    	$wifi = [pscustomobject]$props
    	Write-Host 'Authentication='$wifi.Authentication -fore green
    	if($wifi.Profile -eq 'Open'){
    		Write-Host 'Not secure' -fore Red
    	}else{
    		Write-Host 'Secure connection' -fore Green
    		return $true
    	}
    }
    Test-WiFiSecured


    • Marked as answer by Kertatt Saturday, September 16, 2017 12:49 PM
    Saturday, September 16, 2017 10:23 AM
  • It worked when replaced wifi.profile -eq 'Open' to wifi.authentication as per below

    function Test-WiFiSecured{
    	$props = netsh wlan show interfaces | 
    		Select-Object -skip 4 |
    		Where{$_.Trim()} |
    		ForEach-Object{ $_ -replace ':', '=' } |
    		Out-String |
    		ConvertFrom-StringData
    	$wifi = [pscustomobject]$props
    	Write-Host 'Authentication='$wifi.Authentication -fore green
    	if($wifi.Authentication -eq 'Open'){
    		Write-Host 'Not secure' -fore Red
    	}else{
    		Write-Host 'Secure connection' -fore Green
    		return $true
    	}
    }
    Test-WiFiSecured

    • Marked as answer by Kertatt Saturday, September 16, 2017 12:49 PM
    Saturday, September 16, 2017 12:49 PM

All replies

  • Your code is not even close to correct and the script file is a mystery.

    What is this line?

    $openwifi +=  Invoke-expression 'C:\profile\Desktop\test1.0.ps1' |Where-Object { $_.Authentication -eq "Open" } | Select-Object SSID | ft -HideTableHeaders

    What is in "C:\profile\Desktop\test1.0.ps1"?

    Invoke-Ecpression is not used to call a script file.  Just type the file name.

    To get a raw list of strings use: Select-Object -Expand SSID

    Like this:

    C:\profile\Desktop\test1.0.ps1 |
         Where-Object { $_.Authentication -eq "Open" } | 
         Select-Object  -Expand SSID

    Try to fix your code, post it with the code posting tool and format it so it is readable.


    \_(ツ)_/

    Friday, September 15, 2017 6:03 AM
  • Hello,

    I have not tested that (since I don't have your test1.0.ps1), but I replaced the most obvious mistakes.

    Use comparisson operators -eq, -ne, -gt, -lt etc. Not "=". This is only for assigning values.

    Why are so many people using format-table (ft) in their code? It will destroy your data structure and is only made to produce screen output. Don't use it, if you want to work with the data later! Furthermore, it is absolutely redundant, since there are better ways to produce screen output. 

    It is not really wrong, but you don't have to declare an empty array at beginning.

    $openwifi =  Invoke-expression 'C:\Users\attak026\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"}
    }


    • Edited by hpotsirhc Friday, September 15, 2017 6:07 AM
    • Marked as answer by Kertatt Friday, September 15, 2017 5:21 PM
    Friday, September 15, 2017 6:06 AM
  • Don't use Invoke-Expression.

    This gets the SID:

    if($m = netsh wlan show interfaces | select-string '^\s+SSID\s'){
    	$SSID = ($m.Line -Split ':')[1].Trim()
    	Write-Host $SSID
    }
    


    \_(ツ)_/

    Friday, September 15, 2017 6:12 AM
  • This will get all the connection properties:

    $wifi = netsh wlan show interfaces | 
    	Select-Object -skip 4 | 
    	ForEach-Object{ $_ -replace ':', '=' } | 
    	Out-String | 
    	ConvertFrom-Csv -Header Name, WiFi
    $wifi.Authentication
    $wifi.Profile


    \_(ツ)_/


    • Edited by jrv Friday, September 15, 2017 6:22 AM
    Friday, September 15, 2017 6:21 AM
  • Thanks for you feedback

    #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"; 
    }
    #}

    $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"}
          
           
        }
      
    
     
               
    
    
    
    

    , much appreciated. 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".

    Such feedback is appreciated.

    Thanks


    • Edited by Kertatt Friday, September 15, 2017 4:24 PM
    Friday, September 15, 2017 4:12 PM
  • 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.

    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"}
          
           
        }
      
    
     
               
    
    
    
    
    


    I had uploaded both script., as per above.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

    • Merged by jrv Saturday, September 16, 2017 6:36 AM DUPLICATE
    Saturday, September 16, 2017 6:24 AM
  • Try this:

    function Test-WiFiSecured{
    	$props = netsh wlan show interfaces | 
    		Select-Object -skip 4 |
    		Where{$_.Trim()} |
    		ForEach-Object{ $_ -replace ':', '=' } |
    		Out-String |
    		ConvertFrom-StringData
    	$wifi = [pscustomobject]$props
    	Write-Host 'Authentication='$wifi.Authentication -fore green
    	if($wifi.Authentication -eq 'Open'){
    		Write-Host 'Not secure' -fore Red
    	}else{
    		Write-Host 'Secure connection' -fore Green
    		return $true
    	}
    }
    Test-WiFiSecured


    \_(ツ)_/


    • Edited by jrv Saturday, September 16, 2017 7:30 PM
    Saturday, September 16, 2017 6:50 AM
  • In Windows 10 we can just do this:

    $profilename = ((Get-NetConnectionProfile).Name -split ' ')[0]
    Get-WiFiProfile -ProfileName $profilename |select Authentication


    \_(ツ)_/

    Saturday, September 16, 2017 7:26 AM
  • Actually the WiFiProfileManager works in all current versions of Windows as of this release:

    https://github.com/jcwalker/WiFiProfileManagement


    \_(ツ)_/

    Saturday, September 16, 2017 8:31 AM
  • I tried the following, when I was connected to secure wifi the authentication displayed was WPA2-Personal and stated that Secure connection, but when I connected to unsecure wifi, Authentication was marked as Open and still showed Secure Connection.

    Im totally lost now :(

    function Test-WiFiSecured{
    	$props = netsh wlan show interfaces | 
    		Select-Object -skip 4 |
    		Where{$_.Trim()} |
    		ForEach-Object{ $_ -replace ':', '=' } |
    		Out-String |
    		ConvertFrom-StringData
    	$wifi = [pscustomobject]$props
    	Write-Host 'Authentication='$wifi.Authentication -fore green
    	if($wifi.Profile -eq 'Open'){
    		Write-Host 'Not secure' -fore Red
    	}else{
    		Write-Host 'Secure connection' -fore Green
    		return $true
    	}
    }
    Test-WiFiSecured


    • Marked as answer by Kertatt Saturday, September 16, 2017 12:49 PM
    Saturday, September 16, 2017 10:23 AM
  • It worked when replaced wifi.profile -eq 'Open' to wifi.authentication as per below

    function Test-WiFiSecured{
    	$props = netsh wlan show interfaces | 
    		Select-Object -skip 4 |
    		Where{$_.Trim()} |
    		ForEach-Object{ $_ -replace ':', '=' } |
    		Out-String |
    		ConvertFrom-StringData
    	$wifi = [pscustomobject]$props
    	Write-Host 'Authentication='$wifi.Authentication -fore green
    	if($wifi.Authentication -eq 'Open'){
    		Write-Host 'Not secure' -fore Red
    	}else{
    		Write-Host 'Secure connection' -fore Green
    		return $true
    	}
    }
    Test-WiFiSecured

    • Marked as answer by Kertatt Saturday, September 16, 2017 12:49 PM
    Saturday, September 16, 2017 12:49 PM
  • Sorry.  I typed that for some reason.


    \_(ツ)_/

    Saturday, September 16, 2017 7:29 PM