输出

2017-03-08 54 views
0

我有以下脚本块:输出

$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."} 
       } 

,我传递给PSSession中运行Server 2003 SP2的远程计算机上:

$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 

代码运行正常时,机器上运行。

回答

3

你包裹在一个脚本块的脚本块,所以它不是真正执行脚本块。刚刚从{$scriptblock}周围删除{}

Invoke-Command -Session $pssession -ScriptBlock $scriptblock 
+0

无法相信我忽视了,谢谢! – J1raya