locked
Get a Variable value out of ScriptBlock RRS feed

  • Question

  •  
     $ScriptBlock = {
    $Registry_Key_Client = "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.0\Client"
          $ClientValue = (      (Get-ItemProperty -Path $Registry_Key_Client -Name Enabled).Enabled )
    
    
          
           If ($ClientValue -match "0")
                {
                    Write-Output "TLS 1.0 IS NOT Enabled on client"
                    $TLS_Client_Enable_Check = 0  
                }
                ELSE
                {
                       Write-Output "TLS 1.0 iS  Enabled on client"
                       $TLS_Client_Enable_Check = 1 
                }
    
                If ($ClientValue -match "0")
                {
                    Write-Output "TLS 1.0 IS NOT Enabled on Server"
                    $TLS_Server_Enable_Check = 0  
                }
                ELSE
                {
                       Write-Output "TLS 1.0 IS  Enabled on Server need immediate action" 
                       $TLS_Server_Enable_Check = 1  
                }
    
     }
     
     Invoke-Command -ComputerName MyServer -ScriptBlock $ScriptBlock
    
     Write-Output "$TLS_Client_Enable_Check"
       Write-Output "$TLS_Server_Enable_Check"

    Hi, I'm new to Powershell... I'm trying get the Variable values of ($TLS_Client_Enable_Check and $TLS_Server_Enable_Check)  from Script block which are defined in if condition??
    Friday, January 19, 2018 3:50 PM

Answers

  • You have to pass them back.

     
    $ScriptBlock = {
    	$Registry_Key_Client = "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.0\Client"
    	$ClientValue = ((Get-ItemProperty -Path $Registry_Key_Client -Name Enabled).Enabled)
    	
    	
    	
    	If ($ClientValue -eq 0) {
    		Write-Host "TLS 1.0 IS NOT Enabled on client"
    		$TLS_Client_Enable_Check = 0
    	} ELSE {
    		Write-Host "TLS 1.0 iS  Enabled on client"
    		$TLS_Client_Enable_Check = 1
    	}
    	
    	If ($ClientValue -eq 0) {
    		Write-Host "TLS 1.0 IS NOT Enabled on Server"
    		$TLS_Server_Enable_Check = 0
    	} ELSE {
    		Write-Host "TLS 1.0 IS  Enabled on Server need immediate action"
    		$TLS_Server_Enable_Check = 1
    	}
    	$TLS_Server_Enable_Check, $TLS_Client_Enable_Check
    }
    
    $TLS_Server_Enable_Check,$TLS_Client_Enable_Check = Invoke-Command -ComputerName MyServer -ScriptBlock $ScriptBlock
    
    $TLS_Client_Enable_Check
    $TLS_Server_Enable_Check
    You need to review the basics of PowerShell so you understand how values are passed and what happens when you run  a CmdLet


    \_(ツ)_/


    • Edited by jrv Friday, January 19, 2018 4:00 PM
    • Marked as answer by Jay.Y Friday, January 19, 2018 4:47 PM
    Friday, January 19, 2018 3:59 PM

All replies

  • You have to pass them back.

     
    $ScriptBlock = {
    	$Registry_Key_Client = "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.0\Client"
    	$ClientValue = ((Get-ItemProperty -Path $Registry_Key_Client -Name Enabled).Enabled)
    	
    	
    	
    	If ($ClientValue -eq 0) {
    		Write-Host "TLS 1.0 IS NOT Enabled on client"
    		$TLS_Client_Enable_Check = 0
    	} ELSE {
    		Write-Host "TLS 1.0 iS  Enabled on client"
    		$TLS_Client_Enable_Check = 1
    	}
    	
    	If ($ClientValue -eq 0) {
    		Write-Host "TLS 1.0 IS NOT Enabled on Server"
    		$TLS_Server_Enable_Check = 0
    	} ELSE {
    		Write-Host "TLS 1.0 IS  Enabled on Server need immediate action"
    		$TLS_Server_Enable_Check = 1
    	}
    	$TLS_Server_Enable_Check, $TLS_Client_Enable_Check
    }
    
    $TLS_Server_Enable_Check,$TLS_Client_Enable_Check = Invoke-Command -ComputerName MyServer -ScriptBlock $ScriptBlock
    
    $TLS_Client_Enable_Check
    $TLS_Server_Enable_Check
    You need to review the basics of PowerShell so you understand how values are passed and what happens when you run  a CmdLet


    \_(ツ)_/


    • Edited by jrv Friday, January 19, 2018 4:00 PM
    • Marked as answer by Jay.Y Friday, January 19, 2018 4:47 PM
    Friday, January 19, 2018 3:59 PM
  • I tried this it doesn't work ...
     
     $ScriptBlock = {
    $Registry_Key_Client = "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.0\Client"
          $ClientValue = (      (Get-ItemProperty -Path $Registry_Key_Client -Name Enabled).Enabled )
    
    
          
           If ($ClientValue -match "0")
                {
                    Write-Output "TLS 1.0 IS NOT Enabled on client"
                    $TLS_Client_Enable_Check = 0  
                }
                ELSE
                {
                       Write-Output "TLS 1.0 iS  Enabled on client"
                       $TLS_Client_Enable_Check = 1 
                }
    
                If ($ClientValue -match "0")
                {
                    Write-Output "TLS 1.0 IS NOT Enabled on Server"
                    $TLS_Server_Enable_Check = 0  
                }
                ELSE
                {
                       Write-Output "TLS 1.0 IS  Enabled on Server need immediate action" 
                       $TLS_Server_Enable_Check = 1  
                }
    
     }
     
    $TLS_Server_Enable_Check,$TLS_Client_Enable_Check = Invoke-Command -ComputerName PBISFXPRD022a -ScriptBlock $ScriptBlock
    
     Write-Output "$TLS_Client_Enable_Check"
       Write-Output "$TLS_Server_Enable_Check"

    Friday, January 19, 2018 4:40 PM
  • You did not use my code so, of course it won't work.  Try looking at all of the code.  All changes I made were critical and required.

    You do not need to use Write-Output and you should almost never use it.


    \_(ツ)_/


    • Edited by jrv Friday, January 19, 2018 4:44 PM
    Friday, January 19, 2018 4:43 PM
  • My Bad I missed the If statement .. It worked now...
    Friday, January 19, 2018 4:47 PM
  • Now what have you learned about PowerShell?  It was a very big lesson.  Understanding it fully will change everything about how you write scritps.


    \_(ツ)_/

    Friday, January 19, 2018 4:49 PM