Get-WindowsAutoPilotInfo.ps1

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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<#PSScriptInfo
 
.VERSION 1.1
 
.GUID ebf446a3-3362-4774-83c0-b7299410b63f
 
.AUTHOR Michael Niehaus
 
.COMPANYNAME Microsoft
 
.COPYRIGHT
 
.TAGS Windows AutoPilot
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
Version 1.0: Original published version.
Version 1.1: Added -Append switch.
 
#>


<#
.SYNOPSIS
Retrieves the Windows AutoPilot deployment details from one or more computers
.DESCRIPTION
This script uses WMI to retrieve properties needed by the Microsoft Store for Business to support Windows AutoPilot deployment.
.PARAMETER Name
The names of the computers. These can be provided via the pipeline (property name Name or one of the available aliases, DNSHostName, ComputerName, and Computer).
.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.
.PARAMETER Append
Switch to specify that new computer details should be appended to the specified output file, instead of overwriting the existing file.
.EXAMPLE
.\Get-WindowsAutoPilotInfo.ps1 -ComputerName MYCOMPUTER -OutputFile .\MyComputer.csv
.EXAMPLE
.\Get-WindowsAutoPilotInfo.ps1 -ComputerName MYCOMPUTER -OutputFile .\MyComputer.csv -Append
.EXAMPLE
.\Get-WindowsAutoPilotInfo.ps1 -ComputerName MYCOMPUTER1,MYCOMPUTER2 -OutputFile .\MyComputers.csv
.EXAMPLE
Get-ADComputer -Filter * | .\GetWindowsAutoPilotInfo.ps1 -OutputFile .\MyComputers.csv
.EXAMPLE
Get-CMCollectionMember -CollectionName "All Systems" | .\GetWindowsAutoPilotInfo.ps1 -OutputFile .\MyComputers.csv
 
#>


[CmdletBinding()]
param(
[Parameter(Mandatory=$False,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True,Position=0)][alias("DNSHostName","ComputerName","Computer")] [String[]] $Name = @($env:ComputerName),
[Parameter(Mandatory=$False)] [String] $OutputFile = "", 
[Parameter(Mandatory=$False)] [Switch] $Append = $False
)

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

Process
{
foreach ($comp in $Name)
{
# Get the properties. At least serial number and hash are needed.
Write-Verbose "Checking $comp"
$serial = (Get-WmiObject -ComputerName $comp -class Win32_BIOS).SerialNumber
$licenseProduct = (Get-WmiObject -ComputerName $comp -class SoftwareLicensingProduct -Filter "ProductKeyChannel!=NULL and LicenseDependsOn=NULL AND ApplicationID='55c92734-d682-4d71-983e-d6ec3f16059f'")
if ($licenseProduct)
{
$product = $licenseProduct.ProductKeyID2.Substring(0,17).Replace("-","").TrimStart("0")
}
else
{
$product = ""
}
$devDetail = (Get-WMIObject -ComputerName $comp -Namespace root/cimv2/mdm/dmmap -class MDM_DevDetail_Ext01 -filter "InstanceID='Ext' AND ParentID='./DevDetail'")
if ($devDetail)
{
$hash = $devDetail.DeviceHardwareData

# Create a pipeline object
$c = New-Object psobject -Property @{
"Device Serial Number" = $serial
"Windows Product ID" = $product
"Hardware Hash" = $hash
}

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

}
}

End
{
if ($OutputFile -ne "")
{
if ($Append)
{
if (Test-Path $OutputFile)
{
$computers += Import-CSV -Path $OutputFile
}
}
$computers | Select "Device Serial Number", "Windows Product ID", "Hardware Hash" | ConvertTo-CSV -NoTypeInformation | % {$_ -replace '"',''} | Out-File $OutputFile
}
}