Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell Using Start-Process in PSSession to Open Notepad

I've created a pssession on a remote computer and entered that possession. From within that session I use start-process to start notepad. I can confirm that notepad is running with the get-process command, and also with taskmgr in the remote computer. However, the GUI side of the process isn't showing. This is the sequence I've been using:

$server = New-PSSession -ComputerName myserver -Credential mycreds
Enter-PSSession $server

[$server]: PS C:\>Start-Process notepad -Wait -WindowStyle Maximized

The process is running, but while RDP'd to the box, notepad does not open. If I open notepad from the server, a new notepad process begins. I also tried by using the verb parameter like this:

[$server]: PS C:\>Start-Process notepad -Wait -WindowStyle Maximized -Verb Open

Same result tho... Process starts, but no notepad shows. I've tried this while remoted into the box (but issued from my local host) as well as before remoting into the server.

like image 411
Colyn1337 Avatar asked Sep 11 '13 18:09

Colyn1337


People also ask

How do I open Notepad in PowerShell?

For example, just type notepad.exe in PowerShell console and it will open the program but type wordpad.exe there will be an error. wordpad.exe − The term 'wordpad.exe' is not recognized as the name of a cmdlet, function, script file, or operable program.

How do I open Notepad on a remote computer?

On Windows a remote process can be started using psexec and powershell, for example: OS-COMMAND ("c:\mstools\pstools\psexec \\<server-name> -u domain\username -p <password> -d -i cmd /c notepad.exe"). This will run notepad.exe on the remote server defined in within OS-COMMAND.

What is the PowerShell command enter-PSSession used for?

Description. The Enter-PSSession cmdlet starts an interactive session with a single remote computer. During the session, the commands that you type run on the remote computer, just as if you were typing directly on the remote computer.

How do I start a PowerShell remote computer process?

Use the Enter-PSSession cmdlet to start an interactive session with a remote computer. You can use the ComputerName parameter of Enter-PSSession to establish a temporary connection for the interactive session. Or, you can use the Session parameter to run the interactive session in a PowerShell session (PSSession).


2 Answers

That is because your powershell session on the remote machine does not go to any visible desktop, but to an invisible system desktop. The receiving end of your powershell remote session is a Windows service. The process is started, but nor you nor anyone else can ever see it.

And if you think about it, since multiple users could RDP to the same machine, there is really no reason to assume a remote powershell session would end up showing on any of the users desktops. Actually, in almost all cases you wouldn't want it anyway.

psexec with the -i parameter is able to do what you want, but you have to specify which of the sessions (users) you want it to show up in.

like image 156
Lars Truijens Avatar answered Nov 11 '22 15:11

Lars Truijens


I know this is old, but I came across it looking for the solution myself so I wanted to update it for future poor souls.

A native workaround for this problem is to use a scheduled task. That will use the active session

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

    )

    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
    }

}
like image 25
Ben Avatar answered Nov 11 '22 13:11

Ben