2 min read

ConfigMgr - Get Unique CN from .MIF's

ConfigMgr - Get Unique CN from .MIF's

So, i got faced with the task to get the unique ComputerName's from each .MIF from ".\Inboxes\Auth\dataldr.box\BADMIFS\MajorMismatch" directory in order to see which computers have an MajorMismatch.

This was a real bore going through them, as at this site, there were hundreds of .MIF's, and i thought i probably could script it in order to speed things up and using my time to more fun stuff.

Simple script

And, as it shows, this was possible with this quite simple script (except the RegEX part, i still have a quite hard time grasping the logic, but i got it to work and i'm getting better at it!):

$MifPath = "E:\CMINSTALLDIR\Inboxes\Auth\Dataldr.box\BADMIFS\MajorMismatch\"
$FoundUniqueCN = @()
$FoundUniqueCN += (Get-ChildItem -Path $MifPath -Recurse | Select-String -Pattern '<NetBIOS Name><+\w+>','<NetBIOS Name><+\w-+\w+>').Matches.Value | Select-Object -Unique
$FoundUniqueCN | Out-file "C:\Temp\MajorMismatch-Mif-UnqiueCN.txt"

Using the CM AdminService for a more dynamic version

But, as i needed to do this, i thought that maybe someone else could benefit from this, but in order to make it a bit more dynamic, i did some tweaking and adding in order for you to more easily use it at your site.
Using the CM AdminService you only need to supply an "DOMAIN\USERNAME", ""PASSWORD", "SITESERVERFQDN" and "OUTPUTFILEPATH".

Depending on you computer naming convention, you might want to edit this part, the RegEx pattern, here i use two different patterns as some computers have been named differently:

-Pattern '<NetBIOS Name><+\w+>','<NetBIOS Name><+\w-+\w+>'
<#
    .DESCRIPTION
        Script to collect all the unique ComputerNames from .MIF files in the "MajorMismatch" directory, eg. "E:\Program Files\Configuration manager\inboxes\auth\dataldr.box\BADMIFS\MajorMismatch"


    .REQUIREMENTS
        The account used to query the AdminService needs the following role permissions as a minimum, "Site" -> "read", in order to get the site install directory information

#>

Param(
    [Parameter(HelpMessage = 'Username of the user with sufficent rights to edit Device resources, must conaint "DOMAIN\USERNAME"', Mandatory = $True)]
    $Username,
    
    [Parameter(HelpMessage = 'Password for user account', Mandatory = $True)]
    $Password,

    [Parameter(HelpMessage = 'Output file directory, eg. c:\temp', Mandatory = $true)]
    $OutFileDir,

    [Parameter(HelpMessage = 'Your siteserver FQDN for AdminService URl, eg "cm01.corp.viamonstra.com"', Mandatory = $True)]
    [string]$SiteServerFQDN
)

# Attempt to ignore self-signed certificate binding for AdminService
# Convert encoded base64 string for ignore self-signed certificate validation functionality
$CertificationValidationCallbackEncoded = "DQAKACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAdQBzAGkAbgBnACAAUwB5AHMAdABlAG0AOwANAAoAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAB1AHMAaQBuAGcAIABTAHkAcwB0AGUAbQAuAE4AZQB0ADsADQAKACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAdQBzAGkAbgBnACAAUwB5AHMAdABlAG0ALgBOAGUAdAAuAFMAZQBjAHUAcgBpAHQAeQA7AA0ACgAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgAHUAcwBpAG4AZwAgAFMAeQBzAHQAZQBtAC4AUwBlAGMAdQByAGkAdAB5AC4AQwByAHkAcAB0AG8AZwByAGEAcABoAHkALgBYADUAMAA5AEMAZQByAHQAaQBmAGkAYwBhAHQAZQBzADsADQAKACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAcAB1AGIAbABpAGMAIABjAGwAYQBzAHMAIABTAGUAcgB2AGUAcgBDAGUAcgB0AGkAZgBpAGMAYQB0AGUAVgBhAGwAaQBkAGEAdABpAG8AbgBDAGEAbABsAGIAYQBjAGsADQAKACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAewANAAoAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgAHAAdQBiAGwAaQBjACAAcwB0AGEAdABpAGMAIAB2AG8AaQBkACAASQBnAG4AbwByAGUAKAApAA0ACgAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAewANAAoAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAaQBmACgAUwBlAHIAdgBpAGMAZQBQAG8AaQBuAHQATQBhAG4AYQBnAGUAcgAuAFMAZQByAHYAZQByAEMAZQByAHQAaQBmAGkAYwBhAHQAZQBWAGEAbABpAGQAYQB0AGkAbwBuAEMAYQBsAGwAYgBhAGMAawAgAD0APQBuAHUAbABsACkADQAKACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgAHsADQAKACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAUwBlAHIAdgBpAGMAZQBQAG8AaQBuAHQATQBhAG4AYQBnAGUAcgAuAFMAZQByAHYAZQByAEMAZQByAHQAaQBmAGkAYwBhAHQAZQBWAGEAbABpAGQAYQB0AGkAbwBuAEMAYQBsAGwAYgBhAGMAawAgACsAPQAgAA0ACgAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAZABlAGwAZQBnAGEAdABlAA0ACgAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAKAANAAoAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAATwBiAGoAZQBjAHQAIABvAGIAagAsACAADQAKACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgAFgANQAwADkAQwBlAHIAdABpAGYAaQBjAGEAdABlACAAYwBlAHIAdABpAGYAaQBjAGEAdABlACwAIAANAAoAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAWAA1ADAAOQBDAGgAYQBpAG4AIABjAGgAYQBpAG4ALAAgAA0ACgAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIABTAHMAbABQAG8AbABpAGMAeQBFAHIAcgBvAHIAcwAgAGUAcgByAG8AcgBzAA0ACgAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAKQANAAoAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgAHsADQAKACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgAHIAZQB0AHUAcgBuACAAdAByAHUAZQA7AA0ACgAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAfQA7AA0ACgAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAB9AA0ACgAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAfQANAAoAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAAgACAAIAB9AA0ACgAgACAAIAAgACAAIAAgACAA"
$CertificationValidationCallback = [Text.Encoding]::Unicode.GetString([Convert]::FromBase64String($CertificationValidationCallbackEncoded))

# Load required type definition to be able to ignore self-signed certificate to circumvent issues with AdminService running with ConfigMgr self-signed certificate binding
Add-Type -TypeDefinition $CertificationValidationCallback
[ServerCertificateValidationCallback]::Ignore()

#Create $Credentials var
$SecurePassword = ConvertTo-SecureString $Password -AsPlainText -Force 
$Credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $Username, $SecurePassword

#Get Site Installation directory
$InstallInfoURL = "https://$SiteServerFQDN/AdminService/wmi/SMS_Site"
$SiteInstallInfo = Invoke-RestMethod -Method 'GET' -Uri $InstallInfoURL -Credential $Credentials
$SiteInstalldir = $SiteInstallInfo.value | select -ExpandProperty "InstallDir"

#Set MIF inbox dir
$MifPath = "$SiteInstalldir\inboxes\auth\dataldr.box\BADMIFS\MajorMismatch"

#Get Unqiue NetBIOS names from .MIF's in "MajorMismatch"
$FoundUniqueCN = @()
$FoundUniqueCN += (Get-ChildItem -Path $MifPath -Recurse | Select-String -Pattern '<NetBIOS Name><+\w+>','<NetBIOS Name><+\w-+\w+>').Matches.Value | Select-Object -Unique
$FoundUniqueCN | Out-file "$OutFileDir\MajorMismatch-Mif-UnqiueCN.txt"