Skip to content

Instantly share code, notes, and snippets.

@aplocher
Last active May 28, 2023 00:32
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save aplocher/b10131abc9a4db3dff984416ababd2c1 to your computer and use it in GitHub Desktop.
Save aplocher/b10131abc9a4db3dff984416ababd2c1 to your computer and use it in GitHub Desktop.
Fix for Remove-AppxPackage error "HRESULT: 0x80073CFA, Removal failed The system cannot find the file specified.". Requires psexec to be installed
param (
[switch]$Relaunched = $false
)
$ScriptPath = (Get-Variable MyInvocation).Value.MyCommand.Path
function StartOperation {
Write-Host
Write-Host Now attempting to regenerate missing manifest files...
Write-Host
Find-Module PSSqlite | Install-Module
Write-Host Installing PSSqlite Module if necessary
Write-Host
Import-Module PSSqlite
Write-Host Importing PSSqlite Module
Write-Host
$appRepoPath="$($env:ProgramData)\Microsoft\Windows\AppRepository"
#$appRepoPath="C:\Users\a\Desktop\testapprep"
Invoke-SqliteQuery -Query "SELECT _PackageID, PackageFullName FROM Package" -Datasource "$($appRepoPath)\StateRepository-Machine.srd" |
Select-Object _PackageID, PackageFullName, @{Name="FullPath";Expression={"$($appRepoPath)\$($_.PackageFullName).xml"}} |
Where-Object { (Test-Path $_.FullPath) -eq $false } |
% {
$fullPath=$_.FullPath
Write-Host * Generating $fullPath
Invoke-SqliteQuery -Query "SELECT * FROM AppxManifest WHERE Package=$($_._PackageID)" -Datasource "$($appRepoPath)\StateRepository-Deployment.srd" |
% { Out-File -NoClobber -FilePath $fullPath -InputObject $_.Xml }
}
Write-Host
Write-Host Done
Start-Sleep 3
}
function RelaunchProcessAsSystem {
try {
$AdminProcess = Start-Process -NoNewWindow -Wait -PassThru "psexec.exe" -ArgumentList "-AcceptEula -s ""$PSHOME\powershell.exe"" -ExecutionPolicy Unrestricted -file ""$ScriptPath"" -Relaunched"
return $AdminProcess
} catch {
$Error[0]
exit 1
}
}
function RunAsSystem {
if ($Relaunched) {
Write-Host 'Starting Operation'
Start-Sleep -Seconds 1
StartOperation
} else {
CheckDependencies
Write-Host
Write-Host 'Running tasks under LOCAL SYSTEM user account'
Write-Host '(note, a separate window may popup)'
$AdminProcess = RelaunchProcessAsSystem
while (-not $AdminProcess.HasExited) {
Start-Sleep -Seconds 1
}
}
}
function CheckDependencies {
Write-Host 'Running as admin? .......... ' -nonewline
$isRunningAsAdmin=([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if ($isRunningAsAdmin) {
Write-Host -foregroundcolor Green 'YES'
} else {
Write-Host -foregroundcolor Red 'NO'
exit 1
}
Write-Host 'Is psexec.exe found? ....... ' -nonewline
$isPsExecFound=((Get-Command "psexec.exe" -ErrorAction SilentlyContinue) -ne $null)
if ($isPsExecFound) {
Write-Host -foregroundcolor Green 'YES'
} else {
Write-Host -foregroundcolor Red 'NO'
exit 1
}
}
RunAsSystem
Start-Sleep -Seconds 1
@ricardogaspar2
Copy link

wouldn't it be nice to dynamically install PSTools?
I faced that issue myself.
Here's a nice link:
https://adamtheautomator.com/psexec-ultimate-guide/

@Saderius
Copy link

Saderius commented Feb 1, 2021

Any idea what should I do when I get this?

Invoke-SqliteQuery : Exception calling "Fill" with "1" argument(s): "database disk image is malformed
malformed database schema (IDX_Activation_Flags_Executable_Entrypoint_RuntimeType_StartPage_ResourceGroup) - near "(":
syntax error"
At C:\Users\porto\Desktop\b10131abc9a4db3dff984416ababd2c1-56224bba6bc6cf290fe7dd035a4bbfedeaefe394\FixStoreApps.ps1:22
 char:5
+     Invoke-SqliteQuery -Query "SELECT _PackageID, PackageFullName FRO ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Invoke-SqliteQuery


Done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment