How to close a ConfigMgr Console which are opened since last ‘X’ days

A friend of mine posted a query that in their customer environment, they have a good number of Remote ConfigMgr Console installed and some of the consoles are not closed since last 1 month and more. Hence as a best practice, they wanted to close the ConfigMgr console which are opened than ‘X’ days.

Although there is a console view in latest version of ConfigMgr server called ‘Console Connections’, but it doesn’t allow to close any remote console.

Hence, to achieve the goal to close all console which are opened than X days, we need the following:

  1. A ConfigMgr Collection about all the machine where ConfigMgr console is installed.
  2. CMPivot magic to get the machine details where ConfigMgr console opened since last X days.
  3. PowerShell Script to close the console remotely.
  4. Use PS Script in CMPivot output.

So, here is the details about all the steps:

  1. ConfigMgr Console Collection:

We can use the following WQL query to create a dynamic collection about all the machine where ConfigMgr Console is installed:

select *  from  SMS_R_System inner join SMS_G_System_SERVICE on SMS_G_System_SERVICE.ResourceID = SMS_R_System.ResourceId inner join SMS_G_System_INSTALLED_SOFTWARE on SMS_G_System_INSTALLED_SOFTWARE.ResourceID = SMS_R_System.ResourceId where SMS_G_System_INSTALLED_SOFTWARE.ProductName like “%Configuration Manager Console%”

The output of the query looks like:

A screenshot of a cell phone

Description automatically generated
All Machines with ConfigMgr Console Installed
  1. CMPivot Magic

Now that we have a collection ready, we can use CMPivot against that collection. Here is the way how we can use this:

A screenshot of a cell phone

Description automatically generated
Starting CMPivot

Now the CMPivot is ready to run against this collection:

A screenshot of a cell phone

Description automatically generated
CMPivot against a Collection

The Kusto query which will help us to get the ConfigMgr Console opened is:

Process | where Name == ‘Microsoft.ConfigurationManagement.exe’ 

Here is the screenshot of it:

A screenshot of a social media post

Description automatically generated
Machines with ConfigMgr Console

Let’s verify the PID from the machine:

Viksahay-ConfigMgr’ machine :

A screenshot of a cell phone

Description automatically generated
Server 1 with ConfigMgr Console Installed

‘ConfigMgr-SQL’ Machine:

A picture containing screenshot

Description automatically generated
Server 2 with ConfigMgr Console Installed

Now the important point to note is the to find the ConfigMgr Console opened and older than X days, so the ‘CreationDate’ column of the above output will give me the desired value.

So, lets me try to create a Kusto query of machine having ConfigMgr Console opened and older than 7 days (you can use the day as per your own requirement). The query would be:

Process | where Name == ‘Microsoft.ConfigurationManagement.exe’  and CreationDate <= ago(7d)

The output should ideally give me only 1 machine  (Viksahay-ConfigMgr) as it was opened since 7th Feb 2020. So here is how the output look like:

A screenshot of a cell phone

Description automatically generated
Server with ConfigMgr Console opened since last 1 week

Now , as we got all the ConfigMgr console opened since last 1 week, so let’s take an action to close them. To achieve it, we will create a ‘PS Script’ and will use the ‘Scripts’ feature of ConfigMgr.

  1. Creating Script in ConfigMgr:

Here is how we can create a script to kill a ConfigMgr Console process:

  1. Create script in ConfigMgr console:

ConfigMgr Console | Software Library | Scripts | Right Click and choose ‘Create Script’

Here is how the wizard look like:

A screenshot of a cell phone

Description automatically generated
PS Script to stop a process

We will use the simple PS command to close a ConfigMgr console process. The command is:

Stop-Process -Name “Microsoft.ConfigurationManagement” -Force

Follow the wizard and complete it.

  1. Approve the script:

A script in ConfigMgr can only be used, once its approved. Here is the way how you can approve the script:

A screenshot of a social media post

Description automatically generated
Approve/Deny a Script in ConfigMgr Console
  1. If the ‘Approve/Deny’ option is grayed out, adjust the following ‘Hierarchy setting’ appropriately:
A screenshot of a cell phone

Description automatically generated
Hierarchy Setting in ConfigMgr Console

d)  Now the script is ready to be used in CMPivot Result.

4) Use PS Script in CMPivot output.

  1. As the output of the CMPivot was looking like
A screenshot of a cell phone

Description automatically generated
CMPivot Output

Note: As I have only 1 machine matching the criteria, hence I am only selecting one. If you have multiple machine the criteria, do a  multi select.

  1. Select the machine(s) | Right Click and Choose ‘Run Script’
A screenshot of a social media post

Description automatically generated
Running a PS Script against CMPivot Output
  1. Choose the appropriate PS Script which you created in last step.
A screenshot of a cell phone

Description automatically generated
Select the appropriate script
  1. Follow the wizard and make sure that it gets complete successfully.
  2. You can also monitor the running of the script from:

Monitoring | Client Operations.

  1. Monitor the script execution statistics under: Monitoring / Script Status

You can see the Script Execution State, the Exit Code and the Output

On Client Machine:

Script download location: C:\Windows\CCM\ScriptStore

Logs location: C:\Windows\CCM\logs\Scipts.log

Hope this will help you close all the ConfigMgr console opened since last X days. Although I just demonstrated it for ConfigMgr Console, you can use this solution for any other Remote Console installed and opened since last X days.

Leave a Reply