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.
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