powershell - 在 PowerShell 中对属性上的对象进行分组

标签 powershell calculated-columns

我希望将 Application 和 ShortcutName 列分组为一列,同时保持 Version 和 Architecture 列不变:


$object | Select-Object -Property Application, Version, Architecture, ShortcutName |
  Sort-Object -Property @{expression="Architecture";Descending=$true},Application | 
    Out-GridView

我怎样才能做到这一点 ?

最佳答案

在 PowerShell 中,您可以使用 Group-Object 按属性对对象进行分组.您想要的不是那种意义上的分组,而是定义一个新的自定义属性。

您可以使用 Name 创建自定义属性哈希表和 Expression键。

像这样的事情可能会成功。

$Expression = {
    if ($_.Application) {
        $_.Application
    } 
    else {
        $_.Shortcut
    } 
}

$object | 
    Select-Object -Property @{ Name = 'Application'; Expression = $Expression }, Version, Architecture | 
    Sort-Object -Property @{expression="Architecture";Descending=$true},Application | 
    Out-GridView

该表达式检查 Application 是否属性(property)已满。如果是这样,它将使用现有的应用程序名称,否则将使用 Shortcut属性作为应用程序名称。

关于powershell - 在 PowerShell 中对属性上的对象进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53466437/

相关文章:

powershell - 使用 Powershell 获取上次登录时间、计算机和用户名

Sharepoint:计算列替换所有空格

variables - 如何强制全局变量的类型?

python - conda激活没有效果

powershell - 错误获取-AzureKeyVaultSecret : Unable to retrieve service key for ServicePrincipal account

PowerShell:在标准输出中选择字符串?

mysql - 使用变量计算的 MySQL 列未返回正确的结果

python - 对每对字段执行计算

sql - 对计算日期字段的操作

sql - 我可以在计算列上创建索引吗?