Get-AutopilotCSV.ps1

<#PSScriptInfo
 
.VERSION 1.0
 
.GUID a8fa81e6-53ef-4467-9807-5963c099de5b
 
.AUTHOR Jeff Gilbert
 
.DESCRIPTION This script gathers device information for Autopilot v2 Corp Device Identifier CSV.
    It retrieves the manufacturer, model, and serial number of the device and exports this information to a CSV file.
    If no output file is specified, it defaults to a path in the Autopilot directory under the Windows system root.
 
#>
 
Param(
    [Parameter(Mandatory = $false)]
    [string]$OutputFile
)

function Get-DeviceInfo {
    Write-Host "Gathering device information for APv2 Corp Device Identifier CSV..." -ForegroundColor Cyan

    try {
        $computerInfo = Get-WmiObject -Class Win32_ComputerSystem | Select-Object -Property Manufacturer, Model
        $serialNumber = (Get-WmiObject -Class Win32_BIOS).SerialNumber
        $computerInfo | Add-Member -MemberType NoteProperty -Name SerialNumber -Value $serialNumber

        $csvPath = $OutputFile
        $computerInfo | Export-Csv -Path $csvPath -NoTypeInformation

        # Remove headers and quotes from the CSV file
        (Get-Content -Path $csvPath) | Select-Object -Skip 1 | ForEach-Object { $_ -replace '"', '' } | Set-Content -Path $csvPath

        Write-Host "Gathered details for device with serial number: $serialNumber at $csvPath" -ForegroundColor Green
    }
    catch {
        Write-Host "An error occurred: $($_.Exception.Message)" -ForegroundColor Red
    }
}

# Main logic
if (-not $OutputFile) {
    $defaultDir = Join-Path -Path (Split-Path -Path $env:SystemRoot -Parent) -ChildPath "Autopilot"
    Write-Host "No output file specified, using default path: $defaultDir" -ForegroundColor Cyan
    if (-not (Test-Path -Path $defaultDir)) {
        New-Item -ItemType Directory -Path $defaultDir -Force | Out-Null
    }
    $serialNumber = (Get-WmiObject -Class Win32_BIOS).SerialNumber
    $OutputFile = Join-Path -Path $defaultDir -ChildPath "$serialNumber.csv"
}

$directory = Split-Path -Path $OutputFile -Parent
if (-not (Test-Path -Path $directory)) {
    New-Item -ItemType Directory -Path $directory -Force | Out-Null
}

if (-not (Test-Path -Path $OutputFile)) {
    Get-DeviceInfo
} else {
    Write-Host "CSV file already exists at path: $OutputFile" -ForegroundColor Red
}

Exit