0
votes

I am trying to enter a new PSSession. If I manually run each line it works, but when I run it as part of a script, I get an error. The creds are valid and it seems the session is created, I just cant enter it in the script. The line number referenced is the write-host. Even if I just have an assignment to a variable it errors there.

$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)"

I get this as an error:

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

2 Answers

2
votes

You can't use Enter-PSSession in a script. Reason: It is intended for interactive purpose. You can use Invoke-Command with the corresponding -Session parameter to perform commands remotely.

See also this thread.

Example of 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
votes

Enter-PSSession can only be used interactively. For scripting, use Invoke-Command, and point it at the computer you want to run the command or scriptblock on.