|
| 1 | +# Copyright (c) Microsoft Corporation and Contributors. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +<# |
| 5 | +.SYNOPSIS |
| 6 | + Return the process ids of processes using the package dependency. |
| 7 | +
|
| 8 | +.DESCRIPTION |
| 9 | + Return the process ids of processes using the package dependency. |
| 10 | +
|
| 11 | + This does not add the package to the invoking process' package graph. |
| 12 | +
|
| 13 | + NOTE: -All, -PackageDependencyId and -PackageFamilyName are mutually exclusive. |
| 14 | +
|
| 15 | +.PARAMETER PackageDependencyId |
| 16 | + Find package dependencies with this id. |
| 17 | +
|
| 18 | +.PARAMETER All |
| 19 | + Find all package dependencies. |
| 20 | +
|
| 21 | +.PARAMETER PackageFamilyName |
| 22 | + Find package dependencies with this package family. |
| 23 | +
|
| 24 | +.PARAMETER ScopeIsSystem |
| 25 | + Find package dependencies created with CreatePackageDependencyOptions_ScopeIsSystem. |
| 26 | +
|
| 27 | +.LINK |
| 28 | + https://learn.microsoft.com/windows/win32/api/appmodel/nf-appmodel-findpackagedependency |
| 29 | + https://learn.microsoft.com/windows/win32/api/appmodel/nf-appmodel-getpackagedependencyinformation |
| 30 | + https://learn.microsoft.com/windows/win32/api/appmodel/nf-appmodel-getprocessesusingpackagedependency |
| 31 | +#> |
| 32 | +[CmdletBinding(SupportsShouldProcess=$true)] |
| 33 | +param( |
| 34 | + [Parameter(ParameterSetName='All')] |
| 35 | + [switch]$All, |
| 36 | + |
| 37 | + [Parameter(ParameterSetName='Id')] |
| 38 | + [string]$PackageDependencyId, |
| 39 | + |
| 40 | + [Parameter(ParameterSetName='Package')] |
| 41 | + [string]$PackageFamilyName, |
| 42 | + |
| 43 | + [switch]$ScopeIsSystem |
| 44 | +) |
| 45 | + |
| 46 | +Set-StrictMode -Version 3.0 |
| 47 | + |
| 48 | +$ErrorActionPreference = "Stop" |
| 49 | + |
| 50 | +$ERROR_SUCCESS = 0 |
| 51 | + |
| 52 | +function Is-PackageDependencyId |
| 53 | +{ |
| 54 | + param( |
| 55 | + [string]$id |
| 56 | + ) |
| 57 | + |
| 58 | + if (($id -ne $null) -and ($id.Length -ge 3)) |
| 59 | + { |
| 60 | + $prefix = $id.Substring(0, 2) |
| 61 | + return (($prefix -eq 'T:') -or ($prefix -eq 'P:')) |
| 62 | + } |
| 63 | + return $false |
| 64 | +} |
| 65 | + |
| 66 | +function Is-PackageFamilyName |
| 67 | +{ |
| 68 | + param( |
| 69 | + [string]$packageFamilyName |
| 70 | + ) |
| 71 | + |
| 72 | + $rc = [Microsoft.Windows.ApplicationModel.PackageFamilyName]::Verify($packageFamilyName) |
| 73 | + return $rc == $ERROR_SUCCESS |
| 74 | +} |
| 75 | + |
| 76 | +function Get-Processes |
| 77 | +{ |
| 78 | + param( |
| 79 | + [string]$id |
| 80 | + ) |
| 81 | + |
| 82 | + [uint32]$processIdsCount = 0 |
| 83 | + [uint32[]]$processIds = $null |
| 84 | + $hr = [Microsoft.Windows.ApplicationModel.DynamicDependency.PackageDependency]::GetProcesses( |
| 85 | + $id, $ScopeIsSystem, [ref] $processIdsCount, [ref] $processIds) |
| 86 | + if ($hr -lt 0) |
| 87 | + { |
| 88 | + $win32ex = [System.ComponentModel.Win32Exception]::new($hr) |
| 89 | + Write-Error "Error 0x$($hr.ToString('X')): $($win32ex.Message)" -ErrorAction Stop |
| 90 | + } |
| 91 | + return $processIds |
| 92 | +} |
| 93 | + |
| 94 | +# Import the MSIX Dynamic Dependency module |
| 95 | +if (-not (Get-Module | Where-Object {$_.Name -eq 'MsixDynamicDependency'})) |
| 96 | +{ |
| 97 | + $module = Join-Path $PSScriptRoot 'MsixDynamicDependency.psm1' |
| 98 | + Import-Module -Name $module -ErrorAction Stop |
| 99 | +} |
| 100 | + |
| 101 | +$ids = [ordered]@{} |
| 102 | +if ($PSCmdlet.ParameterSetName -eq 'None') |
| 103 | +{ |
| 104 | + if ([string]::IsNullOrWhiteSpace($PackageDependencyId)) |
| 105 | + { |
| 106 | + $All = $true |
| 107 | + } |
| 108 | + elseif (Is-PackageFamilyName $PackageDependencyId) |
| 109 | + { |
| 110 | + $PackageFamilyName = $PackageDependencyId |
| 111 | + $PackageDependencyId = $null |
| 112 | + $PSCmdlet.ParameterSetName = 'Package' |
| 113 | + } |
| 114 | + else |
| 115 | + { |
| 116 | + $PSCmdlet.ParameterSetName = 'Id' |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | + |
| 121 | + |
| 122 | +if (-not [string]::IsNullOrWhiteSpace($PackageDependencyId)) |
| 123 | +{ |
| 124 | + $ids.Add($PackageDependencyId, $null) |
| 125 | +} |
| 126 | +else #if ($PSCmdlet.ParameterSetName -eq 'Package') |
| 127 | +{ |
| 128 | + $findPackageDependencyCriteria = New-Object Microsoft.Windows.ApplicationModel.DynamicDependency.FindPackageDependencyCriteria |
| 129 | + $findPackageDependencyCriteria.User = [IntPtr]::Zero |
| 130 | + $findPackageDependencyCriteria.ScopeIsSystem = $ScopeIsSystem |
| 131 | + if ($PSCmdlet.ParameterSetName -eq 'Package') |
| 132 | + { |
| 133 | + $findPackageDependencyCriteria.PackageFamilyName = $PackageFamilyName |
| 134 | + } |
| 135 | + else # -All |
| 136 | + { |
| 137 | + $findPackageDependencyCriteria.PackageFamilyName = "" #$null |
| 138 | + } |
| 139 | + [uint]$packageDependencyIdsCount = 0 |
| 140 | + [string[]]$packageDependencyIds = $null |
| 141 | + $hr = [Microsoft.Windows.ApplicationModel.DynamicDependency.PackageDependency]::Find( |
| 142 | + $findPackageDependencyCriteria, [ref] $packageDependencyIdsCount, [ref] $packageDependencyIds); |
| 143 | + if ($hr -lt 0) |
| 144 | + { |
| 145 | + $win32ex = [System.ComponentModel.Win32Exception]::new($hr) |
| 146 | + Write-Error "Error 0x$($hr.ToString('X')): $($win32ex.Message)" -ErrorAction Stop |
| 147 | + } |
| 148 | + else |
| 149 | + { |
| 150 | + Write-Host "Package Dependencies: $packageDependencyIdsCount" |
| 151 | + } |
| 152 | + ForEach ($pdi in $packageDependencyIds) |
| 153 | + { |
| 154 | + $ids.Add($pdi, $null) |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +ForEach ($pdi in $ids.Keys) |
| 159 | +{ |
| 160 | + $pids = Get-Processes $pdi |
| 161 | + |
| 162 | + [string]$familyName = $null |
| 163 | + [ulong]$minVersion = 0 |
| 164 | + $processorArchitectures = 0 |
| 165 | + $lifetimeKind = 0 |
| 166 | + [string]$lifetimeArtifact = $null |
| 167 | + $options = 0 |
| 168 | + $lifetimeExpiration = New-Object DateTime |
| 169 | + $hr = [Microsoft.Windows.ApplicationModel.DynamicDependency.PackageDependency]::GetInfo( |
| 170 | + $pdi, [ref] $familyName, [ref] $minVersion, [ref] $processorArchitectures, |
| 171 | + [ref] $lifetimeKind, [ref] $lifetimeArtifact, [ref] $options, [ref] $lifetimeExpiration) |
| 172 | + if ($hr -lt 0) |
| 173 | + { |
| 174 | + $win32ex = [System.ComponentModel.Win32Exception]::new($hr) |
| 175 | + Write-Error "Error 0x$($hr.ToString('X')): $($win32ex.Message)" -ErrorAction Stop |
| 176 | + } |
| 177 | + if ($lifetimeExpiration.Ticks -eq 0) |
| 178 | + { |
| 179 | + $lifetimeExpiration = $null |
| 180 | + } |
| 181 | + |
| 182 | + $packageFullName = .\Get-PackageDependencyResolved.ps1 $pdi |
| 183 | + |
| 184 | + $pd = [PSCustomObject]@{ |
| 185 | + PackageDependencyId = $pdi |
| 186 | + PackageFamilyName = $familyName |
| 187 | + ResolvedPackage = $packageFullName |
| 188 | + MinVersion = $minVersion |
| 189 | + ProcessorArchitectures = $processorArchitectures |
| 190 | + LifetimeKind = $lifetimeKind |
| 191 | + LifetimeArtifact = $lifetimeArtifact |
| 192 | + Options = $options |
| 193 | + LifetimeExpiration = $lifetimeExpiration |
| 194 | + ProcessIDs = $pids |
| 195 | + } |
| 196 | + $pd | Format-List |
| 197 | +} |
0 commit comments