title: Powershell Run Bat File in Remote machine
body: <p>I modified a function I got from Microsoft forum, it purpose is to run copy a bat file to a remote machine and to run it there. I could see the file being copied over, however it seems not working when I try to call the Invoke-Command to execute the file. Any advice will be appreciated, thank you :)  </p>
  
  <pre><code>function Run-BatchFile ($computer, [string]$batLocation)
  {
  
      $sessions = New-PSSession -ComputerName $computer -Credential qa\qalab3
      Copy-Item -Path $batLocation -Destination "\\$computer\C$\MD5temp" #copy the file locally on the machine where it will be executed
      $batfilename = Split-Path -Path $batLocation -Leaf
      Invoke-Command -Session $sessions -ScriptBlock {param($batfilename) "cmd.exe /c C:\MD5temp\$batfilename" } -ArgumentList $batfilename -AsJob
       $remotejob | Wait-Job #wait for the remote job to complete     
      Remove-Item -Path "\\$computer\C$\MD5temp\$batfilename" -Force #remove the batch file from the remote machine once job done
      Remove-PSSession -Session $sessions #remove the PSSession once it is done
  }
  
  Run-BatchFile 192.168.2.207 "D:\MD5Check\test.bat" 
  </code></pre>