首页 > 解决方案 > 如何比较这些对象?

问题描述

我在这里有一个逻辑,我想在我的脚本上使用它。我比较这两个对象的最佳方法是什么?

C:\Users\***> $value1

My Name 1
My Name 1

C:\Users\***> $Value2

My Name 1
My Name 1

C:\Users\***> if($value1 -eq $value2){ Write-Host "True"} else {Write-Host "False"}

++++++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++++ 这是我要用于逻辑的函数。我的 if 语句不断收到错误的回报

$NestedGroupUsers = Get-ADUsers_cachedV2 -GroupName $nestedmember.distinguishedName
          
    $table2 = $NestedGroupUsers | Add-Member -NotePropertyName MainParentGroup -NotePropertyValue $groupNestedName -PassThru  -Force 
    
    $AddedMainGroupTable = MainGroupChoice -InputObject $table2 -choice1 $adgroupname.name -Choice2 $groupNestedName -Value1  $table2.MainParentGroup -Value2 $table2.ParentGroup 
    
    function MainGroupChoice {
        param (
            [Parameter(ValueFromPipeline)]$InputObject,
            [Parameter(ValuefromPipelineByPropertyName = $true)] $Value1,
            [Parameter(ValuefromPipelineByPropertyName = $true)] $Value2,
            [Parameter(ValuefromPipelineByPropertyName = $true)][String] $Choice1,
            [Parameter(ValuefromPipelineByPropertyName = $true)][String] $Choice2
        )    
    
        process {
            # value1 = table2.parentGroup ,
            # value2 = table2.parentGroup 
            # choice1 = adgroupname, 
            #choice2 nestedmember,distinguishedname 
        
            if ($Value1 -eq $Value2 ) {
                return $InputObject |Add-Member -NotePropertyName MainGroup -NotePropertyValue $choice1 -PassThru  -Force
            }
            else {
                return $InputObject |Add-Member -NotePropertyName MainGroup -NotePropertyValue $Choice2  -PassThru  -Force
            }
        }
    
    }

为什么我对这些变得错误?我想在这里实现它。数据来自对象。我不断变得虚假

标签: powershell

解决方案


你得到了错误,因为你已经声明了 $Value2 变量,并且在 if 部分中你引用了不存在的 $value2 变量。只需更改if($value1 -eq $Value2){ Write-Host "True"} else {Write-Host "False"},您应该得到一个返回 True。或者在您再次声明它们时将 $Value2 更改为 $value2 。


推荐阅读