Powershell[Ref]值未更新主对象
我正在XP上运行Powershell V2: 这是一个更大的脚本的一部分,我注意到我能够使用引用对象来更新“主”对象的方式出现了一些异常。这一开始是为了避免每次都输入复杂的财产名称——我可以很容易地扩展到全名,但现在这种行为的原因严重困扰着我Powershell[Ref]值未更新主对象,powershell,pass-by-reference,Powershell,Pass By Reference,我正在XP上运行Powershell V2: 这是一个更大的脚本的一部分,我注意到我能够使用引用对象来更新“主”对象的方式出现了一些异常。这一开始是为了避免每次都输入复杂的财产名称——我可以很容易地扩展到全名,但现在这种行为的原因严重困扰着我 [CmdletBinding()] Param() Function Breakhere {Write-Verbose " "} Set-PSBreakpoint -Command Breakhere $Data = @" UserID MyUser1 M
[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 :
$CF.Value=2
$CF
价值
-----
2.
$User
UserID:MyUser1
DomainA_ADObject:@{ParentContainer=DomainA/Users;AccountIsDisabled=False;employeeNumber=9999}
域名自信:我的猜测是,这是由域名名称中的点引起的。
将其简化为基本值$CF.Value=1
$Data[0]。domain.name.net.au_Confidence.value=1
这是行不通的。这将:
$Data[0]“domain.name.net.au_Confidence”。值=1
也许这能解决问题
$CF = [ref]$User."`"$($Domain)_Confidence`""
我正在经历类似的事情,似乎找不到解释[ref]
不应该在这里工作。它用于调用带有ref
参数的方法,而不是用于引用属性。