Get-MMDRegistrationInfo.ps1

<#PSScriptInfo
 
.VERSION 1.3
 
.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
#>


<#
.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)] [String] $OutputFile = "",
    [Parameter(Mandatory=$False)] [Switch] $JSON
)

Begin
{
    # Initialize empty list
    $computers = @()
    $Name = @('localhost')
}

Process
{
    foreach ($comp in $Name)
    {

        $session = New-CimSession
        $bad = $false

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

        # Get the hash (if available)
        $devDetail = (Get-CimInstance -CimSession $session -Namespace root/cimv2/mdm/dmmap -Class MDM_DevDetail_Ext01 -Filter "InstanceID='Ext' AND ParentID='./DevDetail'")
        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
        }


        # 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
        {
            $computers += $c
        }

        Remove-CimSession $session
    }
}

End
{
    if($OutputFile -ne "" -and $JSON)
    {
        $OutputObject = $computers | Select "oemManufacturerName","modelName","serialNumber","hardwareBlob" 
        $JsonOutput = ConvertTo-JSON @($OutputObject)
        Out-File $OutputFile -InputObject $JsonOutput
    }
    elseif ($OutputFile -ne "")
    {
        $computers | Select "Manufacturer","Model","Serial Number","Hardware Hash" | ConvertTo-CSV -NoTypeInformation | % {$_ -replace '"',''} | Out-File $OutputFile
    }
}