Search code examples
powershellbatch-fileinvoke-command

Execute bat file to remote client


I am trying to open an HTML file to a client "ClientA" that is locally copied to a folder "C:\1.html" remotely by executing the bellow powershell command

$Username = 'username'
$Password = 'password'
$pass = ConvertTo-SecureString -AsPlainText $Password -Force
$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList                 
$Username,$pass


Invoke-Command -ComputerName ClientA -ScriptBlock { Invoke-Expression - 
Command:"cmd.exe /c 'C:\1.bat'" } -credential $Cred

The "1.bat" batch has the bellow lines

START iexplore -k "C:\1.html"

I get no error on execution... but the file is not opening to the remote client! Any ideas?

Thanks!


Solution

  • The script is correct, no errors within, but probably it is not doing what you believe it does.

    When invoking a remote administration session powershell is working on the remote computer with its own session, which does not have desktop interaction so you are not launching internet explorer in the main shell from a session that an eventual user could have left logged in.

    To double check that your code is working you can use the following content in the 1.bat

    START iexplore -k "C:\1.html"
    echo %PATH% >> c:\patlog.txt
    echo "DONE" >> c:\1_log.txt
    

    You will see the echo from the path and from the final "DONE" on your console as confirm that the bat has been executed.

    Internet Explorer anyway is not going anyway to show up in any logged user session, as it is also discussed at this page:

    https://serverfault.com/questions/690852/use-powershell-to-start-a-gui-program-on-a-remote-machine