简体   繁体   English

将对象与 powershell 中的通配符字符串匹配

[英]Match objects to wildcarded strings in powershell

Writing a script to get rid of certain Windows 10 bloatware and having some issues.编写脚本以摆脱某些 Windows 10 膨胀软件并遇到一些问题。

The intention is to check if the app exists before going ahead and removing it with Remove-AppXPackage.目的是在继续之前检查应用程序是否存在并使用 Remove-AppXPackage 将其删除。 I added all the names to an array of strings with and with asterisks on both sides but I can't get a match while using -like or -match in my if statement.我将所有名称添加到字符串数组中,两边都带有星号和星号,但是在 if 语句中使用 -like 或 -match 时无法匹配。

I wrote the code below to try to make it work exactly as the following command:我写了下面的代码,试图让它完全按照以下命令工作:

Get-AppxPackage *windowsalarms* | Remove-AppxPackage

But instead, first check if that package exists.但相反,首先检查该包是否存在。 I'm assuming the original command is matching objects to eachother.我假设原始命令是相互匹配对象。 And I'm realizing I'm trying to compare an object to string... So should I create an array of objects with just a name property for every single one and then try to compare them?而且我意识到我正在尝试将一个对象与字符串进行比较......那么我应该为每个对象创建一个只有一个 name 属性的对象数组,然后尝试比较它们吗?

$AppsToRemove = @("*broker*"
                ,"3dbuilder"
                ,"windowsalarms"
                ,"officehub"
                ,"skypeapp"
                ,"getstarted"
                ,"zunemusic"
                ,"solitarecollection"
                ,"bingfinance"
                ,"zunevideo"
                ,"bingnews"
                ,"windowsphone"
                ,"bingsports"
                ,"xboxapp"
                ,"whiteboard"
                ,"sway"
                ,"todos"
                ,"lens"
                ,"soundrecorder"
                ,"onenote"
                ,"remotedesktop"
                ,"networkspeedtest")

Get-AppxPackage | ForEach-Object {

$Placeholder = $_.Name

    Foreach ($app in $AppsToRemove){

        If ($app -like $Placeholder) {
            Write-Host "Match"


        }else{
            Write-Host "No match"

        }

    }

}

Try something like this where you join your array into a [Regex] object.尝试这样的事情,将数组连接到 [Regex] 对象中。 Then I got all the AppXPackages and piped that to a Where-Object to tell if they matched the original array.然后我获取了所有 AppXPackages 并将其通过管道传输到 Where-Object 以判断它们是否与原始数组匹配。 I added a -WhatIf at the end for testing.我在最后添加了一个-WhatIf进行测试。 This can be removed when you want to actually run the script to remove them.当您想要实际运行脚本以删除它们时,可以将其删除。

[Regex] $AppsToRemove = @(
    'broker'
    '3dbuilder'
    'windowsalarms'
    'officehub'
    'skypeapp'
    'getstarted'
    'zunemusic'
    'solitarecollection'
    'bingfinance'
    'zunevideo'
    'bingnews'
    'windowsphone'
    'bingsports'
    'xboxapp'
    'whiteboard'
    'sway'
    'todos'
    'lens'
    'soundrecorder'
    'onenote'
    'remotedesktop'
    'networkspeedtest'
) -join '|'

Get-AppxPackage | Where-Object -Property Name -Match -Value $AppsToRemove | Remove-AppxPackage -Verbose -WhatIf

I get the following output given that some of these are already removed on my machine:鉴于其中一些已在我的机器上删除,我得到以下输出:

What if: Performing the operation "Remove package" on target "Microsoft.AAD.BrokerPlugin_1000.16299.15.0_neutral_neutral_cw5n1h2txyewy".
What if: Performing the operation "Remove package" on target "Microsoft.WindowsAlarms_10.1804.1101.1000_x64__8wekyb3d8bbwe".
What if: Performing the operation "Remove package" on target "Microsoft.WindowsSoundRecorder_10.1804.911.1000_x64__8wekyb3d8bbwe".
What if: Performing the operation "Remove package" on target "Microsoft.ZuneVideo_10.18082.10311.0_x64__8wekyb3d8bbwe".
What if: Performing the operation "Remove package" on target "Microsoft.ZuneMusic_10.18091.10321.0_x64__8wekyb3d8bbwe".

The answer by @ShawnEsterman guided me to an idea that I'm more familiar with and I came up with the following that worked: @ShawnEsterman 的回答引导我想到了一个我更熟悉的想法,我想出了以下可行的方法:

$i = 0
$AppsToRemove = @("broker"
                ,"3dbuilder"
                ,"windowsalarms"
                ,"officehub"
                ,"skypeapp"
                ,"getstarted"
                ,"zunemusic"
                ,"solitarecollection"
                ,"bingfinance"
                ,"zunevideo"
                ,"bingnews"
                ,"windowsphone"
                ,"bingsports"
                ,"xboxapp"
                ,"whiteboard"
                ,"sway"
                ,"todos"
                ,"lens"
                ,"soundrecorder"
                ,"onenote"
                ,"remotedesktop"
                ,"networkspeedtest")

$Packages = Get-AppxPackage | ForEach-Object {

Foreach ($App in $AppsToRemove){
    if($_ -match $App){

        Remove-AppxPackage $_
    }
}

} }

Thanks Shawn for your guidance!感谢肖恩的指导!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM