首页 > 解决方案 > 尝试使用 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                          

感谢大家!

-戴夫

标签: powershellif-statementguid

解决方案


您的代码看起来有点过于复杂。

使用它来获取计算机上已安装软件的列表可能更容易:

$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
    } 

推荐阅读