Powershell Using Start-Process in PSSession to Open Notepad

后端 未结 4 544
孤独总比滥情好
孤独总比滥情好 2020-12-03 19:29

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 ru

相关标签:
4条回答
  • 2020-12-03 20:01

    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.

    0 讨论(0)
  • 2020-12-03 20:09

    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
        }
    
    }
    
    0 讨论(0)
  • 2020-12-03 20:09

    When you use New-PSSession and then RDP into that same computer, you're actually using two separate and distinct user login sessions. Therefore, the Notepad.exe process you started in the PSSession isn't visible to your RDP session (except as another running process via Task Manager or get-process).

    Once you've RDP'd into the server (after doing what you wrote in your post), start another Notepad instance from there. Then drop to PowerShell & run this: get-process -name notepad |select name,processid

    Note that there are two instances, each in a different session.

    Now open up Task Manager and look at the user sessions. Your RDP session will probably be listed as session 1.

    Now quit Notepad and run get-process again. You'll see one instance, but for session 0. That's the one you created in your remote PSSession.

    0 讨论(0)
  • 2020-12-03 20:28

    There are only 2 workarounds that I know of that can make this happen.

    1. Create a task schedule as the logged in user, with no trigger and trigger it manually.
    2. Create a service that starts the process with a duplicated token of the logged in user.

    For the task schedule way I will say that new-scheduledtask is only available in Windows 8+. For windows 7 you need to connect to the Schedule Service to create the task like this (this example also starts the task at logon);

                    $sched = new-object -ComObject("Schedule.Service")
                    $sched.connect()
                    $schedpath = $sched.getFolder("\")
                    $domain = "myDomain"
                    $user="myuser"
    
                    $domuser= "${domain}\${user}"
    
                    $task = $sched.newTask(0) # 0 - reserved for future use
                    $task.RegistrationInfo.Description = "Start My Application"
                    $task.Settings.DisallowStartIfOnBatteries=$false
                    $task.Settings.ExecutionTimeLimit="PT0S" # there's no limit
                    $task.settings.priority=0 # highest
                    $task.Settings.IdleSettings.StopOnIdleEnd=$false
                    $task.settings.StopIfGoingOnBatteries=$false
    
                    $trigger=$task.Triggers.create(9)  # 9 - at logon
                    $trigger.userid="$domuser"  # at logon
    
                    $action=$task.actions.create(0) # 0 - execute a command
                    $action.path="C:\windows\system32\cmd.exe"
                    $action.arguments='/c "c:\program files\vendor\product\executable.exe"'
                    $action.WorkingDirectory="c:\program files\vendor\product\"
    
                    $task.principal.Id="Author"
                    $task.principal.UserId="$domuser"
                    $task.principal.LogonType=3 # 3 - run only when logged on
                    $task.principal.runlevel=1 # with elevated privs
    
                    # 6 - TASK_CREATE_OR_UPDATE
                    $schedpath.RegisterTaskDefinition("MyApplication",$viztask,6,$null,$null,$null)
    
    

    Creating a service is way more complicated, so I'll only outline the calls needed to make it happen. The easy way is to use the invoke-asservice script on powershell gallery: https://www.powershellgallery.com/packages/InvokeAsSystem/1.0.0.0/Content/Invoke-AsService.ps1

    Use WTSOpenServer and WTSEnumerateSessions to get the list of sessions on the machine. You also need to use WTSQuerySessionInformation on each session to get additional information like username. Remember to free your resources using WTSFreeMemory and WTSCloseServer You'll end up with some data which looks like this (this is from the qwinsta command);

     SESSIONNAME       USERNAME                 ID  STATE   
     services                                    0  Disc
    >rdp-tcp#2         mheath                    1  Active 
     console                                     2  Conn
     rdp-tcp                                 65536  Listen
    

    Here's an SO post about getting this data; How do you retrieve a list of logged-in/connected users in .NET?

    This is where you implement your logic to determine which session to target, do you want to display it on the Active desktop regardless of how it's being presented, over RDP or on the local console? And also what will you do if there is no one logged on? (I've setup auto logon and call a lock desktop command at logon so that a logged in user is available.) You need to find the process id of a process that is running on the desktop as that user. You could go for explorer, but your machine might be Server Core, which explorer isn't running by default. Also not a good idea to target winlogon because it's running as system, or dwm as it's running as an unprivileged user. The following commands need to run in a service as they require privileges that only system services have. Use OpenProcess to get the process handle, use OpenProcessToken to get the security token of the process, duplicate the token using DuplicateTokenEx then call ``CreateProcessAsUser``` and finally Close your handles.

    The second half of this code is implemented in invoke-asservice powershell script.

    You can also use the sysinternals tool psexec, I didn't list it as a 3rd way because it just automates the process of creating a service.

    0 讨论(0)
提交回复
热议问题