Powershell [Ref]值不更新主对象

时间:2014-09-19 10:37:54

标签: powershell pass-by-reference

我在XP上运行Powershell V2: 这是一个更大的脚本的一部分,我注意到一些异常,我能够使用参考对象更新" master"宾语。这开始是为了避免每次都输入复杂的属性名称 - 我可以很容易地扩展到全名,但现在这种行为的原因严重困扰我。

[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和控制台中尝试了这一点,两者都有相同的行为。

1 个答案:

答案 0 :(得分:0)

我的猜测是,这是由域名中的点引起的。 将其减少到基础$ 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`""