Add Export-AutopilotHash-ToEmail.ps1
This commit is contained in:
parent
94db1e4fd6
commit
94f1a88802
96
Export-AutopilotHash-ToEmail.ps1
Normal file
96
Export-AutopilotHash-ToEmail.ps1
Normal file
@ -0,0 +1,96 @@
|
||||
# Configuration
|
||||
$csvPath = "$env:TEMP\AutopilotHash.csv"
|
||||
$autopilotScript = "$env:TEMP\Get-WindowsAutopilotInfo.ps1"
|
||||
|
||||
# Microsoft 365 OAuth Configuration
|
||||
$tenantId = "YOUR_TENANT_ID"
|
||||
$clientId = "YOUR_CLIENT_ID"
|
||||
$clientSecret = "YOUR_CLIENT_SECRET" # This must be stored securely!
|
||||
$fromEmail = "ivo.oskamp@vooruit.nl"
|
||||
$toEmail = "ivo.oskamp@vooruit.nl"
|
||||
|
||||
# Retrieve the device serial number
|
||||
$serialNumber = (Get-WmiObject -Class Win32_BIOS).SerialNumber
|
||||
if (-not $serialNumber) {
|
||||
$serialNumber = "Unknown_SerialNumber"
|
||||
}
|
||||
|
||||
# Email subject including the serial number
|
||||
$subject = "Autopilot Hash Export - $serialNumber"
|
||||
$body = "See the attached CSV file containing the Autopilot Hash for device $serialNumber."
|
||||
|
||||
# Download the latest version of Get-WindowsAutopilotInfo.ps1 from Gitea
|
||||
Write-Host "Downloading Get-WindowsAutopilotInfo.ps1..."
|
||||
$downloadUrl = "https://gitea.oskamp.info/ivooskamp/Autopilot/raw/branch/main/Get-WindowsAutoPilotInfo.ps1"
|
||||
|
||||
Try {
|
||||
Invoke-WebRequest -Uri $downloadUrl -OutFile $autopilotScript -UseBasicParsing -ErrorAction Stop
|
||||
} Catch {
|
||||
Write-Host "Error: Failed to download Get-WindowsAutopilotInfo.ps1."
|
||||
Exit 1
|
||||
}
|
||||
|
||||
# Verify if the script was downloaded correctly
|
||||
if (-not (Test-Path $autopilotScript)) {
|
||||
Write-Host "Error: Get-WindowsAutopilotInfo.ps1 does not exist after download."
|
||||
Exit 1
|
||||
}
|
||||
|
||||
# Execute the script to collect the Autopilot hash
|
||||
Write-Host "Collecting the Autopilot hash..."
|
||||
Try {
|
||||
& PowerShell -ExecutionPolicy Bypass -File $autopilotScript -OutputFile $csvPath -ErrorAction Stop
|
||||
} Catch {
|
||||
Write-Host "Error retrieving the Autopilot hash: $_"
|
||||
Exit 1
|
||||
}
|
||||
|
||||
# Check if the CSV file was created
|
||||
if (-not (Test-Path $csvPath)) {
|
||||
Write-Host "Error: CSV file was not created."
|
||||
Exit 1
|
||||
}
|
||||
|
||||
# Obtain Microsoft 365 OAuth Token
|
||||
Write-Host "Retrieving Microsoft 365 OAuth token..."
|
||||
$tokenUrl = "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token"
|
||||
$tokenBody = @{
|
||||
client_id = $clientId
|
||||
scope = "https://graph.microsoft.com/.default"
|
||||
grant_type = "client_credentials"
|
||||
client_secret = $clientSecret
|
||||
}
|
||||
|
||||
$tokenResponse = Invoke-RestMethod -Method Post -Uri $tokenUrl -ContentType "application/x-www-form-urlencoded" -Body $tokenBody
|
||||
$accessToken = $tokenResponse.access_token
|
||||
|
||||
# Send email via Microsoft Graph API
|
||||
$graphUrl = "https://graph.microsoft.com/v1.0/users/$fromEmail/sendMail"
|
||||
|
||||
$emailJson = @{
|
||||
message = @{
|
||||
subject = $subject
|
||||
body = @{
|
||||
contentType = "Text"
|
||||
content = $body
|
||||
}
|
||||
toRecipients = @(@{ emailAddress = @{ address = $toEmail } })
|
||||
attachments = @(@{
|
||||
"@odata.type" = "#microsoft.graph.fileAttachment"
|
||||
name = "AutopilotHash_$serialNumber.csv"
|
||||
contentType = "text/csv"
|
||||
contentBytes = [Convert]::ToBase64String([System.IO.File]::ReadAllBytes($csvPath))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
$emailJson = $emailJson | ConvertTo-Json -Depth 10
|
||||
|
||||
Write-Host "Sending email..."
|
||||
Invoke-RestMethod -Uri $graphUrl -Headers @{Authorization = "Bearer $accessToken"; "Content-Type" = "application/json"} -Method Post -Body $emailJson
|
||||
|
||||
Write-Host "Email sent to $toEmail"
|
||||
|
||||
# Cleanup
|
||||
Remove-Item -Path $csvPath -Force
|
||||
Remove-Item -Path $autopilotScript -Force
|
Loading…
Reference in New Issue
Block a user