Powershell invoke命令的输出未返回

Powershell invoke命令的输出未返回,powershell,Powershell,我有以下脚本块: $scriptblock = { $regpath = "HKLM:\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp" $securitylayer = Get-ItemProperty -Path $regpath -Name SecurityLayer -ErrorAction SilentlyContinue

我有以下脚本块:

   $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."}
               }
我传递给运行Server 2003 SP2的远程计算机上的PSSession:

$computername = 'computer'
$pssession = New-PSSession -ComputerName $computername -Name $computername
Invoke-Command -Session $pssession -ScriptBlock {$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')) {
                    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

在计算机上运行时,代码按预期运行。

您正在将脚本块包装到脚本块中,因此它实际上并没有执行脚本块。只需从
{$scriptblock}
周围删除
{}

Invoke-Command -Session $pssession -ScriptBlock $scriptblock

您正在将脚本块包装到脚本块中,因此它实际上并没有执行脚本块。只需从
{$scriptblock}
周围删除
{}

Invoke-Command -Session $pssession -ScriptBlock $scriptblock