execute .bat file on remote computer

Welcome Forums General PowerShell Q&A execute .bat file on remote computer

Viewing 2 reply threads
  • Author
    Posts
    • #222471
      Participant
      Topics: 30
      Replies: 74
      Points: 402
      Rank: Contributor

      Hi

      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: (:) [Invoke-Command], ParameterBindingException
      + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.InvokeCommandCommand
      

      Paul

       

      • This topic was modified 1 month, 1 week ago by acer460527.
    • #222768
      Participant
      Topics: 0
      Replies: 2
      Points: 12
      Rank: Member

      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

    • #222840
      Participant
      Topics: 30
      Replies: 74
      Points: 402
      Rank: Contributor

      twilightScripter thanks for your help

      it’s working now

       

Viewing 2 reply threads
  • You must be logged in to reply to this topic.