Skip to content

Allow code to detect when it is run inside a background job, as distinct from a remoting session #11293

@mklement0

Description

@mklement0

Summary of the new feature/enhancement

Behind the scenes, background jobs use the remoting infrastructure, which means that $PSSenderInfo is populated in both cases, and $Host.Name contains ServerRemoteHost.

Currently, it isn't always possible to detect when code is executing in a background job vs. when it is running in a true remoting session - but it should be possible to do so, reliably.

Proposed technical implementation details (optional)

Currently, $PSSenderInfo.ConnectionString contains the fixed string http://localhost in background jobs, whereas in WinRM-based remoting sessions the URL contains the actual host name, port number, and client PowerShell version.

However, in SSH-based remoting sessions, the same fixed string is currently used, which should be considered a bug - see #11292

Once #11292 gets fixed, testing $PSSenderInfo.ConnectionString -eq 'http://localhost' could serve as the test for detecting background jobs, but that is both obscure and technically incorrect - though it could serve as a solution if there's a backward compatibility concern over changing the value for background jobs.

Since there is no actual network connection involved in the case of background jobs (I presume), I suggest simply setting .ConnectionString to '' (empty string).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Enhancementthe issue is more of a feature request than a bugResolution-No ActivityIssue has had no activity for 6 months or moreWG-RemotingPSRP issues with any transport layer

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions