Как сделать массив с CNotLike - PullRequest
       1

Как сделать массив с CNotLike

0 голосов
/ 22 февраля 2019
$intel = "AppUp.IntelGraphicsControlPanel"
$nvidia = "NVIDIACorp.NVIDIAControlPanel"
$store = "*Store*"
Get-AppxProvisionedPackage -Online | Where-Object {$_.DisplayName -CNotLike $intel -and $_.DisplayName -CNotLike $nvidia -and $_.DisplayName -CNotLike $store} | Remove-AppxProvisionedPackage -Online -ErrorAction SilentlyContinue

Возможно ли использовать массив из-за слишком длинной строки с переменными?Что-то вроде (может быть)

$apps = @()
Foreach ($app in $apps)
{    }

Ответы [ 2 ]

0 голосов
/ 22 февраля 2019
$apps = @(  
"AppUp.IntelGraphicsControlPanel",  
"Microsoft.LanguageExperiencePackru-RU",  
"Microsoft.Windows.Photos",  
"Microsoft.ScreenSketch",  
"NVIDIACorp.NVIDIAControlPanel",  
"Store")  
(Get-AppxPackage -AllUsers | Where-Object {$_.Name -notmatch ($apps -join '|')}).name
0 голосов
/ 22 февраля 2019

Я думаю, вы можете сократить это повторение -cnotlike, используя регулярное выражение -cnotmath, например:

$re = "AppUp\.IntelGraphicsControlPanel|NVIDIACorp\.NVIDIAControlPanel|.*Store.*"
Get-AppxProvisionedPackage -Online | 
    Where-Object {$_.DisplayName -cnotmatch $re} | 
        Remove-AppxProvisionedPackage -Online -ErrorAction SilentlyContinue

Regex Подробности:

                                 Match this alternative (attempting the next alternative only if this one fails)
   AppUp                         Match the character string “AppUp” literally (case sensitive)
   \.                            Match the character “.” literally
   IntelGraphicsControlPanel     Match the character string “IntelGraphicsControlPanel” literally (case sensitive)
|
                                 Or match this alternative (attempting the next alternative only if this one fails)
   NVIDIACorp                    Match the character string “NVIDIACorp” literally (case sensitive)
   \.                            Match the character “.” literally
   NVIDIAControlPanel            Match the character string “NVIDIAControlPanel” literally (case sensitive)
|
                                 Or match this alternative (the entire match attempt fails if this one fails to match)
   .                             Match any single character that is NOT a line break character (line feed)
      *                          Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
   Store                         Match the character string “Store” literally (case sensitive)
   .                             Match any single character that is NOT a line break character (line feed)
      *                          Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...