Public/Get-OpenIntuneBaseline.ps1
|
function Get-OpenIntuneBaseline { <# .SYNOPSIS Downloads OpenIntuneBaseline repository from GitHub .DESCRIPTION Downloads and extracts the OpenIntuneBaseline repository containing all baseline policies .PARAMETER RepoUrl GitHub repository URL (default: https://github.com/jorgeasaurus/OpenIntuneBaseline) .PARAMETER Branch Branch to download (default: main) .PARAMETER DestinationPath Path to extract the repository (default: temp directory) .EXAMPLE Get-OpenIntuneBaseline -DestinationPath ./Baselines #> [CmdletBinding(SupportsShouldProcess)] param( [Parameter()] [string]$RepoUrl = "https://github.com/jorgeasaurus/OpenIntuneBaseline", [Parameter()] [string]$Branch = "main", [Parameter()] [string]$DestinationPath ) if (-not $DestinationPath) { $DestinationPath = Join-Path -Path ([System.IO.Path]::GetTempPath()) -ChildPath "OpenIntuneBaseline" } $zipUrl = "$RepoUrl/archive/refs/heads/$Branch.zip" $zipPath = Join-Path -Path ([System.IO.Path]::GetTempPath()) -ChildPath "OpenIntuneBaseline-$Branch.zip" if (-not $PSCmdlet.ShouldProcess($DestinationPath, "Download and extract OpenIntuneBaseline from '$RepoUrl'")) { return $DestinationPath } try { Write-Information (Format-HydrationDisplayMessage -Message "Downloading OpenIntuneBaseline from $zipUrl" -Style 'Info' -Emoji '📥') -InformationAction Continue # Download the repository Invoke-WebRequest -Uri $zipUrl -OutFile $zipPath -UseBasicParsing -ErrorAction Stop # Clean existing directory if present if (Test-Path -Path $DestinationPath) { Remove-Item -Path $DestinationPath -Recurse -Force } # Extract Expand-Archive -Path $zipPath -DestinationPath $DestinationPath -Force # The archive extracts to a subfolder, move contents up $extractedFolder = Get-ChildItem -Path $DestinationPath -Directory | Select-Object -First 1 if ($extractedFolder) { $tempMove = Join-Path -Path ([System.IO.Path]::GetTempPath()) -ChildPath "OIB-temp-$(Get-Random)" Move-Item -Path $extractedFolder.FullName -Destination $tempMove Remove-Item -Path $DestinationPath -Force -Recurse Move-Item -Path $tempMove -Destination $DestinationPath } # Clean up zip Remove-Item -Path $zipPath -Force -ErrorAction SilentlyContinue Write-Information (Format-HydrationDisplayMessage -Message "OpenIntuneBaseline downloaded to: $DestinationPath" -Style 'Success' -Emoji '✅') -InformationAction Continue return $DestinationPath } catch { $errorRecord = [System.Management.Automation.ErrorRecord]::new( [System.Exception]::new("Failed to download OpenIntuneBaseline: $($_.Exception.Message)", $_.Exception), 'BaselineDownloadFailed', [System.Management.Automation.ErrorCategory]::ConnectionError, $null ) $PSCmdlet.ThrowTerminatingError($errorRecord) } } |