嘿,希望有人能帮助我。我有一个Powershell脚本,该脚本可以标识旧的.msi并将其从Windows / Installer文件夹中删除,但是我需要添加一些代码以绕过或跳过某些产品ID /源哈希。基本上,我需要脚本来跳过此脚本:{1610CB69-BE80-41B9-9B77-E346C1DA12F3},并且不要将其删除。有谁知道我怎么能做到这一点?
这是我的剧本:
<#
This script uses VB script to identify which files need to be saved and then removes everything else.
#>
$VBSFile = @"
'' Identify which patches are registered on the system, and to which
'' products those patches are installed.
'Option Explicit
Dim msi : Set msi = CreateObject("WindowsInstaller.Installer")
'Output CSV header
WScript.Echo "The data format is ProductCode, PatchCode, PatchLocation"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile("output.txt", True)
objFile.WriteLine "ProductCode, PatchCode, PatchLocation"
objFile.WriteLine ""
' Enumerate all products
Dim products : Set products = msi.Products
Dim productCode
For Each productCode in products
' For each product, enumerate its applied patches
Dim patches : Set patches = msi.Patches(productCode)
Dim patchCode
For Each patchCode in patches
' Get the local patch location
Dim location : location = msi.PatchInfo(patchCode, "LocalPackage")
objFile.WriteLine productCode & ", " & patchCode & ", " & location
Next
Next
WScript.Echo "Data written to output.txt, these are the registered objects and SHOULD be kept!"
"@
$VBSFile | Set-Content .\WiMsps.vbs
cscript .\WiMsps.vbs
$savelist = Import-Csv .\output.txt
$filelocation = $savelist | select -ExpandProperty PatchLocation
#First pass to remove exact file names
dir C:\windows\Installer -file | ForEach-Object{
$fullname = $_.FullName
if($filelocation | Where-Object{$_ -like "*$fullname*"}){
"Keeping $fullname"
}
else{
Remove-Item $fullname -Force -Verbose
}
}
#second pass to match product and patch codes
dir C:\windows\Installer -Directory | ForEach-Object{
$fullname = $_.name
if($savelist | Where-Object{$_.ProductCode -like "*$fullname*" -or $_.PatchCode -like "*$fullname*" }){
"Keeping $fullname"
}
else{
Remove-Item $_.fullname -Force -Verbose -Recurse
}
}
pause
基本上现在,它将删除找不到依赖源的所有内容。或者至少我认为这是...我从其他人那里得到了这个脚本,所以是的,我基本上不知道该怎么做。
MSI缓存文件夹:您一定不要在这个文件夹里乱扔东西!通常,您最终会获得无法卸载的产品。
cleanmgr.exe
)清除它们。有关更多信息,请参见下面的链接。卸载有问题的产品时,也应删除文件。[磁盘空间:如果您要清除一些磁盘空间,则可以尝试以下建议:Ways to clear disk space。并且有a long version of the same list。请至少访问第一个链接,并找到有关cleanmgr.exe
的信息。您也可以从运行对话框中运行更新的ms-settings:storagesense
。在链接中也有介绍。
工具摘要:可从Windows Key
+ Tap R
:
cleanmgr.exe
ms-settings:storagesense
COM Automation:为记录起见,您可以像下面这样通过COM Automation有选择地卸载产品(不包括产品):
Dim installer : Set installer = CreateObject("WindowsInstaller.Installer")
Set products = installer.ProductsEx("", "", 7)
'Iterate over all MSI packages on the box
For Each product In products
If LCase("{1610CB69-BE80-41B9-9B77-E346C1DA12F3}") = LCase(product.productcode) Then
MsgBox "Found Product: " + product.InstallProperty("ProductName")
Else
' Can run uninstall here via COM method - ADMIN RIGHTS!
' installer.ConfigureProduct product.productcode, 0, 2
End If
Next
MsgBox "Finished."
Powershell MSI模块:还有一个Powershell module for MSI made by deployment guru Heath Stewart。 Github.com:https://github.com/heaths/psmsi。我没有积极使用它,但是可以肯定的是,此模块已解决了大多数问题,并且比通过COM更好的选择是吗?就我个人而言,我只会使用VBScript和COM-我认为更简单。