Trigger a Task from an Event

or how I got ConnectWise Control to switch to single monitor when I remote connected.

I’m pretty proud of this solution that I whipped up on the fly one morning. One gripe our techs had was that when we connected in the field from a single monitor PC using Control, it always displayed all monitors as spanned and scaled, making things hard to see. At home I sometimes have to work from my 4K 70″ TV, connecting into my 3 display setup at the office, I sometimes have to break out the magnifier application or get off the couch and set a chair in front of the TV.

While taking certification tests I noticed that disconnecting another display need not be done physically (except during the test, they did require that) but it could be done with the Win + P button to change your presentation mode. If you toggle to PC screen only, the other displays are ignored, this worked on Control as well, it would revert to single display.

Now for the complex solution to a first world problem. I don’t want to have to hit that button combo when I remote connect.

When I connect it does display that I’ve connected in Event Viewer. The trouble is that it uses the same event for everything, only the details are unique.

So I first created a task using “Attach a Task to this Log…” but once I determined the built-in nature of that was no sufficient, I just built them from scratch in Task Scheduler.

Code time!
If you’re using this as a basis for your own solution, do some testing to determine what XML works for your trigger event, here is my connect task, my Control username has been replaced with “First Last” and my computer name has been replaced with “MYPC.subdomain.domain.com”

<QueryList>
  <Query Id="0" Path="Application">
    <Select Path="Application">*[System[Provider[@Name='ScreenConnect Client (0ba1111111111111)'] and (Computer='MYPC.subdomain.domain.com') and (Level=4 or Level=0) and (EventID=0)] and EventData[Data="First Last Connected"]]</Select>
  </Query>
</QueryList>

Action:

displayswitch /internal

Disconnect task:

<QueryList>
  <Query Id="0" Path="Application">
    <Select Path="Application">*[System[Provider[@Name='ScreenConnect Client (0ba1111111111111)'] and (Computer='MYPC.subdomain.domain.com') and (Level=4 or Level=0) and (EventID=0)] and EventData[Data="First Last Disconnected"]]</Select>
  </Query>
</QueryList>

Action:

displayswitch /extend

As I said before the only big downside is the need for the user to be logged in, so if your system sleeps and you use Wake action to wake it remotely, it won’t be available until after you sign-in. But once the desktop loads, just disconnect and then reconnect and it will change the screens. I generally use this during the day so my PC would be locked but not logged out.

2022-12-27 UPDATE: Screenconnect updated their client software and the event log info is now changed to the point that this would not work, they now include the version of the software in each event log message, so I could fix it for one version until they update it again. What a pain, I’d either have to create another script to update the version as it updates or trigger more generically and try to read the event with powershell. I think two scripts would be the best option, but that’s more than I want to for what is a minor annoyance. I looked a little closer and the software doesn’t even mention when the version is updated. I’d have to watch for an msi or something. Maybe I should periodically check eventlog for a regex match on the version and then write it to the scheduled task trigger. This could be another fun ChatGPT project.

Leave a comment

Design a site like this with WordPress.com
Get started