当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 比较 PowerShell 属性名称

作者:精品下载站 日期:2024-12-14 08:02:06 浏览:16 分类:玩电脑

比较 PowerShell 属性名称


[玩转系统] 比较 PowerShell 属性名称

最近,我和我的朋友 Gladys Kravitz 讨论了在 PowerShell 中比较对象的麻烦。即使过了这么多年。她有一个特定的用例,但您可能也觉得需要更好的比较选项。需要明确的是,我们讨论的比较不是对象的值,正如您在 Compare-Object 中看到的那样。而是属性名称

在 Gladys 的情况下,她从 CSV 文件导入数据,并通过脚本进行处理以完成工作。她的用例是比较属性名称。此 CSV 文件是否与该 CSV 文件具有相同的属性名称?有额外的属性吗?或者缺少什么?那么让我们看看如何解决这个问题。

思考对象

尽管我们从 CSV 文件开始,但我们实际上讨论的是对象。 Gladys 可以导入她的 CSV 文件并将其转换为自定义对象。您可能正在创建对象以通过某种其他机制进行比较。最重要的是,我们应该将此视为对象比较问题,而不是文件问题。

让我首先生成一些示例数据。

$csvA = @"
samantha,darren-1,darren-2,tabitha,esmerelda
1,2,3,4,5
2,4,6,8,10
3,6,9,11,12
11,22,33,44,55
"@

$csvB = @"
samantha,darren-1,darren-2,esmerelda,gladys,agatha
1,2,3,4,0,9
2,4,6,8,1,8
3,6,9,11,8,0
11,22,33,55,66,77
"@

$a = $csvA | ConvertFrom-Csv
$b = $csvB | ConvertFrom-Csv

就我而言,$A 是参考对象,$B 是差异对象。在查看数据时,您可以看到 $B 缺少“tabitha”,并且具有额外的属性“agatha”和“gladys”。我不关心价值观。我们甚至并不真正关心属性名称的顺序,因为我们有一个对象,我们将通过属性名称引用,而不是它在 CSV 标头中的位置。

PS对象

要比较对象属性名称,我们不需要整个对象。我们只需要分析一项,因为对象的属性名称是相同的。 PowerShell 付出了很多努力来使其易于 IT 专业人员使用。 PowerShell 隐藏了许多 .NET 香肠制作过程,因此您只需处理最终结果。但有时,我们需要亲自动手。在本例中,我们将使用一个名为 PSObject 的普遍属性。

[玩转系统] 比较 PowerShell 属性名称

我们将使用 Properties 属性。它看起来是这样的。

[玩转系统] 比较 PowerShell 属性名称

然后很容易为引用和差异对象创建属性名称列表。

$refProp = $a[0].psobject.properties.name | Sort-Object
$diffProp = $b[0].psobject.properties.name | Sort-Object

然后我可以浏览每个列表并查看缺少或额外的属性。

#find extra  properties in the difference object
foreach ($name in $diffProp) {
    if ($refProp -notcontains $name) {
      $name
    }
} #foreach

#find missing reference properties from the difference object
foreach ($name in $refProp) {
    if ($diffProp -notcontains $name) {
       $name
    }
} #foreach

当然,我想创建一个有意义的结果,以便格拉迪斯一眼就能看出事物的比较情况。

比较属性名称

这是我编写的 PowerShell 函数。

Function Compare-PropertyName {
    [cmdletbinding(DefaultParameterSetName="default")]
    [alias("cpn")]
    [Outputtype("PSPropertyNameDifference","string","boolean")]
    Param(
        [Parameter(Position = 0, Mandatory)]
        [ValidateNotNullOrEmpty()]
        [object]$Reference,
        [Parameter(Position = 1, Mandatory)]
        [ValidateNotNullOrEmpty()]
        [object]$Difference,
        [Parameter(HelpMessage = "Indicate if property names match with a simple True/False.",ParameterSetName="quiet")]
        [switch]$Quiet,
        [Parameter(HelpMessage = "Only show missing property names in the difference object.",ParameterSetName="missing")]
        [switch]$MissingOnly,
        [Parameter(HelpMessage = "Only show extra property names in the difference object.",ParameterSetName="extra")]
        [switch]$ExtraOnly
    )

    #get property names from the first item
    $refProp = $Reference[0].psobject.properties.name | Sort-Object
    Write-Verbose "Found $(($refProp).count) reference properties"

    $diffProp = $Difference[0].psobject.properties.name | Sort-Object
    Write-Verbose "Found $(($diffProp). count) difference properties"

    $missing = [System.Collections.Generic.List[string]]::new()
    $extra = [System.Collections.Generic.List[string]]::new()

    #find extra  properties in the difference object
    foreach ($name in $diffProp) {
        if ($refProp -notcontains $name) {
            Write-Verbose "$Name not found in the reference object"
            $extra.Add( $name)
        }
    } #foreach

    #find missing reference properties from the difference object
    foreach ($name in $refProp) {
        if ($diffProp -notcontains $name) {
            Write-Verbose "$Name not found in the difference object"
            $missing.Add( $name)
        }
    } #foreach

    #create a custom object for the reseult
    $result = [pscustomobject]@{
            PSTypename = "PSPropertyNameDifference"
            Missing = $missing
            Extra = $Extra
            ReferenceProperties = $refProp
            DifferenceProperties = $diffProp
            ReferenceCount = $refProp.count
            DifferenceCount = $diffProp.count
         }

         switch ($pscmdlet.ParameterSetName) {
            "Quiet" {
                if ($result.missing.count -eq 0 -AND $result.extra.count -eq 0) {
                    $true
                }
                else {
                    $false
                }
            }
            "Missing" {
                $result.Missing
            }
            "Extra" {
                $result.Extra
            }
            Default {
                $result
            }
         } #switch


} #close function

该函数默认生成一种类型的对象。

[玩转系统] 比较 PowerShell 属性名称

但我也想提供替代方案,以便快速为格拉迪提供她可能想要的信息。

[玩转系统] 比较 PowerShell 属性名称

结果都是相对于差异对象而言的。例如,差异对象 $b 缺少属性“tabitha”。

我很想听听这对您有何作用。我认为这将为 PSScriptTools 模块提供一个很好的补充,因此请在接下来的几周内进行更新。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯