gpt4 book ai didi

Powershell [Ref] 值未更新主对象

转载 作者:行者123 更新时间:2023-12-03 09:50:53 27 4
gpt4 key购买 nike

我在 XP 上运行 Powershell V2:这是一个更大的脚本的一部分,我注意到我使用引用对象更新“主”对象的方式存在一些异常。这开始是为了避免每次都输入复杂的属性名称 - 我可以轻松地扩展到全名,但现在这种行为的原因严重困扰着我。

[CmdletBinding()]
Param()
Function Breakhere {Write-Verbose " "}
Set-PSBreakpoint -Command Breakhere
$Data = @"
UserID
MyUser1
MyUser2
User3
"@
$Data = $Data|ConvertFrom-Csv

$Domains = "DomainA","DomainB"

$Props = "ParentContainer","AccountIsDisabled","employeeNumber"
$Connection = New-Object HashTable

ForEach ($Domain in $Domains)
{
Write-Verbose "Current Domain: $Domain"
# Add necessary headers to main data
$text1 = "$($Domain)_ADObject"
$text2 = "$($Domain)_Confidence"
$Data = $Data |Select *,$text1
$Data = $Data |Select *,$text2

#Bind to each domain and save the connection contexts into a hashtable
Write-Verbose "Binding to $Domain"
$Connection.Add($Domain,(Connect-QADService -service $Domain))
}

ForEach ($User in $Data)
{
ForEach ($Domain in $Domains)
{
$User."$($Domain)_ADObject" = Get-QADUser -Connection $Connection[$Domain] -SamAccountName $User.UserID -DontUseDefaultIncludedProperties -IncludedProperties $Props|Select $Props

# Referencing the confidence parameter does not seem to work.
$CF = [ref]$User."$($Domain)_Confidence"
# Weirdly, this one does work.
$AD = [ref]$User."$($Domain)_ADObject"

If ($AD.Value)
{
$CF.Value = 1
Breakhere # Break here and allow for opportunity to inspect $user and $CF objects

If ($AD.Value.AccountIsDisabled)
{
Write-Verbose "$Domain\$($User.UserID): Account Disabled"
$CF.Value *= 0.8
}
}
Else
{
Write-Verbose "$Domain\$($User.UserID): No AD Object found"
$CF.Value = 0
}
}
} #End ForEach $UserID

在断点处,如果我查询 $User,我会收到类似于以下内容的信息:

UserID             : MyUser1
DomainA_ADObject : @{ParentContainer=DomainA/Users; AccountIsDisabled=False; employeeNumber=123456}
DomainA_Confidence :
DomainB_ADObject :
DomainB_Confidence :

一切顺利。如果我愿意,我什至可以使用 $AD ref 对象并更新 DomainA_ADobject:

$AD.Value.employeeNumber = 9999
$User

UserID : MyUser1
DomainA_ADObject : @{ParentContainer=DomainA/Users; AccountIsDisabled=False; employeeNumber=9999}
DomainA_Confidence :
DomainB_ADObject :
DomainB_Confidence :

但是,用 $CF ref 试试这个,同样的事情不会发生

$CF.Value = 2
$CF

Value
-----
2

$User

UserID : MyUser1
DomainA_ADObject : @{ParentContainer=DomainA/Users; AccountIsDisabled=False; employeeNumber=9999}
DomainA_Confidence : *<====== Expecting this to update!*
DomainB_ADObject :
DomainB_Confidence :

为什么不同?有什么方法可以查询 [ref] 对象并查看它指向什么?我不明白为什么其中一个有效而另一个无效。它们似乎都以相同的方式设置。在 ISE 和控制台中试过这个,两者的行为相同。

最佳答案

我的猜测是,这是由域名中的点引起的。将其简化为基础 $CF.Value = 1 会做一些类似的事情

$Data[0].domain.name.net.au_Confidence.value = 1

这是行不通的。这将:

$Data[0]."domain.name.net.au_Confidence".value = 1

也许这会解决它?:

$CF = [ref]$User."`"$($Domain)_Confidence`""

关于Powershell [Ref] 值未更新主对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25931995/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com