WindowsAutoPilotPartnerCenter.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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
}