PoSh 2.0 testing zone2 in IE

Tagged: , ,

This topic contains 4 replies, has 2 voices, and was last updated by Profile photo of BrandonB BrandonB 5 months, 3 weeks ago.

  • Author
    Posts
  • #54765
    Profile photo of BrandonB
    BrandonB
    Participant

    Trying to return pass/fail requirements based on finding in reg key.

    Although sites are indeed in zone2 for trusted sites in IE this aspect of script is still faililng.

    Essentially what I'm trying to mimic in powershell is the system requirements test this bank utilizes:

    Link at bottom of page here.

    I was advised in this thread to grab my data first then output to powershell. There's a link to Padlet to display the look/feel I'm going for just having some trouble with the scripts shown. They're failing when they shouldn't be.

    I've tried getting everything in functions as described here as well.

    If I can remedy this that will fix 3 of 6 errors I'm getting on false fail responses.

    #Checking Internet Trusted Websites
    $global:boolResult
    function CheckTrustedWebsite {
    	param (
    		[String] $siteName
    	)
    	
    	$trustedSites = $(get-item "HKLM:\SOFTWARE\Policies\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMapKey").property
    	foreach ($site in $trustedSites) {
    		if ($site -like $siteName) {
    			$boolResult = $true
    		} else {
    			$boolResult = $false
    		}
    	}	
    }
    	
    CheckTrustedWebsite "*dpm.google.com*"
    if ($boolResult) {
    	$status += 'pass'
    } else {
    	$status += 'fail'
    }
    $systemAttrs += ("Trusted Website Check: http://dpm.google.com/wddl") 
    
    CheckTrustedWebsite "*express.google.com*"
    if ($boolResult) {
    	$status += 'pass'
    } else {
    	$status += 'fail'
    }
    $systemAttrs += ("Trusted Website Check: http://express.google.com") 
    
    CheckTrustedWebsite "*direct.google.com*"
    if ($boolResult) {
    	$status += 'pass'
    } else {
    	$status += 'fail'
    }
    $systemAttrs += ("Trusted Website Check: http://direct.google.com") 
    
    CheckTrustedWebsite
  • #54784
    Profile photo of Matt Bloomfield
    Matt Bloomfield
    Participant

    You will need to provide more details about what's not working. Are you getting an error? If so, what's the error?

  • #54802
    Profile photo of BrandonB
    BrandonB
    Participant

    Please pardon my not putting what should have been logical information in initial thread regarding errors.

    I went ahead an added below the systemAttrs variable just trying to get a different output in the interim.

    $systemAttrs += ("Trusted Website Check: http://direct.google.com") 
    
    foreach ($site in $trustedSites) {
        Write-Output $boolResult
        }
    
    CheckTrustedWebsite
    

    What it appears to doing is either spitting my code back at me if I try to run the entire code by hitting the play button in the ise.

    Then if I select just the function CheckTrustedWebsite and run by tapping F8 this outputs:

    PS C:\Users\User\Desktop> CheckTrustedWebsite

    It's not an error per se but it's not behaving as I'd anticipate by passing pass or fail based on the requirements.

    • This reply was modified 5 months, 3 weeks ago by Profile photo of BrandonB BrandonB.
  • #54808
    Profile photo of Matt Bloomfield
    Matt Bloomfield
    Participant

    This looks like a problem with variable scope. You're using $global:boolResult at the top of your script but not specifying $global in your function(s). This means that you're modifying the boolResult variable only in the local scope. $global:boolResult does not have a value (and never gets one) so you have no output. The quick fix is to ensure you always reference $global:boolResult.

    Here's a quick example that shows how the local and global scopes are different:

    $global:test = 'i am global'
    
    function foo {
    
        $test = 'i am local'
        Write-Output $test
        Write-Output $global:test
    
    }
    
    foo
    

    Generally, it's not considered a best practice to use global variables unless it cannot be avoided. There's probably better way to do this that doesn't use global variables but I'm running out of lunchtime...

  • #54809
    Profile photo of BrandonB
    BrandonB
    Participant

    Ah wonderful thank you! Can't believe I'd missed that one... familiar with the scope just wasn't too familiar on how it operated here. Doesn't make sense to set a global variable and not utilize it as such... Will get that fixed and report back. Thank you kindly again for the help!

    Please go finish your lunch! Don't work on it!

You must be logged in to reply to this topic.