powershell - 如何比较这些对象?
问题描述
我在这里有一个逻辑,我想在我的脚本上使用它。我比较这两个对象的最佳方法是什么?
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
}
}
}
为什么我对这些变得错误?我想在这里实现它。数据来自对象。我不断变得虚假
解决方案
你得到了错误,因为你已经声明了 $Value2 变量,并且在 if 部分中你引用了不存在的 $value2 变量。只需更改if($value1 -eq $Value2){ Write-Host "True"} else {Write-Host "False"}
,您应该得到一个返回 True。或者在您再次声明它们时将 $Value2 更改为 $value2 。
推荐阅读
- javascript - 是否可以创建一个可以使用 JavaScript 从 safari 访问火炬的网络应用程序?
- angular - 角材料从桶进口转向单独进口有什么好处 9
- asp.net - 连接/检查会话无限循环身份服务器 4 v4 asp.net 核心与反应应用程序
- uwp - 如何将 UWP 应用程序迁移到 .Net 中的桌面应用程序
- angular - Angular - 通过@Input 属性进行依赖注入
- c# - 文本框不会更新以处理输出,但 ShowMessage 会(C#)
- excel - 撤消 Workbook_BeforeClose 事件
- javascript - 数组上“大于”(和另一个不等式比较运算符)的行为
- c# - 无法加载文件或程序集“石英,版本=3.0.7.0,文化=中性,PublicKeyToken=f6b8c98a402cc8a4”
- c# - 光子库离线模式问题