J1raya
J1raya

Reputation: 340

Output from invoke-command not returning

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.

Upvotes: 0

Views: 3164

Answers (1)

zdan
zdan

Reputation: 29470

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

Upvotes: 3

Related Questions