Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Run batch job with Nes-PSSession with username and password

$
0
0

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:\>

 


Viewing all articles
Browse latest Browse all 6937

Trending Articles