0

I am trying to launch Chrome in my AWS EC2 Windows Instance from my local windows machine using PowerShell.

I have tried the below commands but they haven't worked.

Invoke-Command -Session $session -ScriptBlock {Start-Process -FilePath www.google.com}

Wrote Selenium java code to launch Chrome, exported it a jar file, copied to my remote machine. I tried to execute it from my local machine using:

Invoke-Command -Session $session -ScriptBlock {java -jar launcbrowser.jar}

The jar file is executing and running the process in background, but it is not launching the browser.

henrycarteruk
  • 12,708
  • 2
  • 36
  • 40
SivaTeja
  • 378
  • 1
  • 3
  • 16
  • 1
    You can't start a GUI application through Powershell remoting. There are more then enough topics on the internet about this. One way to do this is by writing a "server" that runs under the specific user account and issuing commands to it, but through real PS remoting this is simply not possible. – bluuf Apr 20 '17 at 11:25
  • Possible duplicate of [Powershell Using Start-Process in PSSession to Open Notepad](http://stackoverflow.com/questions/18748349/powershell-using-start-process-in-pssession-to-open-notepad) – BenH Apr 20 '17 at 13:32
  • @BenH that didn't worked for me. – SivaTeja Apr 24 '17 at 07:25

1 Answers1

0
    function Start-Process-Active{
     param  (
    [System.Management.Automation.Runspaces.PSSession]$Session,
    [string]$Executable,
    [string]$Argument,
    [string]$WorkingDirectory,
    [string]$UserID,
    [switch]$Verbose = $false

)

if (($Session -eq $null) -or ($Session.Availability -ne [System.Management.Automation.Runspaces.RunspaceAvailability]::Available))
{
    $Session.Availability
    throw [System.Exception] "Session is not availabile"
}

Invoke-Command -Session $Session -ArgumentList $Executable,$Argument,$WorkingDirectory,$UserID -ScriptBlock {
    param($Executable, $Argument, $WorkingDirectory, $UserID)
    $action = New-ScheduledTaskAction -Execute $Executable -Argument $Argument -WorkingDirectory $WorkingDirectory
    $principal = New-ScheduledTaskPrincipal -userid $UserID
    $task = New-ScheduledTask -Action $action -Principal $principal
    $taskname = "_StartProcessActiveTask"
    try 
    {
        $registeredTask = Get-ScheduledTask $taskname -ErrorAction SilentlyContinue
    } 
    catch 
    {
        $registeredTask = $null
    }
    if ($registeredTask)
    {
        Unregister-ScheduledTask -InputObject $registeredTask -Confirm:$false
    }
    $registeredTask = Register-ScheduledTask $taskname -InputObject $task

    Start-ScheduledTask -InputObject $registeredTask

    Unregister-ScheduledTask -InputObject $registeredTask -Confirm:$false
}

}

SivaTeja
  • 378
  • 1
  • 3
  • 16