2017-02-16 35 views
0

我正在嘗試輸入新的PSSession。如果我手動運行它的每一行,但是當我將它作爲腳本的一部分運行時,出現錯誤。這些信用是有效的,看起來會話是創建的,我只是不能在腳本中輸入它。引用的行號是寫主機。即使我只是有一個賦值給一個變量,它也存在錯誤。試圖進入psssession時Powershell錯誤

$cred = New-Object Management.Automation.PSCredential ($username, $pw) 
New-PSSession -ComputerName App02 -Credential $cred 
Get-PSSession | Enter-PSSession 

Write-Host "ERROR: Partial Service Deployment. See error log file(s)" 

我得到這個作爲一個錯誤:

Cannot perform operation because operation "NewNotImplementedException at offset 63 in file:line:column :0:0 " is not implemented. At C:\DEV\Sandbox\Sandbox.ps1:29 char:14 + Write-Host "ERROR: Partial Service Deployment. See error log file(s ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotImplemented: (:) [], RuntimeException + FullyQualifiedErrorId : NotImplemented

回答

2

你不能在腳本中使用Enter-PSSession。原因:它旨在用於交互式目的。您可以使用Invoke-Command和相應的-Session參數遠程執行命令。請參閱thread

Invoke-Command示例:

$cred = New-Object Management.Automation.PSCredential ($username, $pw) 
$session = New-PSSession -ComputerName App02 -Credential $cred 
Invoke-Command -Session $session -ScriptBlock { Get-Service * } #Return all services from the remote machine 
0

Enter-PSSession只能交互式使用。對於腳本,使用Invoke-Command,並將其指向要運行命令或腳本塊的計算機。