Gather process id's and window titles from remote servers

Hi,

First time posting, so I hope I get it right!

I’ve got the following script; I just need to collect Process ID’s, Usernames, Remote Server Name and any Window Titles relating to the process. I can run this code locally and it lists all the the Window Titles, but when I try to run it remotely, it comes back with nothing.

$remoteServerName = "server.name"

$session = New-PSSession -ComputerName $remoteServerName

Invoke-Command -Session $session -ScriptBlock {
    Get-Process -IncludeUserName| Where-Object {$_.MainWindowTitle -ne ""} | Select-Object ProcessName, Id, MainWindowTitle, UserName
}

When I run the simplified code below, I get a table of processes returned from the remote server, so I think it’s a permissions thing, i.e. my instance of the code running remotely can’t see any open windows an their window titles, therefore nothing is returned. Question is, how do I overcome it?

$remoteServerName = "server.name"

$session = New-PSSession -ComputerName $remoteServerName

Invoke-Command -Session $session -ScriptBlock {
    Get-Process 
}

Thanks in advance!

When you are executing remotely that is in a separate, non-interactive session. You can see all processes show no window title over the remote execution.

$remoteServerName = "server.name"

$session = New-PSSession -ComputerName $remoteServerName

Invoke-Command -Session $session -ScriptBlock {
    Get-Process -IncludeUserName | Select-Object ProcessName, Id, MainWindowTitle, UserName
}

If you want to pull from an active session, you’ll probably need to utilize Task Scheduler. Here is what I put together to achieve your desired goal. It creates a scheduled task, runs it to export the processes with window titles, then imports the data and cleans up files/task.

$remoteServerName = "server.name"
$session = New-PSSession -ComputerName $remoteServerName

$process = Invoke-Command -Session $session -ScriptBlock {
    $scriptfile = Join-Path $env:temp processes.ps1
    $csvfile = Join-Path $env:temp processes.csv

    $script = @"
    
    `$process = Get-Process -IncludeUserName | Where-Object MainWindowTitle | Select-Object ProcessName, Id, MainWindowTitle, UserName
    
    `$process | Export-Csv "$csvfile" -NoTypeInformation
"@

    $script | Set-Content $scriptfile

    $action = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument "-Ex bypass -WindowStyle Hidden -file `"$scriptfile`""
    
    Register-ScheduledTask -TaskName ProcessWTitle -Action $action -Force -RunLevel Highest | Start-ScheduledTask
    
    $timeout = 0

    While((Get-ScheduledTask -TaskName ProcessWTitle).State -eq 'Running' -and $timeout -lt 10){
        Start-Sleep -Seconds 1
        $timeout++
    }

    try{
        Unregister-ScheduledTask -TaskName ProcessWTitle -Confirm:$False -ErrorAction Stop
    }
    catch{
        Write-Warning "Unable to unregister task 'ProcessWTitle': $($_.exception.message)"
    }

    if(Test-Path $csvfile){
        Import-Csv $csvfile
        Remove-Item $csvfile -Confirm:$false -Force
    }

    Remove-Item $scriptfile -Confirm:$false -Force
}

$process

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.