Documentation
¶
Index ¶
Constants ¶
const FirewallRuleApplyScript = `` /* 1213-byte string literal not displayed */
const FirewallRuleCheckScript = `` /* 932-byte string literal not displayed */
const ModuleRemoveAppxApplyScript = `` /* 3141-byte string literal not displayed */
ModuleRemoveAppxApplyScript preserves the module-side apply flow, including iterating raw installed package results before skipping empty PackageFullName entries with output.
const PackageApplyScript = `` /* 1819-byte string literal not displayed */
const PackageCheckScript = `` /* 773-byte string literal not displayed */
const PowerPlanApplyScript = `` /* 2644-byte string literal not displayed */
const PowerPlanCheckScript = `` /* 1911-byte string literal not displayed */
const PowerPlanModuleApplyScript = `` /* 2834-byte string literal not displayed */
const PowerPlanModuleCheckScript = `` /* 2599-byte string literal not displayed */
const RegistryApplyScript = `` /* 2204-byte string literal not displayed */
const RegistryCheckScript = `` /* 3647-byte string literal not displayed */
const RegistryEnsureScript = `` /* 4777-byte string literal not displayed */
RegistryEnsureScript combines check and apply in one PowerShell invocation. It outputs "ok", "changed", or "would-change" (dry-run). $__pf_dry_run must be injected by the caller before $params.
const RegistryModuleCheckScript = `` /* 3648-byte string literal not displayed */
const RemoveAppxApplyScript = RemoveAppxHelperFunctions + `
foreach ($spec in $pkgs) {
$name = [string]$spec.name
$scope = if ($spec.scope) { [string]$spec.scope } else { 'both' }
$hasWildcard = [WildcardPattern]::ContainsWildcardCharacters($name)
Write-Output ("processing appx package " + $name + " (" + $scope + ")")
if ($scope -ne 'provisioned') {
foreach ($pkg in (Get-InstalledAppxMatches $scope $name)) {
if ($null -eq $pkg) { continue }
$packageFullName = [string]$pkg.PackageFullName
if ([string]::IsNullOrWhiteSpace($packageFullName)) {
Write-Output ("skipping appx package " + $name + " because PackageFullName is empty")
continue
}
Remove-AppxInstalledSafe $packageFullName $scope
}
}
if ($scope -eq 'provisioned' -or $scope -eq 'both') {
@(Get-ProvisionedAppxMatches $scope $name $hasWildcard) | ForEach-Object {
Remove-AppxProvisionedSafe $_.PackageName
}
}
}
`
RemoveAppxApplyScript calls Get-AppxProvisionedPackage -Online once before the loop and filters the cached list per package. Removed packages may still appear in the cached list but Remove-AppxProvisionedPackage with -ErrorAction SilentlyContinue handles that gracefully.
const RemoveAppxCheckScript = RemoveAppxHelperFunctions + `
foreach ($spec in $pkgs) {
$name = [string]$spec.name
$scope = if ($spec.scope) { [string]$spec.scope } else { 'both' }
$hasWildcard = [WildcardPattern]::ContainsWildcardCharacters($name)
$installed = Get-InstalledAppxMatches $scope $name
$provisioned = Get-ProvisionedAppxMatches $scope $name $hasWildcard
if (($installed.Count + $provisioned.Count) -gt 0) { Write-Output 'true'; exit 0 }
}
Write-Output 'false'
`
const RemoveAppxCheckScriptWithOutput = RemoveAppxHelperFunctions + `
foreach ($spec in $pkgs) {
$name = [string]$spec.name
$scope = if ($spec.scope) { [string]$spec.scope } else { 'both' }
$hasWildcard = [WildcardPattern]::ContainsWildcardCharacters($name)
Write-Output ("checking appx package " + $name + " (" + $scope + ")")
$installed = Get-InstalledAppxMatches $scope $name
$provisioned = Get-ProvisionedAppxMatches $scope $name $hasWildcard
if (($installed.Count + $provisioned.Count) -gt 0) { Write-Output 'true'; exit 0 }
}
Write-Output 'false'
`
const RemoveAppxEnsureScript = RemoveAppxHelperFunctions + `
$needs = $false
foreach ($spec in $pkgs) {
$name = [string]$spec.name
$scope = if ($spec.scope) { [string]$spec.scope } else { 'both' }
$hasWildcard = [WildcardPattern]::ContainsWildcardCharacters($name)
if ((Get-InstalledAppxMatches $scope $name).Count -gt 0) { $needs = $true; break }
if ((Get-ProvisionedAppxMatches $scope $name $hasWildcard).Count -gt 0) { $needs = $true; break }
}
if (-not $needs) { Write-Output 'ok'; exit 0 }
if ($__pf_dry_run) { Write-Output 'would-change'; exit 0 }
foreach ($spec in $pkgs) {
$name = [string]$spec.name
$scope = if ($spec.scope) { [string]$spec.scope } else { 'both' }
$hasWildcard = [WildcardPattern]::ContainsWildcardCharacters($name)
if ($scope -ne 'provisioned') {
foreach ($pkg in (Get-InstalledAppxMatches $scope $name)) {
if ($null -eq $pkg) { continue }
$packageFullName = [string]$pkg.PackageFullName
if ([string]::IsNullOrWhiteSpace($packageFullName)) { continue }
Remove-AppxInstalledSafe $packageFullName $scope
}
}
if ($scope -eq 'provisioned' -or $scope -eq 'both') {
@(Get-ProvisionedAppxMatches $scope $name $hasWildcard) | ForEach-Object {
Remove-AppxProvisionedSafe $_.PackageName
}
}
}
Write-Output 'changed'
`
RemoveAppxEnsureScript combines check and apply in one invocation, calling Get-AppxProvisionedPackage -Online exactly once regardless of outcome. Outputs "ok", "would-change" (dry-run), or "changed". $__pf_dry_run must be set before $params by the caller.
const RemoveAppxHelperFunctions = `` /* 2723-byte string literal not displayed */
RemoveAppxHelperFunctions is shared preamble for all remove_appx_packages scripts. Get-AppxProvisionedPackage -Online is a slow DISM call; $allProvisioned caches it once per script invocation rather than once per package.
const ScheduledTaskApplyScript = `` /* 3950-byte string literal not displayed */
const ScheduledTaskCheckScript = `` /* 3987-byte string literal not displayed */
const ServiceApplyScript = `` /* 667-byte string literal not displayed */
const ServiceCheckScript = `` /* 947-byte string literal not displayed */
const ShortcutApplyScript = `` /* 671-byte string literal not displayed */
const ShortcutCheckScript = `` /* 662-byte string literal not displayed */
const UserApplyScript = `` /* 900-byte string literal not displayed */
const UserCheckScript = `` /* 678-byte string literal not displayed */
const WindowsFeatureApplyScript = `` /* 306-byte string literal not displayed */
const WindowsFeatureCheckScript = `` /* 411-byte string literal not displayed */
const WindowsFeatureModuleCheckScript = `` /* 401-byte string literal not displayed */
const WingetPackageApplyScript = `
$pkgs = @($params.packages)
Get-Command winget.exe -ErrorAction Stop | Out-Null
` + wingetPackagePresenceScript + `
$tempPath = Join-Path $env:TEMP ("preflight-winget-" + [guid]::NewGuid().ToString() + ".json")
$stdoutPath = Join-Path $env:TEMP ("preflight-winget-" + [guid]::NewGuid().ToString() + ".stdout.log")
$stderrPath = Join-Path $env:TEMP ("preflight-winget-" + [guid]::NewGuid().ToString() + ".stderr.log")
try {
$process = Start-Process -FilePath 'winget.exe' -ArgumentList @('export', '--output', $tempPath, '--include-versions', '--accept-source-agreements', '--disable-interactivity') -Wait -PassThru -NoNewWindow -RedirectStandardOutput $stdoutPath -RedirectStandardError $stderrPath
if ($process.ExitCode -ne 0) {
$details = @()
if (Test-Path -LiteralPath $stdoutPath) {
$stdout = Get-Content -LiteralPath $stdoutPath -Raw
if (-not [string]::IsNullOrWhiteSpace($stdout)) { $details += $stdout.Trim() }
}
if (Test-Path -LiteralPath $stderrPath) {
$stderr = Get-Content -LiteralPath $stderrPath -Raw
if (-not [string]::IsNullOrWhiteSpace($stderr)) { $details += $stderr.Trim() }
}
if ($details.Count -gt 0) {
throw ("winget export failed with exit code $($process.ExitCode)" + [Environment]::NewLine + [string]::Join([Environment]::NewLine, $details))
}
throw "winget export failed with exit code $($process.ExitCode)"
}
$doc = Get-Content -LiteralPath $tempPath -Raw | ConvertFrom-Json
} finally {
Remove-Item -LiteralPath $tempPath -Force -ErrorAction SilentlyContinue
Remove-Item -LiteralPath $stdoutPath -Force -ErrorAction SilentlyContinue
Remove-Item -LiteralPath $stderrPath -Force -ErrorAction SilentlyContinue
}
$installedMap = @{}
foreach ($src in @($doc.Sources)) {
foreach ($pkg in @($src.Packages)) {
$installedMap[$pkg.PackageIdentifier] = $pkg
}
}
foreach ($spec in $pkgs) {
$id = [string]$spec.id
$ensure = if ($spec.ensure) { [string]$spec.ensure } else { 'present' }
$version = if ($spec.version) { [string]$spec.version } else { '' }
$source = if ($spec.source) { [string]$spec.source } else { '' }
$scope = if ($spec.scope) { [string]$spec.scope } else { 'machine' }
$wingetArgs = @()
if ($spec.args) {
foreach ($arg in $spec.args) { $wingetArgs += [string]$arg }
}
$isPresent = Test-WingetDesiredPresent -Spec $spec -InstalledMap $installedMap
if ($ensure -eq 'absent' -and -not $isPresent) { continue }
if ($ensure -ne 'absent' -and $isPresent) { continue }
$args = @()
if ($ensure -eq 'absent') {
$args = @('uninstall', '--id', $id, '--exact', '--disable-interactivity', '--accept-source-agreements')
} else {
$args = @('install', '--id', $id, '--exact', '--silent', '--disable-interactivity', '--accept-package-agreements', '--accept-source-agreements', '--scope', $scope)
}
if ($version) { $args += @('--version', $version) }
if ($source) { $args += @('--source', $source) }
$args += $wingetArgs
$installStdoutPath = Join-Path $env:TEMP ("preflight-winget-" + [guid]::NewGuid().ToString() + ".stdout.log")
$installStderrPath = Join-Path $env:TEMP ("preflight-winget-" + [guid]::NewGuid().ToString() + ".stderr.log")
try {
$process = Start-Process -FilePath 'winget.exe' -ArgumentList $args -Wait -PassThru -NoNewWindow -RedirectStandardOutput $installStdoutPath -RedirectStandardError $installStderrPath
} finally {
$details = @()
if (Test-Path -LiteralPath $installStdoutPath) {
$installStdout = Get-Content -LiteralPath $installStdoutPath -Raw
if (-not [string]::IsNullOrWhiteSpace($installStdout)) { $details += $installStdout.Trim() }
}
if (Test-Path -LiteralPath $installStderrPath) {
$installStderr = Get-Content -LiteralPath $installStderrPath -Raw
if (-not [string]::IsNullOrWhiteSpace($installStderr)) { $details += $installStderr.Trim() }
}
Remove-Item -LiteralPath $installStdoutPath -Force -ErrorAction SilentlyContinue
Remove-Item -LiteralPath $installStderrPath -Force -ErrorAction SilentlyContinue
}
if ($process.ExitCode -ne 0) {
$combinedDetails = [string]::Join([Environment]::NewLine, $details)
# WinGet can turn "install an already-present package" into an update path
# and return UPDATE_NOT_APPLICABLE even when the unversioned desired state is satisfied.
if ($ensure -ne 'absent' -and $process.ExitCode -eq -1978335189) {
if ((Test-WingetDesiredPresent -Spec $spec -InstalledMap $installedMap) -or ($combinedDetails -match 'No available upgrade found' -and $combinedDetails -match 'No newer package versions are available')) {
continue
}
}
if ($details.Count -gt 0) {
throw ("winget command failed for '$id' with exit code $($process.ExitCode)" + [Environment]::NewLine + $combinedDetails)
}
throw "winget command failed for '$id' with exit code $($process.ExitCode)"
}
$machinePath = [Environment]::GetEnvironmentVariable('Path', 'Machine')
$userPath = [Environment]::GetEnvironmentVariable('Path', 'User')
$env:Path = (@($machinePath, $userPath) | Where-Object { -not [string]::IsNullOrEmpty($_) }) -join ';'
}
`
const WingetPackageCheckScript = `
$pkgs = @($params.packages)
Get-Command winget.exe -ErrorAction Stop | Out-Null
` + wingetPackagePresenceScript + `
$tempPath = Join-Path $env:TEMP ("preflight-winget-" + [guid]::NewGuid().ToString() + ".json")
$stdoutPath = Join-Path $env:TEMP ("preflight-winget-" + [guid]::NewGuid().ToString() + ".stdout.log")
$stderrPath = Join-Path $env:TEMP ("preflight-winget-" + [guid]::NewGuid().ToString() + ".stderr.log")
try {
$process = Start-Process -FilePath 'winget.exe' -ArgumentList @('export', '--output', $tempPath, '--include-versions', '--accept-source-agreements', '--disable-interactivity') -Wait -PassThru -NoNewWindow -RedirectStandardOutput $stdoutPath -RedirectStandardError $stderrPath
if ($process.ExitCode -ne 0) {
$details = @()
if (Test-Path -LiteralPath $stdoutPath) {
$stdout = Get-Content -LiteralPath $stdoutPath -Raw
if (-not [string]::IsNullOrWhiteSpace($stdout)) { $details += $stdout.Trim() }
}
if (Test-Path -LiteralPath $stderrPath) {
$stderr = Get-Content -LiteralPath $stderrPath -Raw
if (-not [string]::IsNullOrWhiteSpace($stderr)) { $details += $stderr.Trim() }
}
if ($details.Count -gt 0) {
throw ("winget export failed with exit code $($process.ExitCode)" + [Environment]::NewLine + [string]::Join([Environment]::NewLine, $details))
}
throw "winget export failed with exit code $($process.ExitCode)"
}
$doc = Get-Content -LiteralPath $tempPath -Raw | ConvertFrom-Json
} finally {
Remove-Item -LiteralPath $tempPath -Force -ErrorAction SilentlyContinue
Remove-Item -LiteralPath $stdoutPath -Force -ErrorAction SilentlyContinue
Remove-Item -LiteralPath $stderrPath -Force -ErrorAction SilentlyContinue
}
$installedMap = @{}
foreach ($src in @($doc.Sources)) {
foreach ($pkg in @($src.Packages)) {
$installedMap[$pkg.PackageIdentifier] = $pkg
}
}
foreach ($spec in $pkgs) {
$ensure = if ($spec.ensure) { [string]$spec.ensure } else { 'present' }
$isPresent = Test-WingetDesiredPresent -Spec $spec -InstalledMap $installedMap
if ($ensure -eq 'absent') {
if ($isPresent) { Write-Output 'true'; exit 0 }
} else {
if (-not $isPresent) { Write-Output 'true'; exit 0 }
}
}
Write-Output 'false'
`
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
This section is empty.