@@ -60,88 +60,93 @@ $itemNames = loadManifest -ManifestUri "$ManifestDirectory\$ManifestCategory.txt
60
60
61
61
$PurgePackage = {
62
62
param (
63
- [Parameter (Position = 0 , Mandatory = $true )] [string ] $PackageName ,
64
- [Parameter (Position = 1 , Mandatory = $false )] [int ] $attempt = 0
63
+ [Parameter (Position = 0 , Mandatory = $true )] [string ] $PackageName
65
64
)
66
65
67
- $maxAttempts = 4
68
- $appxOpCollisionErr = " Another operation on app packages (.appx) is in progress.`n Wait for the current operation to complete and then retry the command. For more information, see the help."
66
+ function PurgeBlock {
67
+ param (
68
+ [Parameter (Position = 0 , Mandatory = $true )] [string ] $PackageName ,
69
+ [Parameter (Position = 1 , Mandatory = $false )] [int ] $attempt = 0
70
+ )
69
71
70
- # REMOVING PACKAGES
71
- try {
72
- $installed = Get-AppxPackage - AllUsers - ErrorAction Stop | Select-Object PackageFullName, Name `
73
- | Where-Object Name -eq $PackageName `
74
- | Select-Object - ExpandProperty PackageFullName
75
- } catch {
76
- if ($_.Exception.Message -like $appxOpCollisionErr ) {
77
- if ($attempt -lt $maxAttempts ) {
78
- Start-Sleep - Seconds 5
79
- return & $PurgePackage - PackageName $PackageName - attempt ($attempt + 1 )
80
- } else {
81
- Write-Error " Failed to query installed AppX packages for $PackageName after $maxAttempts attempts."
82
- }
83
- } else {
84
- throw $_.Exception
85
- }
86
- }
72
+ $maxAttempts = 4
73
+ $appxOpCollisionErr = " Another operation on app packages (.appx) is in progress."
87
74
88
- if ( $installed ) {
75
+ # REMOVING PACKAGES
89
76
try {
90
- Remove-AppxPackage - Verbose:$false - Package $installed - ErrorAction Stop
91
- Write-Verbose " Removed AppX package $PackageName ."
77
+ $installed = Get-AppxPackage - AllUsers - ErrorAction Stop | Select-Object PackageFullName, Name `
78
+ | Where-Object Name -eq $PackageName `
79
+ | Select-Object - ExpandProperty PackageFullName
92
80
} catch {
93
- if ($_.Exception.Message -like $appxOpCollisionErr ) {
81
+ if ($_.Exception.Message -like " * $appxOpCollisionErr * " ) {
94
82
if ($attempt -lt $maxAttempts ) {
95
83
Start-Sleep - Seconds 5
96
- return & $PurgePackage - PackageName $PackageName - attempt ($attempt + 1 )
84
+ PurgeBlock - PackageName $PackageName - attempt ($attempt + 1 )
97
85
} else {
98
- Write-Error " Failed to query provisioned AppX packages for $PackageName after $maxAttempts attempts."
86
+ Write-Error " Failed to query installed AppX packages for $PackageName after $maxAttempts attempts."
99
87
}
100
88
} else {
101
- Write-Error " Failed to deprovision AppX package $PackageName . `n $ _ .Exception.Message "
89
+ throw $ _.Exception
102
90
}
103
91
}
104
- Clear-Variable installed
105
- }
106
-
107
92
108
-
109
- # DEPROVISION
110
- try {
111
- $provisioned = Get-AppxProvisionedPackage - Online - Verbose:$false - ErrorAction Stop `
112
- | Where-Object DisplayName -eq $PackageName `
113
- | Select-Object - ExpandProperty PackageName
114
- } catch {
115
- if ($_.Exception.Message -like $appxOpCollisionErr ) {
116
- if ($attempt -lt $maxAttempts ) {
117
- Start-Sleep - Seconds 5
118
- return & $PurgePackage - PackageName $PackageName - attempt ($attempt + 1 )
119
- } else {
120
- Write-Error " Failed to query provisioned AppX packages for $PackageName after $maxAttempts attempts."
93
+ if ($installed ) {
94
+ try {
95
+ Remove-AppxPackage - Verbose:$false - Package $installed - ErrorAction Stop
96
+ Write-Verbose " Removed AppX package $PackageName ."
97
+ } catch {
98
+ if ($_.Exception.Message -like " *$appxOpCollisionErr *" ) {
99
+ if ($attempt -lt $maxAttempts ) {
100
+ Start-Sleep - Seconds 5
101
+ PurgeBlock - PackageName $PackageName - attempt ($attempt + 1 )
102
+ } else {
103
+ Write-Error " Failed to query provisioned AppX packages for $PackageName after $maxAttempts attempts."
104
+ }
105
+ } else {
106
+ Write-Error " Failed to deprovision AppX package $PackageName .`n $_ .Exception.Message"
107
+ }
121
108
}
122
- } else {
123
- throw $_.Exception
109
+ Clear-Variable installed
124
110
}
125
- }
126
111
127
- if ( $provisioned ) {
112
+ # DEPROVISION
128
113
try {
129
- Remove-AppxProvisionedPackage - Online - Verbose:$false - PackageName $provisioned - ErrorAction Stop | Out-Null
130
- Write-Verbose " Deprovisioned AppX package $PackageName ."
114
+ $provisioned = Get-AppxProvisionedPackage - Online - Verbose:$false - ErrorAction Stop `
115
+ | Where-Object DisplayName -eq $PackageName `
116
+ | Select-Object - ExpandProperty PackageName
131
117
} catch {
132
- if ($_.Exception.Message -like $appxOpCollisionErr ) {
118
+ if ($_.Exception.Message -like " * $appxOpCollisionErr * " ) {
133
119
if ($attempt -lt $maxAttempts ) {
134
120
Start-Sleep - Seconds 5
135
- return & $PurgePackage - PackageName $PackageName - attempt ($attempt + 1 )
121
+ PurgeBlock - PackageName $PackageName - attempt ($attempt + 1 )
136
122
} else {
137
123
Write-Error " Failed to query provisioned AppX packages for $PackageName after $maxAttempts attempts."
138
124
}
139
125
} else {
140
- Write-Error " Failed to deprovision AppX package $PackageName .`n $_ .Exception.Message"
126
+ throw $_.Exception
127
+ }
128
+ }
129
+
130
+ if ($provisioned ) {
131
+ try {
132
+ Remove-AppxProvisionedPackage - Online - Verbose:$false - PackageName $provisioned - ErrorAction Stop | Out-Null
133
+ Write-Verbose " Deprovisioned AppX package $PackageName ."
134
+ } catch {
135
+ if ($_.Exception.Message -like " *$appxOpCollisionErr *" ) {
136
+ if ($attempt -lt $maxAttempts ) {
137
+ Start-Sleep - Seconds 5
138
+ PurgeBlock - PackageName $PackageName - attempt ($attempt + 1 )
139
+ } else {
140
+ Write-Error " Failed to query provisioned AppX packages for $PackageName after $maxAttempts attempts."
141
+ }
142
+ } else {
143
+ Write-Error " Failed to deprovision AppX package $PackageName .`n $_ .Exception.Message"
144
+ }
141
145
}
146
+ Clear-Variable provisioned
142
147
}
143
- Clear-Variable provisioned
144
148
}
149
+ PurgeBlock - PackageName $PackageName
145
150
}
146
151
147
152
$RemovalJobs = @ ()
0 commit comments