execute .bat file on remote computer


what I want to achieve here is
connect to the remote computer

execute a bat file which contains the following code
PowerShell -Command “Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted” >> “c:\temp\StartupLog.txt” 2>&1
PowerShell c:\users\username\Documents\AdDumpTalent.ps1 >> “c:\temp\StartupLog.txt” 2>&1
showing progress by preference in the console
once finished the output file needs to be copied to my local machine

this is what I have sofar

$path =“c:\Temp”
$bat = “copy.bat”
$file =“AdDumpTalent.ps1”
$resultpath =“C:\Temp\Talent”
$destination = “c:\temp\Talent_result”
$cred = Import-Clixml -Path “${env:\userprofile}\paul.Cred”
$session = new-PSSession -ComputerName myserver.com -Credential $cred
$remotejob = Invoke-Command -session $session -ScriptBlock {
“cmd.exe /c $path$bat”

copy-item “$resultpath” -Destination “$destination” -FromSession $session
} -ArgumentList $bat -AsJob $remotejob|wait-job

Remove-PSSession -Session $session
however when I try to run this code I get this error


Invoke-Command : A positional parameter cannot be found that accepts argument ‘$null’.
At line:8 char:15

  • $remotejob = Invoke-Command -session $session -ScriptBlock {
  • CategoryInfo : InvalidArgument: (:slight_smile: [Invoke-Command], ParameterBindingException
  • FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.InvokeCommandCommand



Hi Paul,

The error your getting is because you provided an argument that’s not required. Invoke-Command -AsJob does not need a parameter to work. Just remove $remotejob from line 13 and it will run without that error.

Check out example 8 to see an example of what I mean https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/invoke-command?view=powershell-7

If you want to get the job information just run $remotejob. If you want to get the results of the job run $remotejob | Receive-Job

Hope this helps

twilightScripter thanks for your help

it’s working now