Monday, February 6, 2017

02-06-2017: SCCM Powershell Tools

I'll get this out of the way right now: I think SCCM's PowerShell module is garbage.  It's slow, it doesn't work like you'd expect a lot of the time, and it's got absolutely nothing on the ease of use and functionality of something like the ActiveDirectory module.  Thankfully, WMI is still a thing, and that gives us an excellent way to interact with SCCM via PowerShell. With that in mind, here's some quick and dirty (but still useful) PowerShell functions I've put together. Most, if not all, PowerShell tools I've written for SCCM can also be done with SQL queries and commands, so if that's more your thing, have at.

Find-ClientByMac
File can be downloaded from my technet gallery here
This is a simple enough tool. It will find any clients in the SCCM database that match a "like" query against the MAC address you provide.  This is handy for finding duplicate objects with the same MAC, which can royally screw with trying to PXE boot and image a computer.

Function Find-ClientByMAC ($inputMac){
    $namespace = "root/SMS/Site_TST"
    $siteServer = "testServer"

    $inputMac = $inputMac.Replace(" ","")
   $inputMac = $inputMac.replace("-",":")
   
    if ($inputMac -notlike "*:*")
    {
        $count = 0
        while ($count -lt ($inputMac.Length - 2))
        {
            $inputMac = $inputMac.Insert(($count)+2,':')
            $count += 3
        }
        $inputMac
    }
    Get-WmiObject -Namespace $namespace -ComputerName $siteServer -class SMS_R_System -filter "MACAddresses like '%$inputMac%'"
 }


Something to keep in mind with SCCM is that it stores MAC addresses with ":" between each pair, but your computer's IPConfig command will give it to you with "-"'s.  This will replace a - with a :, and if you just feed it a straight set of text, it will insert the ":" between each pair.
This will find any computer that matches the MAC you provided and returns with a WMI object of the SMS_Device class.  If you want to delete the duplicate objects, you can simply pipe to the remove-wmiobject command to delete.

No comments:

Post a Comment