[英]How to compare two custom ps custom objects and change the value of one based on the result of another?
Currently I have two sql queries that I have outputting the following columns目前我有两个 sql 查询,我有输出以下列
$A | Select-Object -Property DatabaseUserName, Role, PermissionType, PermissionState, ObjectType, ObjectName, col
AND和
$B | Select-Object -Property DatabaseUserName, sysadmin
What I want to do in psuedo code: WHERE $B.DatabaseUserName = $A.DatabaseUserName THEN $A.col = $B.sysadmin.我想在伪代码中做什么:WHERE $B.DatabaseUserName = $A.DatabaseUserName THEN $A.col = $B.sysadmin。 Currently I have it setup the following way:
目前我通过以下方式设置它:
$allUsers = $A | % {
New-Object psobject -Property @{
DatabaseUserName = $_.DatabaseUserName
Role = $_.Role
PermissionType = $_.PermissionType
PermissionState = $_.PermissionState
ObjectType = $_.ObjectType
ObjectName = $_.ObjectName
col = 0
}
}
$allUsers += $B | Where-Object {$_.DatabaseUserName -like $B.DatabaseUserName} | % {
New-Object psobject -Property @{
$_.col = $B.sysadmin
}
}
This still leaves all the columns to be 0 from what I can see when I want them to actually be the value of $B.sysadmin.当我希望它们实际上是 $B.sysadmin 的值时,这仍然使所有列从我可以看到的内容中保持为 0。 I am not sure if this is the correct approach.
我不确定这是否是正确的方法。
Your Where-Object clause doesn't make much sense.您的 Where-Object 子句没有多大意义。 What it says is "Where a property of one instance of array B is like the entire array of properties".
它所说的是“数组 B 的一个实例的属性就像整个属性数组”。 -like is used to compare stings.
-like 用于比较蜇伤。 -contains for arrays...but that wouldn't fix your code either....
-包含数组......但这也不能修复你的代码......
If I understand what you're asking in your pseudocode, you want to assign a property of $B
to a different property of $A
when the DatabaseUserName
property matches.如果我知道你要问你什么伪代码,想要的属性分配
$B
到一个不同的属性$A
的时候DatabaseUserName
属性相匹配。 However the code you posted assigns an object to $allusers
for each element in $A
.但是,您发布的代码为
$A
每个元素为$allusers
分配了一个对象。 I am going to assume you want to update the objects in the $allusers
array rather than in the $A
directly.我将假设您要更新
$allusers
数组中的对象,而不是直接更新$A
。 To do that, try this:要做到这一点,试试这个:
Foreach ($au in $allusers) {
$au.col = ($B | ? {$_.DatabaseUserName -eq $au.DatabaseUserName}).col
}
The above code loops through each item in $allusers
and sets the .col
property.上面的代码循环遍历
$allusers
每个项目并设置.col
属性。 Note, this code works only if you have one and only one instance of $B
which meets your criteria.请注意,此代码仅在您有一个且只有一个符合条件的
$B
实例时才有效。 If you have 0, .col
could equal $null
or blank.如果您有 0,
.col
可能等于$null
或空白。 If you have more than one which meets the criteria, then your .col
value would be an array.如果您有多个满足条件,那么您的
.col
值将是一个数组。
I figured this out by doing a nested for each like so我通过为每个人做一个嵌套来解决这个问题
$AllUsers = $svrInfo | % {
New-Object psobject -Property @{
DatabaseUserName = $_.DatabaseUserName
Role = $_.Role
PermissionType = $_.PermissionType
PermissionState = $_.PermissionState
ObjectType = $_.ObjectType
ObjectName = $_.ObjectName
col = 0
}
}
foreach ($admin in $sysAdmin)
{
foreach ($User in $AllUsers)
{
if($User.DatabaseUserName -contains $admin.DatabaseUserName)
{
Write-Host("$User.DatabaseUserName matches $admin.DatabaseUserName")
$User.col = 1
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.