Greetings fellow IT geeks. Today’s post is centered around PowerShell and a recent request we had from a client.
So like many companies out there, there can be many computers in your SCCM database and Active Directory that are one or more of the following:
Stale AD Object
Non standard computer name (likely meaning it wasn’t built with your awesome automation)
Sitting on the Computers container in AD (also likely meaning it wasn’t built with your awesome automation)
Inactive or non-existent SCCM client
When looking through your SCCM database or reviewing AD Users and Computers it’s not readily apparent which computers are still legitimate and should be being managed by SCCM. I have written PowerShell scripts in the past that review AD on a schedule and take action like moving an object into a “Stale” OU for future deletion, or simply emailing the list of stale machines to an Admin to take action. Today’s script is a seek and report script only but could easily be modified to take any actions you wish once you find the machines that are not being managed.
I created a PowerShell script that will look at both AD and SCCM to find machines we are deeming “unmanaged”, gather additional data about the computer, including trying to find out who the last logged on user is and if they are actively logged on, formatting the data and dumping it into 2 difference CSV files. All objects are crossed referenced from AD and SCCM to gather as much data as we can. Lastly it generates an HTML report with 2 tables containing said data, then emails this report along with the CSV attachments to the person or DL you wish to notify.
Note: This script was written to run on the SCCM Primary Site server however it could be run remotely using a PSSession, or on a machine that has the SCCM Admin console installed (not tested but should work).
The first thing you will need to do is create a new collection if you don’t already have one that contains all SCCM Computer objects that do not have a client. Hopefully you already have one as you continue to strive for 100% client saturation, but if not, here’s a Collection query you can use:
select SMS_R_SYSTEM.ResourceID,SMS_R_SYSTEM.ResourceType,SMS_R_SYSTEM.Name,SMS_R_SYSTEM.SMSUniqueIdentifier,SMS_R_SYSTEM.ResourceDomainORWorkgroup,SMS_R_SYSTEM.Client from SMS_R_System where SMS_R_System.Client != "1" or SMS_R_System.Client is null and SMS_R_System.OperatingSystemNameandVersion like "%NT%"
The last line of that query is useful when you have non-Windows OS devices like Intune MDM objects. 🙂
Once you have your Collection you will need the Collection ID as a script parameter so make note of it. You can simply go to the properties of the collection and its right there on the General tab.
Next, save the content at the bottom of this post as a PowerShell script then execute it with the required parameters using an account that has at least Read permissions to SCCM Computer objects, and local Admin permissions to the remote computers/servers. * The admin permissions are used when trying to connect to the remote machine to find the last logged on user information.
Here’s what an example of the report looks like. As mentioned above we have split the report in two. One table/CSV for Stale unmanaged objects, and one for Recent unmanaged objects. Basically just the two sides of the number of days you choose to look for.
The Stale table:
And underneath that the Recent table:
We have found this to be incredibly helpful in tracking down machines that should be managed and are not, and machines that need to be cleaned up in AD and/or SCCM.
As always you should run this script first in a lab environment before executing in production. And as always, no warranty is granted for the outcome of this script. Use at your own will. 🙂
Without further ado, here’s the PowerShell. If you grab this script and make it better (there’s always a better way) then please drop me a line at email@example.com. Would love to see any evolutions!
Emails report of unmanaged devices with CSV attachments.
Pulls all devices from AD that have not changed their password in X days.
Pulls all SCCM unmanaged devices.
Combines this data and checks for last logon user, then pulls the following information into a report:
Computer | InSCCM | Client | OU | Last SCCM Activity | Last AD Logon | User | Time | Currently Logged in | Operating System | Additional Info
Designed to run from the SCCM Server
Account running must have permissions to read CM Objects
Account running must have permissions to read AD Computer objects and atrributes
Mandatory string parameter; The number of days since machines in AD have changed their password (Default AD policy is every 30 days. Recommend this parameter be 90 days).
Mandatory string parameter; Your internal domain suffix. (Example: contoso.com)
Mandatory string parameter; The ID of a collection you create that holds devices that have no client
- Collection Query to use: select SMS_R_SYSTEM.ResourceID,SMS_R_SYSTEM.ResourceType,SMS_R_SYSTEM.Name,
SMS_R_SYSTEM.Client from SMS_R_System where SMS_R_System.Client != "1"
or SMS_R_System.Client is null and SMS_R_System.OperatingSystemNameandVersion like "%NT%"
Mandatory string parameter; The FQDN of your SMTP server
Mandatory string parameter; The email address or DL you want to sent the report to
Mandatory string parameter; The email address you want to send the report from
.EXAMPLE (Office 365 Exchange Online)
.\Get-UnmanagedDevices-Report.ps1 -DaysOld 90 -DomainSuffix contoso.com -StaleCollectionID P0120304 -SMTPServer contoso-com01c.mail.protection.outlook.com -SMTPTo DL-Notifications@contoso.com -SMTPFrom Automation@contoso.com
.EXAMPLE (On premises Exchange)
.\Get-UnmanagedDevices-Report.ps1 -DaysOld 90 -DomainSuffix contoso.com -StaleCollectionID P0120304 -SMTPServer smtp.contoso.com -SMTPTo DL-Notifications@contoso.com -SMTPFrom Automation@contoso.com
This script is written to email using SMTP without a named account. You will need to adjust if you need to send from a mailbox.
Written by William Bracken (Borrowed parts of other scripts of course)
Model Technology Solutions
Version 1.0 - 01/18/2017
Version 1.1 - 01/25/2017
Partner – Model Technology Solutions
William is an experienced and results-driven IT geek who is passionate about the “automation of things,” with an extensive background in systems management, advanced OS deployment automation, and overall infrastructure automation. He has more than 19 years of experience in IT, and has designed and implemented management solutions that have dramatically reduced support costs and ultimately brought consistent and well managed operating environments to organizations across the US.