pscript

package
v1.0.0-beta.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 14, 2026 License: ISC Imports: 0 Imported by: 0

Documentation

Index

Constants

View Source
const FirewallRuleApplyScript = `` /* 1213-byte string literal not displayed */
View Source
const FirewallRuleCheckScript = `` /* 932-byte string literal not displayed */
View Source
const PackageApplyScript = `` /* 1819-byte string literal not displayed */
View Source
const PackageCheckScript = `` /* 773-byte string literal not displayed */
View Source
const PowerPlanApplyScript = `` /* 2644-byte string literal not displayed */
View Source
const PowerPlanCheckScript = `` /* 1911-byte string literal not displayed */
View Source
const RegistryApplyScript = `` /* 3321-byte string literal not displayed */
View Source
const RegistryCheckScript = `` /* 4192-byte string literal not displayed */
View Source
const RegistryEnsureScript = `` /* 6417-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.

View Source
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.

View Source
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'
`
View Source
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.

View Source
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.

View Source
const ScheduledTaskApplyScript = `` /* 4850-byte string literal not displayed */
View Source
const ScheduledTaskCheckScript = `` /* 4693-byte string literal not displayed */
View Source
const ServiceApplyScript = `` /* 667-byte string literal not displayed */
View Source
const ServiceCheckScript = `` /* 947-byte string literal not displayed */
View Source
const ShortcutApplyScript = `` /* 809-byte string literal not displayed */
View Source
const ShortcutCheckScript = `` /* 842-byte string literal not displayed */
View Source
const UserApplyScript = `` /* 862-byte string literal not displayed */
View Source
const UserCheckScript = `` /* 1192-byte string literal not displayed */
View Source
const WindowsFeatureApplyScript = `` /* 319-byte string literal not displayed */
View Source
const WindowsFeatureCheckScript = `` /* 401-byte string literal not displayed */
View Source
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")
try {
  $result = Invoke-Winget -Arguments @('export', '--output', $tempPath, '--include-versions', '--accept-source-agreements', '--disable-interactivity')
  if ($result.ExitCode -ne 0) {
    if ($result.Details.Count -gt 0) {
      throw ("winget export failed with exit code $($result.ExitCode)" + [Environment]::NewLine + [string]::Join([Environment]::NewLine, $result.Details))
    }
    throw "winget export failed with exit code $($result.ExitCode)"
  }
  $doc = Get-Content -LiteralPath $tempPath -Raw | ConvertFrom-Json
} finally {
  Remove-Item -LiteralPath $tempPath -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
  $result = Invoke-Winget -Arguments $args
  if ($result.ExitCode -ne 0) {
    $combinedDetails = [string]::Join([Environment]::NewLine, $result.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 $result.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 ($result.Details.Count -gt 0) {
      throw ("winget command failed for '$id' with exit code $($result.ExitCode)" + [Environment]::NewLine + $combinedDetails)
    }
    throw "winget command failed for '$id' with exit code $($result.ExitCode)"
  }
  $machinePath = [Environment]::GetEnvironmentVariable('Path', 'Machine')
  $userPath = [Environment]::GetEnvironmentVariable('Path', 'User')
  $env:Path = (@($machinePath, $userPath) | Where-Object { -not [string]::IsNullOrEmpty($_) }) -join ';'
}
`
View Source
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")
try {
  $result = Invoke-Winget -Arguments @('export', '--output', $tempPath, '--include-versions', '--accept-source-agreements', '--disable-interactivity')
  if ($result.ExitCode -ne 0) {
    if ($result.Details.Count -gt 0) {
      throw ("winget export failed with exit code $($result.ExitCode)" + [Environment]::NewLine + [string]::Join([Environment]::NewLine, $result.Details))
    }
    throw "winget export failed with exit code $($result.ExitCode)"
  }
  $doc = Get-Content -LiteralPath $tempPath -Raw | ConvertFrom-Json
} finally {
  Remove-Item -LiteralPath $tempPath -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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL