简体   繁体   English

Get-Package - PS CORE 不再提供已安装的软件

[英]Get-Package - PS CORE no longer provides installed software

I'm moving a yaml pipeline so it uses PS Core.我正在移动 yaml 管道,因此它使用 PS Core。 One of the steps is to parse currently installed software and uninstall it if it exists:其中一个步骤是解析当前安装的软件并在存在时将其卸载:


    $appsToUninstall = Get-Package -Provider Programs -IncludeWindowsInstaller -name "*$Instance*"
    if ($appsToUninstall.count -gt 0)
    {
        Get-Service | Where-Object { $_.Name -match "$Instance" } | Stop-Service
        $appsToUninstall | Uninstall-Package -Force -Verbose
        Write-Output "Uninstalled $Instance"
    }
    else
    {
        Write-Warning "Nothing to uninstall! Could not locate $Instance as an installed instance. Continuing as usual."
    }


However, it would seem that the new Get-Package - no longer provides installed software.但是,似乎新的 Get-Package - 不再提供已安装的软件。

不工作1 不工作2 作品

Is there any way to use native cmdlets (not CMI/WMI [wmi is deprecated?]) to achieve that in the new PS 7+?有没有办法在新的 PS 7+ 中使用本机 cmdlet(不是 CMI/WMI [wmi 已弃用?])来实现这一点?

You can parse the registry to achieve this:您可以解析注册表以实现此目的:

$Instance = "MyAppToUninstall"

# Initialize array to avoid errors on 32 bits applications addition
[array]$appsToUninstall = @()

# Get 64 bits programs
$appsToUninstall = Get-ItemProperty `
                   HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | 
                   Select DisplayName, UninstallString | 
                   Where { $_.DisplayName -like "*$Instance*" }

# Add 32 bits programs
$appsToUninstall += Get-ItemProperty `
                    HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | 
                    Select DisplayName, UninstallString | 
                    Where { $_.DisplayName -like "*$Instance*" }

if ($appsToUninstall.count -gt 0)
{
    Get-Service | Where-Object { $_.Name -match "$Instance" } | Stop-Service
    $appsToUninstall | ForEach-Object {
        $app = $_.UninstallString.Substring(0, $_.UninstallString.IndexOf(".exe") + 4)
        $arguments = $_.UninstallString.Substring($_.Uninstallstring.IndexOf(".exe") + 5)
        if ($app -match "(?i)msiexec")
        {
            # if MSI package, replace /i parameter by /x to uninstall and
            # add /passive parameter to automate the uninstallation
            $arguments = "$($arguments.Replace("/I", "/x", [system.StringComparison]::InvariantCultureIgnoreCase)) /passive"
        }
        Start-Process -FilePath $app -ArgumentList $arguments
    }
    Write-Output "Uninstalled $Instance"
}
else
{
    Write-Warning "Nothing to uninstall! Could not locate $Instance as an installed instance. Continuing as usual."
}

I hope this helps:)我希望这有帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Powershell`install-package`工作正常,但`get-package`不显示已安装的软件包 - Powershell `install-package` works fine, but `get-package` doesn't show the package installed 在获取包时抑制 PowerShell 中的错误消息 - Suppress Error Messages in PowerShell at get-package “获取包*记事本* | Uninstall-Package -Force”不起作用 - “Get-Package *notepad* | Uninstall-Package -Force” not working 获取包 | 卸载Package不起作用,没有错误 - Get-Package | Uninstall Package doesn't work, no error Get-Package 命令包含 xml 字符串 - 需要转换为 PSObject - Get-Package command includes xml string - Need to convert to PSObject PowerShell 7 “Get-Package”命令返回 NOTHING 作为结果。 PowerShell 5 RETURNS 同一台电脑上的包列表 - PowerShell 7 “Get-Package” command returns NOTHING as result. PowerShell 5 RETURNS list of packages on the same computer 如何获取已安装软件的列表 - How to get the List of Installed software 获取远程计算机安装的软件列表 - Get list of installed software of remote computer 获取远程计算机上已安装软件的版本 - Get version of Installed Software on remote machine PowerShell 命令获取安装的应用程序/软件版本 - PowerShell command to get installed application/software version
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM