Arrays Powershell数组键 Powershell中的键控阵列

Arrays Powershell数组键 Powershell中的键控阵列,arrays,powershell,Arrays,Powershell,我发现这个2015年的旧PowerShell文件删除了无用的Windows 10应用程序。非常有用。但出于代码压缩的原因,我观察到它是这样写的,我知道这只是重复语句的浪费: Write-Host -NoNewline "Removing Candy Crush App..." -ForegroundColor White Get-AppxPackage -AllUsers | Where-Object {$_.Name -like "king.com*"} | $ Write-Host "DON

我发现这个2015年的旧PowerShell文件删除了无用的Windows 10应用程序。非常有用。但出于代码压缩的原因,我观察到它是这样写的,我知道这只是重复语句的浪费:

Write-Host -NoNewline "Removing Candy Crush App..." -ForegroundColor White
Get-AppxPackage -AllUsers | Where-Object {$_.Name -like "king.com*"} | $
Write-Host "DONE" -ForegroundColor Green
Write-Host -Nonewline "Removing Twitter App..." -ForegroundColor White
Get-AppxPackage -AllUsers | Where-Object {$_.Name -like "*Twitter"} | Remove-AppxPackage
Write-Host "DONE" -ForegroundColor Green
Write-Host -Nonewline "Removing Facebook App..." -ForegroundColor White
Get-AppxPackage -AllUsers | Where-Object {$_.Name -like "*Facebook"} | Remove-AppxPackage
Write-Host "DONE" -ForegroundColor Green

...

# News / Sports / Weather
If ($App.DisplayName -eq "Microsoft.BingFinance")
{
    Write-Host -NoNewline "Removing Finance App..." -ForegroundColor Yellow
    Remove-AppxProvisionedPackage -Online -PackageName $App.PackageName | Out-Null
    Remove-AppxPackage -Package $App.PackageName | Out-Null
    Write-Host "DONE" -ForegroundColor Green
}

If ($App.DisplayName -eq "Microsoft.BingNews")
{
    Write-Host -NoNewline "Removing News App..." -ForegroundColor Yellow
    Remove-AppxProvisionedPackage -Online -PackageName $App.PackageName | Out-Null
    Remove-AppxPackage -Package $App.PackageName | Out-Null
    Write-Host "DONE" -ForegroundColor Green
}

...
我的想法:将每个应用程序及其在每个
Write Host
中显示的状态消息自动存储为键控数组,例如:

$Apps (
  [0] (
    [string]$StatusMessage = "Removing Candy Crush App...",
    [object]$AppObject = object
    [string]$AppType = "allusers"
   )

  ...

  [7] (
    [string]$StatusMessage = "Removing Bing News app..."
    [object]$AppObject = object
    [string]$AppType = "provisioned"
  )
  [8] (
    [string]$StatusMessage = "Removing Bing Finance app..."
    [object]$AppObject = object
    [string]$AppType = "provisioned"
  )

  ...
)
$Apps = Get-AppxPackage -AllUsers
$Apps += Get-AppxProvisionedPackage -Online
$AppHash = @{
    'king.com' =          [pscustomobject]@{
                              'StatusMessage' = "Removing Candy Crush App..."
                              'AppObject' = $Apps | Where{$_.Name -like 'king.com*'}
                              'AppType' = 'AllUsers'
                          }
    'Microsoft.BingNews' = [pscustomobject]@{
                              'StatusMessage' = "Removing Candy Crush App..."
                              'AppObject' = $Apps | Where{$_.Name -like 'king.com*'}
                              'AppType' = 'Provisioned'
                          }
}
以及我们来自configs的“黑名单”,我希望与
$Apps[key]匹配。appobject.Name

$Blacklist (
  "king.com",
  "*Twitter",
  "*Facebook",
  "Microsoft.BingFinance",
  "Microsoft.BingNews",
  ...
)
这样我就可以在一个简单的
中将每个$App作为$App
处理,如果需要将其作为配置应用程序而不是常规用户应用程序删除,则进行分叉,并将出色的过程作为用户自定义的ANSI条输出,因为我们有一个键控数组,其中包含大量实际存在的应用程序,这些应用程序肯定会与我们的黑名单相匹配。:-)


我如何将我们的应用程序数组存储在这样一个键控数组中,这样我就可以轻松地将每个$App作为$App在
中进行字符串匹配,以正确处理每个应用程序?`

听起来你想要的是一个哈希表。通过哈希表,您可以定义键和关联值。出于您的目的,我将为每个值创建一个PSCustomObject。大概是这样的:

$Apps (
  [0] (
    [string]$StatusMessage = "Removing Candy Crush App...",
    [object]$AppObject = object
    [string]$AppType = "allusers"
   )

  ...

  [7] (
    [string]$StatusMessage = "Removing Bing News app..."
    [object]$AppObject = object
    [string]$AppType = "provisioned"
  )
  [8] (
    [string]$StatusMessage = "Removing Bing Finance app..."
    [object]$AppObject = object
    [string]$AppType = "provisioned"
  )

  ...
)
$Apps = Get-AppxPackage -AllUsers
$Apps += Get-AppxProvisionedPackage -Online
$AppHash = @{
    'king.com' =          [pscustomobject]@{
                              'StatusMessage' = "Removing Candy Crush App..."
                              'AppObject' = $Apps | Where{$_.Name -like 'king.com*'}
                              'AppType' = 'AllUsers'
                          }
    'Microsoft.BingNews' = [pscustomobject]@{
                              'StatusMessage' = "Removing Candy Crush App..."
                              'AppObject' = $Apps | Where{$_.Name -like 'king.com*'}
                              'AppType' = 'Provisioned'
                          }
}
然后你就按照你描述的那样称呼它:

Write-Host $AppHash['king.com'].StatusMessage
$AppHash['king.com'].AppObject | Remove-AppxPackage

您需要在其中编写更多的逻辑来处理配置的内容,但这应该适合您所描述的内容。

听起来您需要的是一个哈希表。通过哈希表,您可以定义键和关联值。出于您的目的,我将为每个值创建一个PSCustomObject。大概是这样的:

$Apps (
  [0] (
    [string]$StatusMessage = "Removing Candy Crush App...",
    [object]$AppObject = object
    [string]$AppType = "allusers"
   )

  ...

  [7] (
    [string]$StatusMessage = "Removing Bing News app..."
    [object]$AppObject = object
    [string]$AppType = "provisioned"
  )
  [8] (
    [string]$StatusMessage = "Removing Bing Finance app..."
    [object]$AppObject = object
    [string]$AppType = "provisioned"
  )

  ...
)
$Apps = Get-AppxPackage -AllUsers
$Apps += Get-AppxProvisionedPackage -Online
$AppHash = @{
    'king.com' =          [pscustomobject]@{
                              'StatusMessage' = "Removing Candy Crush App..."
                              'AppObject' = $Apps | Where{$_.Name -like 'king.com*'}
                              'AppType' = 'AllUsers'
                          }
    'Microsoft.BingNews' = [pscustomobject]@{
                              'StatusMessage' = "Removing Candy Crush App..."
                              'AppObject' = $Apps | Where{$_.Name -like 'king.com*'}
                              'AppType' = 'Provisioned'
                          }
}
然后你就按照你描述的那样称呼它:

Write-Host $AppHash['king.com'].StatusMessage
$AppHash['king.com'].AppObject | Remove-AppxPackage

您需要在其中编写更多的逻辑来处理已配置的内容,但这应该适合您对所述内容的需要。

谢谢,我将尝试一下!谢谢,我来试试看!