thanks in advance!
I found this code to bring a running process (Notepad) to Foreground
(New-Object -ComObject WScript.Shell).AppActivate((get-process notepad).MainWindowTitle)
The above only works on my Local computer, I'd like to run it on a remote computer with currently logged on user...
I also have a script that enumerates the session on a remote computer, in this example I can choose let say session 1 and it will logoff that user session.
$pc = Read-Host "Computer Name"
echo $pc
Invoke-Command -ComputerName $pc -ScriptBlock { quser } -ErrorAction SilentlyContinue
$session = Read-Host "Session ID: (enter to quit)" -ErrorAction SilentlyContinue
If (!$session)
{
echo "No Sessions found"
Exit
}
Invoke-Command -ComputerName $pc -ScriptBlock { logoff $using:session } -ErrorAction SilentlyContinue
Can I somehow do something similar, get the session ID and use it to run the top command to bring Notepad to front, or any other/easier way to accomplish it?
Thank you so much!
CodePudding user response:
Just as myself, and Bender the Greatest mention, you can take the Scheduled Task route to run it under the users context:
try {
$command = "(New-Object -ComObject WScript.Shell).AppActivate((get-process notepad).MainWindowTitle)"
$computer = $env:COMPUTERNAME
if (Test-Connection -ComputerName $Computer -Count 1 -ErrorAction "Stop") {
$username = Get-CimInstance -ClassName "Win32_ComputerSystem" -ComputerName $computer | Select-Object -ExpandProperty UserName
if (-not$Username) {
Write-Output -InputObject "No user logged into: $Computer."
Break
}
else {
Write-Verbose -Message "Current logged in user: $Username."
Invoke-Command -ScriptBlock {
$time = (Get-Date).AddMinutes(2).ToString("HH:mm")
#The name of your scheduled task.
$taskName = "MainWindow"
#Task description.
$description = "Bring notepad to front."
#Task action - what it should do.
$taskAction = New-ScheduledTaskAction -Execute 'PowerShell.exe' `
-Argument "-Command $using:command"
#Task trigger
$taskTrigger = New-ScheduledTaskTrigger -At $time -Once
#Register the scheduled task.
Register-ScheduledTask -TaskName $taskName `
-Description $description `
-Action $taskAction `
-User $using:username #| Out-Null
#Run the task
Start-ScheduledTask -TaskPath $taskName #| Out-Null
#optional timer
#Start-Sleep -Seconds 1
#Remove the newly created task since it's done running
Unregister-ScheduledTask -TaskName $taskName -Confirm:$false
} -ComputerName $computer
}
}
}
catch {
Write-Output -InputObject $_.Exception.Message
}
You may have to remove the pre-pended domain in the username for Task Scheduler to properly find the user in AD (ran into this issue before - should work though). As you can see it's a tedious process but, it can be done. The biggest hassle is trying to piece it all together as it can become a bit confusing.
If you have the time, I'd recommend turning it into a function that accepts a -Command
, and -FilePath
argument for future use as well as robustness.
I haven't tested it out, but I wouldn't see why it wouldn't work. You may have to provide administrative credentials given your environment but, when using Kerberos you should be good; as long as your session is ran as Admin.