Skip to content

Instantly share code, notes, and snippets.

@tabs-not-spaces
Last active February 25, 2021 15:39
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tabs-not-spaces/5ab2525657c578ef4683bd3c9ba5f43c to your computer and use it in GitHub Desktop.
Save tabs-not-spaces/5ab2525657c578ef4683bd3c9ba5f43c to your computer and use it in GitHub Desktop.
$script:tick = [char]0x221a
$hashPath = "$env:SystemDrive\hwhash.csv"
function Install-PreReq {
[cmdletbinding()]
param (
[Parameter(Mandatory = $true)]
[string[]]$reqs,
[Parameter(Mandatory = $false)]
[ValidateSet('Module', 'Script')]
$type = "Module"
)
foreach ($x in $reqs) {
Write-Host "Installing $type`: $x`.." -Foreground Blue -NoNewLine
if ($type -eq "Script") {
$exists = Get-Command -Name $x -errorAction SilentlyContinue
}
else {
$exists = Get-Module -Name $x -ListAvailable -errorAction SilentlyContinue
}
if (!($exists)) {
Invoke-Expression "Install-$type $x -Force -Confirm:`$false"
}
Write-Host " $script:tick" -Foreground Green
}
}
try {
Write-Host "Installing pre-reqs.." -Foreground Yellow
Find-PackageProvider -Name "Nuget" | Install-PackageProvider -Force -Confirm:$false | Out-Null
Install-PreReq -reqs "Get-WindowsAutoPilotInfo" -type "Script"
Install-PreReq -reqs "Microsoft.Graph.Intune", "WindowsAutoPilotIntune" -type "Module"
Write-Host "Grabbing device hash.." -Foreground Yellow -NoNewLine
if (!(Test-Path $hashPath)) {
$csv = Get-WindowsAutoPilotInfo
$csv | Export-Csv -Path $hashPath -NoTypeInformation -Force
}
if (Test-Path $hashPath) {
Write-Host " $script:tick" -Foreground Green
}
Write-Host "Authenticating to Azure tenant.." -Foreground Yellow -NoNewLine
$context = Connect-MSGraph
if ($context) {
Write-Host " $script:tick" -Foreground Green
Write-Host "Uploading hardware hash to tenant.." -ForegroundColor Yellow
Import-AutoPilotCSV -csvFile $hashPath
}
else {
Write-Host " X" -Foreground Red
throw "Authentication probably failed.."
}
}
catch {
$errorMsg = $_.Exception.Message
}
finally {
if ($errorMsg) {
Write-Warning $errorMsg
}
else {
Write-Host "Script completed with no obvious isssues.." -ForegroundColor Green
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment