WindowsAutoPilotPartnerCenter.psm1

function Connect-AutopilotPartnerCenter {
    <#
    .SYNOPSIS
    Authenticates to Azure AD and Partner Center.
     
    .DESCRIPTION
    The Connect-AutopilotPartnerCenter function authenticates to Azure AD and Partner Center using the PartnerCenter PoweShell module. The settings to use are read from the PartnerCenter.xml file, which must be updated to reflect your partner details.
     
    .EXAMPLE
    Connect to Azure AD to access the Partner Center REST API
     
    Connect-AutopilotPartnerCenter
     
    #>


    # Get the configuration
    [xml] $settings = Get-Content "$PSScriptRoot\PartnerCenter.xml"
    
    Import-Module PartnerCenter

    # Create a credential for the app
    $appSecret = $settings.Settings.AppSecret | ConvertTo-SecureString -AsPlainText -Force
    $credential = New-Object System.Management.Automation.PSCredential ($settings.Settings.AppID, $appSecret)

    # Get an access token for Partner Center
    $token = New-PartnerAccessToken -Environment AzureCloud -Scopes "$($settings.Settings.Resource)/user_impersonation" -ServicePrincipal -Credential $credential -Tenant $settings.Settings.PartnerTenantID -ApplicationId $settings.Settings.AppID -UseAuthorizationCode

    # Now connect to Partner Center
    Connect-PartnerCenter -Environment AzureCloud -AccessToken $token.AccessToken
}


Function Import-AutoPilotPartnerCenterCSV() {
    <#
    .SYNOPSIS
    Adds a batch of new devices into Windows Autopilot.
     
    .DESCRIPTION
    The Import-AutoPilotPartnerCenterCSV cmdlet processes a list of new devices (contained in a CSV file) using the PartnerCenter cmdlets. It is a convenient wrapper to handle the details. After the devices have been added, the cmdlet will continue to check the status of the import process. Once all devices have been processed (successfully or not) the cmdlet will complete. This can take several minutes, as the devices are processed by Intune as a background batch process.
     
    .PARAMETER csvFile
    The file containing the list of devices to be added.
     
    .PARAMETER $CustomerID
    The identifier of the customer, as indicated by the Get-PartnerCustomer cmdlet.
 
    .PARAMETER $BatchID
    The group tag (batch name or order ID) value that should be assigned to all devices being imported.
     
    .EXAMPLE
    Add a batch of devices to Windows Autopilot for the current Azure AD tenant.
     
    Import-AutoPilotPartnerCenterCSV -csvFile C:\Devices.csv -CustomerID '00000000-60de-432e-a3ec-20bcc5b26ec2' -BatchID 'My Group'
    #>

        [cmdletbinding()]
        param
        (
            [Parameter(Mandatory=$true)] $csvFile,
            [Parameter(Mandatory=$true, ValueFromPipeline=$true)] [String] $CustomerID,
            [Parameter(Mandatory=$true)] [String] $BatchID
        )
        
        # Read CSV and create a list of devices to add
        $devices = Import-CSV -Path $csvFile
        $deviceList = @()
        foreach ($device in $devices) {

        $d = New-Object -TypeName Microsoft.Store.PartnerCenter.PowerShell.Models.DevicesDeployment.PSDevice
        $d.HardwareHash = $device.'Hardware Hash'
        $d.ProductKey = $device.'Windows Product ID'
        $d.SerialNumber = $device.'Device Serial Number'
        $d.ModelName = $device.'Device model'
        $d.OemManufacturerName = $device.'Manufacturer name'
        
        $deviceList += $d
        }

    # Add the devices
    $results = New-PartnerCustomerDeviceBatch -BatchId $BatchID -CustomerID $CustomerID -Devices $deviceList

    # Report statistics
    $success = 0
    $failure = 0
    $Results.DevicesStatus | % {
        if ($_.ErrorCode -eq 0) {
            $success++
        }
        else
        {
            $failure++
        }
    }
    Write-Host "Batch processed."
    Write-Host "Devices successfully added = $success"
    Write-Host "Devices not added due to errors = $failure"

    # Return the list of results
    $Results.DevicesStatus
}