powershell - 尝试使用 Powershell 消除我们使用的软件的不需要的安装。(GUID)
问题描述
我正在尝试创建一个脚本来卸载我们在工作站上使用的不需要的软件实例(或旧实例)。不过,我似乎无法正确过滤。
function Get-InstalledSoftware2 {
[OutputType([System.Management.Automation.PSObject])]
[CmdletBinding()]
param (
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]$Name , [string] $OurName
)
$UninstallKeys = "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall", "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
$null = New-PSDrive -Name HKU -PSProvider Registry -Root Registry::HKEY_USERS
$UninstallKeys += Get-ChildItem HKU: -ErrorAction SilentlyContinue | Where-Object { $_.Name -match 'S-\d-\d+-(\d+-){1,14}\d+$' } | ForEach-Object { "HKU:\$($_.PSChildName)\Software\Microsoft\Windows\CurrentVersion\Uninstall" }
if (-not $UninstallKeys) {
Write-Verbose -Message 'No software registry keys found'
} else {
foreach ($UninstallKey in $UninstallKeys) {
# if (!$PSBoundParameters.ContainsKey('OurName')) {
if ($PSBoundParameters.ContainsKey('Name')) {
$WhereBlock = { ($_.PSChildName -match '^{[A-Z0-9]{8}-([A-Z0-9]{4}-){3}[A-Z0-9]{12}}$') -and ($_.GetValue('DisplayName') -like "$Name*") }
} else {
$WhereBlock = { ($_.PSChildName -match '^{[A-Z0-9]{8}-([A-Z0-9]{4}-){3}[A-Z0-9]{12}}$') -and ($_.GetValue('DisplayName')) }
}
$gciParams = @{
Path = $UninstallKey
ErrorAction = 'SilentlyContinue'
}
$selectProperties = @(
@{n='GUID'; e={$_.PSChildName}},
@{n='Name'; e={$_.GetValue('DisplayName')}}
)
Get-ChildItem @gciParams | Where $WhereBlock | Select-Object -Property $selectProperties
# msiexec /x 'GUID' /qn /norestart
}
}
}
# }
Get-InstalledSoftware2 -Name 'ScreenConnect' -OurName 'ScreenConnect Client (b3d049b2cd879dd9)'
注释掉命令后,我得到以下输出:
GUID Name
---- ----
{80E0C92B-A22E-4CCA-BB15-E7F8CAE95A96} ScreenConnect
{B92DB068-8FAF-4F4E-8ECC-13FF34DA74A5} ScreenConnect Client (b3d049b2cd879dd9)
但是,如果我删除 If 语句中的哈希值,我会得到 0 输出。我不应该得到第一个结果吗?
GUID Name
---- ----
{80E0C92B-A22E-4CCA-BB15-E7F8CAE95A96} ScreenConnect
感谢大家!
-戴夫
解决方案
您的代码看起来有点过于复杂。
使用它来获取计算机上已安装软件的列表可能更容易:
$Software = Invoke-Command -Computer $Computer -ScriptBlock {Get-CimInstance -ClassName Win32_Product}
从那里您可以查看标识号等。它还显示了本地 msi 的保存位置。
$Software | Select-Object Name,IdentifyingNumber,LocalPackage
所以一个示例函数可能是
param(
[Parameter(Mandatory=$true)]
[string[]]$Name
)
ForEach ($x in $Name) {
[array]$RemoveIDs += Get-CimInstance -ClassName Win32_Product |
Where-Object {$_.Name -match $x}
}
$RemoveIDs | ForEach-Object {
Sleep 10
$msi=$_.LocalPackage
#$msi=$_.IdentifyingNumber
& msiexec /x $msi /qn
}
推荐阅读
- java - 使用 MediaStore Api 录制音频
- mysql - 我的 SELECT 查询应该运行多快?0.25 秒似乎真的很慢
- c# - 在 Monogame 中按下键时,我需要帮助上下移动精灵
- reactjs - 无法将 sass 集成到 ReactJS 故事书
- javascript - Google App 脚本:附加多个图像时出错(在对象 DriveApp 上获取方法或属性 getFileById 时出现意外错误)
- android - Jetpack Compose 的 React useStyle
- python - 在 django model_formset 中获取使用 jquery 生成的 sub_forms 的数量
- python - 在 Pytorch 中使用 png 格式的图像创建图像数据集
- python - 为什么我收到“sqlite3.ProgrammingError:提供的绑定数量不正确。当前语句使用 5,并且提供了 4 个”错误
- android - 颤振构建apk反复安装平台工具