0

I have the following scriptblock:

   $scriptblock = {
                $regpath = "HKLM:\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp"
                $securitylayer = Get-ItemProperty -Path $regpath -Name SecurityLayer -ErrorAction SilentlyContinue

                If (!($securitylayer) -or ($securitylayer.securitylayer -ne '0')) {
                    Write-Host -ForegroundColor Yellow "Regkey not present or value not 0. Creating/setting to 0"
                    #Commented out for testing purposes
                    #Set-ItemProperty -Path $regpath -Name SecurityLayer -Value 0
                    }
                Else {Write-Host -ForegroundColor green "Regkey present and set to 0. Skipping."}
               }

that I pass to a PSSession on a remote machine running Server 2003 SP2:

$computername = 'computer'
$pssession = New-PSSession -ComputerName $computername -Name $computername
Invoke-Command -Session $pssession -ScriptBlock {$scriptblock}

But i don't see any output.

I've also tried

write-output

but still don't see any output.

I saw another post that suggested doing the following, however nothing was returned:

$scriptblock = {
                    $regpath = "HKLM:\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp"
                    $securitylayer = Get-ItemProperty -Path $regpath -Name SecurityLayer -ErrorAction SilentlyContinue

                    If (!($securitylayer) -or ($securitylayer.securitylayer -ne '0')) {
                    new-object pscustomobject –property @{Result = "Regkey not present or value not 0. Creating/setting to 0"}
                    #Commented out for testing purposes
                    #Set-ItemProperty -Path $regpath -Name SecurityLayer -Value 0
                        }
                    Else {new-object pscustomobject –property @{Result = "Regkey present and set to 0. Skipping."}}
               }

$computername = 'computer'
$pssession = New-PSSession -ComputerName $computername -Name $computername
$results = Invoke-Command -Session $pssession -ScriptBlock {$scriptblock}

$results.result

Code runs as expected when run on machine.

1 Answer 1

3

You are wrapping your scriptblock in a scriptblock, so it's not actually executing the script block. Just remove the {} from around the {$scriptblock}:

Invoke-Command -Session $pssession -ScriptBlock $scriptblock
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.