Dhuruvan
Dhuruvan

Reputation: 91

psexec query session isn't returning any result when run from jenkins Powershell build step

End Goal : To configure a jenkins job to run a automation script on an windows desktop app in windows active session, am using Jenkins + Psexec command to achieve this goal.

Code that i used is below :

$sessionInfo = & psexec \\$remoteMachine -s -i query session
Write-Output "session info: "+ $sessionInfo

$sessionInfo | Where-Object { $_ -match "^\s*rdp" } | ForEach-Object {
    $fields = $_ -split '\s{2,}'
    if ($fields.Length -ge 4 -and $fields[3] -eq 'Active') {
        $fields[2]
        $sessionId = $fields[2]  # Output the session ID
        Write-Output "inner loop-Session id for current open session : "+ $sessionId
    }
}

psexec \\$remoteMachine -s -i $sessionId powershell.exe -ExecutionPolicy Bypass -Command "& {C:\bat-automatic\PS_trail.ps1 -SessID '$sessionId'}"

The above script works completely fine by triggering the automation script in remote machine when i run it from the host powershell app, but when i tried the same with the Jenkins job in the powershell build step, the below command

& psexec \\$remoteMachine -s -i query session

console output of jenkins looks like below :

Connecting to <remoteMahcine1>...
Starting PSEXESVC service on <remoteMahcine1>...
Copying authentication key to <remoteMahcine1>...
Connecting with PsExec service on <remoteMahcine1>...
Starting query on <remoteMahcine1>...

 SESSIONNAME       USERNAME                 ID  STATE   TYPE        DEVICE

query exited on <remoteMahcine1> with error code 1.

so couldn't able to trigger the automation script in an active windows session. In order to achieve this, could anyone point out Where am i making mistake ?

Upvotes: 0

Views: 68

Answers (1)

Iterokun
Iterokun

Reputation: 2548

If I had to guess your Jenkins user doesn't have enough permissions. According to the doc:

A user can always query the session to which the user is currently logged on. To query other sessions, the user must have special access permission.

You can try two things:

  1. If querying sessions for the Jenkins user works then it's most likely a permissions issue
  2. Check Security logs for errors around the time when you are making a connection. It might have the information you are looking for

Upvotes: 0

Related Questions