我在 Powershell 中有一组自定义对象。每个对象都有一个可以在数组中查找的唯一引用和一个我希望稍后使用的值,该值不一定是唯一的。该数组填充在 for 循环中,如下所示
#Create empty array
$array1 = @()
#Populate array
foreach($otherElement in $otherArray){
#Create custom array element
$arrayElement = New-Object PSObject
#Update Credential element
$arrayElement | Add-Member -type NoteProperty -Name 'objRef' -Value $($otherElement.getAttribute("ref") )
$arrayElement | Add-Member -type NoteProperty -Name 'objValue' -Value $($otherElement.getAttribute("someValue") )
#Add element to array
$array1 += $arrayElement
}
Run Code Online (Sandbox Code Playgroud)
构建数组后,我想以某种方式访问与正确objRef对应的objValue。我知道您可以使用-contains参数测试数组是否包含一个值,但我不知道如何使用该值获取对象的索引。
基本上这个数组就像一个查找表。我想要一个函数来放入 objRef 以获取 objValue。
在这种情况下 objValue 是一个System.Management.Automation.PSCredential,为了安全起见,每个对象的密码都是在运行时输入的。在工作中,我有时必须使用相同的 5 个凭据在不同的机器上安装相同的软件 30 次,弄清楚这一点将帮助我远程自动化该过程。
提前致谢!