在 Powershell 中如何在删除旧的 .msi 时绕过某个产品 code/Patch 代码?
In Powershell how to bypass a certain Product code/Patch code when deleting old .msi?
大家好,希望有人能帮助我。我有一个 powershell 脚本,可以识别旧的 .msi(s) 并将它们从 Windows/Installer 文件夹中删除,但我需要添加一些代码来绕过或(跳过)某个产品 ID/Source 哈希。基本上我需要脚本来跳过这个:{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 缓存文件夹: 你不能在这个文件夹里乱来!您通常会得到无法卸载的产品。
- 这是一个 "super hidden" 文件夹,这意味着即使您显示隐藏文件它也不会显示,您也必须 "show OS files"。这是对其重要性和更改其内容相关危险的有力暗示。
- 所有内容都是自动管理的。一些剩余的文件可能在这里,但它们应该被 OS 中内置的其他机制清理 - 例如
cleanmgr.exe
。在下面的 link 中有更多相关信息。卸载相关产品时也应删除这些文件。
磁盘Space:如果你想清理一些磁盘space,也许试试这些建议改为:Ways to clear disk space. And there is 。请至少访问第一个 link 并找到有关 cleanmgr.exe
的信息。您还可以从 运行 对话框中 运行 更新的 ms-settings:storagesense
。 link.
中也有描述
工具摘要:可从 Windows Key
+ [=36= 访问]Tap R
:
cleanmgr.exe
ms-settings:storagesense
COM 自动化:根据记录,您可以通过 COM 自动化有选择地卸载产品(不包括您的产品),如下所示:
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 模块: 还有一个. Github.com: https://github.com/heaths/psmsi。我没有积极使用过它,但肯定这个模块已经解决了大部分问题并且会是比通过 COM 更好的选择吗?就我个人而言,我只会使用 VBScript 和 COM - 在我看来更容易。
大家好,希望有人能帮助我。我有一个 powershell 脚本,可以识别旧的 .msi(s) 并将它们从 Windows/Installer 文件夹中删除,但我需要添加一些代码来绕过或(跳过)某个产品 ID/Source 哈希。基本上我需要脚本来跳过这个:{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 缓存文件夹: 你不能在这个文件夹里乱来!您通常会得到无法卸载的产品。
- 这是一个 "super hidden" 文件夹,这意味着即使您显示隐藏文件它也不会显示,您也必须 "show OS files"。这是对其重要性和更改其内容相关危险的有力暗示。
- 所有内容都是自动管理的。一些剩余的文件可能在这里,但它们应该被 OS 中内置的其他机制清理 - 例如
cleanmgr.exe
。在下面的 link 中有更多相关信息。卸载相关产品时也应删除这些文件。
磁盘Space:如果你想清理一些磁盘space,也许试试这些建议改为:Ways to clear disk space. And there is cleanmgr.exe
的信息。您还可以从 运行 对话框中 运行 更新的 ms-settings:storagesense
。 link.
工具摘要:可从 Windows Key
+ [=36= 访问]Tap R
:
cleanmgr.exe
ms-settings:storagesense
COM 自动化:根据记录,您可以通过 COM 自动化有选择地卸载产品(不包括您的产品),如下所示:
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 模块: 还有一个