I need to run a batch jobs from 1 server so i made the following:
$pw=convertto-securestring-AsPlainText-Force-StringPassword $cred=New-ObjectSystem.Management.Automation.PsCredential("Domain\user",$pw) $s=New-PSSession-computerNamedasamsitpac2-credential$cred Invoke-Command-Session$s-Scriptblock {powershell.exe E:\script.ps1} Remove-PSSession$s If i run the first 3 lines and than the last 2 lines it works ok. If i run the total script the Invoke-command is part of the first 3 lines. See the following.
PS U:\> $pw=convertto-securestring-AsPlainText-Force-StringPassword $cred=New-ObjectSystem.Management.Automation.PsCredential("Domain\user",$pw) $s=New-PSSession-computerNamedasamsitpac2-credential$cred Invoke-Command-Session$s-Scriptblock {powershell.exe E:\script.ps1} Remove-PSSession$s PS U:\>