Launching a process in user’s session from a service

后端 未结 5 1714
一个人的身影
一个人的身影 2020-11-28 08:07

In Windows Vista/7/2008/2008R2, is it at all possible to launch a process in a user\'s session from a service? Specifically, the local session would be most useful.

5条回答
  •  情歌与酒
    2020-11-28 08:13

    If it helps, i was faced with a similar problem, but wanted a pure powershell solution.

    I cobbled together bits from other websites and came up with this:

    function Invoke-CommandInSession 
    {
        [CmdletBinding()]
        param(
            [Parameter(Mandatory = $true)]
            [ValidateNotNullOrEmpty()]
            [ScriptBlock] $expression
        )
    
        $commandLine = “powershell“
    
        ## Convert the command into an encoded command for PowerShell
        $commandBytes = [System.Text.Encoding]::Unicode.GetBytes($expression)
        $encodedCommand = [Convert]::ToBase64String($commandBytes)
        $args = “-Output XML -EncodedCommand $encodedCommand”
    
    
        $action = New-ScheduledTaskAction -Execute $commandLine -Argument $args
        $setting = New-ScheduledTaskSettingsSet -DeleteExpiredTaskAfter ([Timespan]::Zero)
        $trigger = New-ScheduledTaskTrigger -Once -At ((Get-Date) + ([Timespan]::FromSeconds(5)))
        $task = New-ScheduledTask -Action $action -Trigger $trigger -Settings $setting
        Register-ScheduledTask "Invoke-CommandInSession - $([Guid]::NewGuid())" -InputObject $task
    }
    

    Yes, its a little weird, but it works - and most importantly you can call it from within ps remoting

提交回复
热议问题