Get-MMDRegistrationInfo.ps1

<#PSScriptInfo
 
.VERSION 1.6
 
.GUID feee5016-0fa7-479d-a48d-3ff3e824f7f5
 
.AUTHOR James Murray
 
.COMPANYNAME Microsoft
 
.COPYRIGHT
 
.TAGS Microsoft Managed Desktop
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
Version 1.0: Original published version.
Version 1.1: Updated Synopsis
Version 1.2: Fixing order of object properties
Version 1.3: Added -JSON flag to output format for services consumption
Version 1.4: Added -Ring parameter for services consumption; only compatible when used with -JSON
Version 1.5: Added support for remote computers
Version 1.6: Added Append support.
#>


<#
.SYNOPSIS
Retrieves the Device Registration details from the current computers in the format supported by Microsoft Managed Desktop
.DESCRIPTION
This script uses WMI to retrieve properties needed by Microsoft Managed Desktop for the purposes of Device Registration
.PARAMETER OutputFile
The name of the CSV file to be created with the details for the computers. If not specified, the details will be returned to the PowerShell
pipeline.
#>


[CmdletBinding()]
param(
    [Parameter(Mandatory = $False,ValueFromPipeline = $True,ValueFromPipelineByPropertyName = $True,Position = 0)][Alias("DNSHostName","ComputerName","Computer")] [String[]]$Name = @("localhost"),
    [Parameter(Mandatory = $False)] [string]$OutputFile = "",
    [Parameter(Mandatory = $False)] [switch]$JSON,
    [Parameter(Mandatory = $False)] [Switch]$Append = $false,
    [Parameter(Mandatory = $False)] [System.Management.Automation.PSCredential]$Credential = $null,
    [Parameter(Mandatory = $False)][ValidateSet('Test','First','Fast','Broad')] [string]$Ring
)

begin
{
    # Initialize empty list
    $computers = @()
    $comps = @()
    $i = 1
}

process
{
    $comps += $Name
}

end {
    Write-Host "Trying to retrieve the hash from $($comps.Count) computers."

    foreach ($comp in $comps) {
        Write-Host "Attempting to get hash from #$i out of $($comps.Count) computers. Name: $comp"
        $i++
        $bad = $false

        # Get a CIM session
        if ($comp -eq "localhost") {
            $session = New-CimSession
        }
        else
        {
            try
            {
                if ($Credential -eq $null) {
                    throw "Missing Creds"
                }
                $session = New-CimSession -ComputerName $comp -Credential $Credential -ErrorAction Stop
            }
            catch
            {
                if ($_.ToString() -eq "Missing Creds")
                {
                    Write-Host "To retrieve hashes from remote computers you must provide the Credential parameter." -ForegroundColor RED
                    Write-Verbose $_
                    break
                } else {
                    Write-Host "Failed to communicate with $comp" -ForegroundColor RED
                    Write-Verbose $_
                    continue
                }
            }

        }

        # Get the common properties.
        Write-Verbose "Checking $comp"
        $serial = (Get-CimInstance -CimSession $session -Class Win32_BIOS).SerialNumber

        # Get the hash (if available)
        try
        {
            $devDetail = (Get-CimInstance -CimSession $session -Namespace root/cimv2/mdm/dmmap -Class MDM_DevDetail_Ext01 -Filter "InstanceID='Ext' AND ParentID='./DevDetail'" -ErrorAction Stop)
        }
        catch
        {
            Write-Host "Failed to get hash from $comp. Usually due to incorrect Windows version." -ForegroundColor RED
            Write-Verbose $_
            continue
        }

        if ($devDetail -and (-not $Force))
        {
            $hash = $devDetail.DeviceHardwareData
        }
        else
        {
            $hash = ""
            $bad = $true
        }

        $cs = Get-CimInstance -CimSession $session -Class Win32_ComputerSystem
        $make = $cs.Manufacturer.Trim()
        $model = $cs.Model.Trim()

        # Create a pipeline object
        $c = New-Object psobject -Property @{
            "Manufacturer" = $make
            "Model" = $model
            "Serial Number" = $serial
            "Hardware Hash" = $hash
            "oemManufacturerName" = $make
            "modelName" = $model
            "serialNumber" = $serial
            "hardwareBlob" = $hash
            "ring" = $Ring
        }


        # Write the object to the pipeline or array
        if ($bad)
        {
            # Report an error when the hash isn't available
            #Write-Error -Message "Unable to retrieve device hardware hash from computer $comp" -Category DeviceError
        }
        elseif ($OutputFile -eq "")
        {
            $c
        }
        else
        {
            Write-Host "Successfully retrieved hash from $comp" -ForegroundColor GREEN
            $computers += $c
        }

        if ($session) { Remove-CimSession $session }
    }

    if ($OutputFile -ne "" -and $JSON)
    {
        if ($Ring -ne "") {
            $OutputObject = $computers | Select-Object "oemManufacturerName","modelName","serialNumber","hardwareBlob","ring"
        } else {
            $OutputObject = $computers | Select-Object "oemManufacturerName","modelName","serialNumber","hardwareBlob"
        }
        $JsonOutput = ConvertTo-Json @($OutputObject)
        Out-File $OutputFile -InputObject $JsonOutput
    }
    elseif ($OutputFile -ne "")
    {
        if ($Append)
        {
            if (Test-Path $OutputFile)
            {
                $computers += Import-CSV -Path $OutputFile
            }
        }
        $computers | Select-Object "Manufacturer","Model","Serial Number","Hardware Hash" | ConvertTo-Csv -NoTypeInformation | ForEach-Object { $_ -replace '"','' } | Out-File $OutputFile
    }
}