【发布时间】:2019-12-06 14:28:58
【问题描述】:
大家好,希望有人可以帮助我。我有一个 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
基本上现在它正在删除所有无法找到依赖源的内容。或者至少我认为它是......我从其他人那里得到了这个脚本,所以是的,基本上我不知道该怎么做。
【问题讨论】:
-
如果匹配值否从数组中删除?也可以对多个值执行此操作 [Array]$index = $row.Split(" ") -ne $Guid1 -ne $Guid2
-
是的,如果它匹配一个值。那么我会把它放在哪里呢? [Array]$index = $row.Split(" ") -ne $Guid1 -ne $Guid2 我猜在 (" ") 里面可以放产品代码,它会跳过吗?
-
在您遍历并将 GUIDS 列表放入一个数组之后。那么您指定的 guid 将被删除
-
在不知道自己在做什么的情况下,不能像这样手动破解此文件夹,即使这样也很危险。我在下面添加了一些建议。总体目标是什么:
just cleanup或create disk space还是什么?
标签: powershell windows-installer delete-file