无法检索嵌套属性

时间:2017-07-20 08:51:07

标签: powershell

我们有两个函数,一个函数叫做Get-MatrixADNamesHC,它根据一些字符串值生成AD对象名称,另一个函数叫Get-MatrixPermissionsHC,它使用这些值来创建新对象。

问题似乎是将值存储在$Obj.ADObject中。出于某种原因Debugger,在单步执行时,您可以看到它找到了正确的值并设置了它。但是当检查属性ADObject的结果时,此值为空。

代码:

Function Get-MatrixADNamesHC {
    [CmdLetBinding()]
    Param (
        [Parameter(Mandatory)]
        [ValidateCount(3,9999)]
        [Array]$Matrix,
        [Parameter(Mandatory)]
        [String]$Begin,
        [Parameter(Mandatory)]
        [String]$Middle,
        [ValidateNotNullOrEmpty()]
        [String]$BeginTemplate = 'GroupName',
        [ValidateNotNullOrEmpty()]
        [String]$MiddleTemplate = 'SiteCode'
    )

    Process {
        Try {
            $Matrix | Select-Object -Index 2 | Get-Member | where {
            ($_.Name -ne ($Matrix[0].PSObject.Properties).Name[0]) -and 
            ($_.MemberType -EQ 'NoteProperty')} | ForEach-Object {

                $Header = $_.Name
                $Value = $Matrix.($_.Name)
                $End = $Matrix[0].($_.Name)
                $SamAccountName = $null

                $BeginResult = Switch ($Value[2]) {
                    $BeginTemplate {$Begin  + ' ' ;break}
                    ''             {break}
                    Default        {$Value[2] + ' '}
                }

                $MiddleResult = Switch ($Value[1]) {
                    $MiddleTemplate  {$Middle + ' ' ;break}
                    ''               {break}
                    Default          {$Value[1] + ' '}
                }

                if ($BeginResult -or $MiddleResult -or $End) {
                    $SamAccountName = ($BeginResult + $MiddleResult + $End).Trim()
                }

                Write-Verbose "Generated from the matrix AD Name '$SamAccountName'"                 
                [PSCustomObject]@{
                    Path = $_.Name
                    Name = $SamAccountName
                    Input = [PSCustomObject]@{
                        Begin  = $Value[2]                                                
                        Middle = $Value[1]
                        End    = $End
                    }
                }    
            }
        }
        Catch {
            throw "Failed generating the correct AD Name for $BeginTemplate '$Begin' and $MiddleTemplate '$Middle': $_"
        }
    }
}


Function Get-MatrixPermissionsHC {
    [CmdletBinding()]
    Param (
        [parameter(Mandatory)]
        [Array]$Matrix,
        [parameter(Mandatory)]
        [String]$GroupName,
        [parameter(Mandatory)]
        [String]$SiteCode
    )

    Process {
        Try {
            $ADObjectParams = @{
                Begin  = $GroupName
                Middle = $SiteCode
                Matrix = $Matrix
            }
            $ADObjects = Get-MatrixADNamesHC @ADObjectParams

            foreach ($M in ($Matrix | Select-Object -Skip 3)) {
                $Obj = [PSCustomObject]@{
                    Path     = if ($M.P1) {($M.P1).Trim('\', ' ')} else {$null}
                    ADObject = $null
                    ACE      = $null
                    Parent   = $false
                }

                if (-not $FirstTimeThrough) {
                    $FirstTimeThrough = $true                                        
                    $Obj.Parent = $true
                }

                $M | Get-Member | where {($_.Name -ne ($Matrix[0].PSObject.Properties).Name[0]) -and 
                ($_.MemberType -EQ 'NoteProperty')} | ForEach-Object {
                    $Obj.ADObject = $ADObjects | where Path -eq $_.Name | Select Name, Input
                    $Obj.ACE = $M.($_.Name)
                    $Obj
                    Write-Verbose "Permission '$($Obj.ACE)' on '$($Obj.Path)' for '$($Obj.ADObject.Name)'"
                }
            }
        }
        Catch {
            throw "Failed getting the permissions from the matrix with GroupName '$GroupName' and SiteCode '$SiteCode': $_"
        }
    }
}

$Matrix = @(
    [PSCustomOBject]@{
        P1 = 'R Read / W Write' 
        P2 = 'Manager'
        P3 = 'Directors'
        P4 = ''
        P5 = 'All users'
        P6 = 'cnorris'
        P7 = 'Support staff'
        P8 = ''
    }
    [PSCustomOBject]@{
        P1 = 'SiteCode'
        P2 = 'SiteCode'
        P3 = 'SiteCode'
        P4 = 'camerica'
        P5 = ''
        P6 = $null
        P7 = ''
        P8 = ''
    }
    [PSCustomOBject]@{
        P1 = 'GroupName'
        P2 = 'GroupName'
        P3 = 'BEL TEAM'
        P4 = ''
        P5 = 'GroupName'
        P6 = ''
        P7 = 'BEL ROL-STAFF-IT'
        P8 = ''
    }
    [PSCustomOBject]@{
        P1 = 'Path'
        P2 = 'L'
        P3 = 'L'
        P4 = 'L'
        P5 = 'L'
        P6 = 'L'
        P7 = 'L'
        P8 = 'L'
    }
)

$Test = Get-MatrixPermissionsHC -Matrix $Matrix -GroupName 'BEL ROL-TEAM' -SiteCode 'Logistics' -Verbose

$Test

$Test.ADObject.Name
$Test.ADObject.Input
$Test[0] | fl *
$Test[0].ADObject.Input | fl *

无论我尝试什么,我都无法获得ADObject.InputADObject.Name的价值。我可能会遗漏一些非常明显的东西,但我无法看到它。

1 个答案:

答案 0 :(得分:0)

真奇怪......最后我必须像这样解决它:

Function Get-MatrixPermissionsHC {
    [CmdletBinding()]
    Param (
        [parameter(Mandatory)]
        [Array]$Matrix,
        [parameter(Mandatory)]
        [String]$GroupName,
        [parameter(Mandatory)]
        [String]$SiteCode
    )

    Process {
        Try {
            $ADObjectParams = @{
                Begin  = $GroupName
                Middle = $SiteCode
                Matrix = $Matrix
            }
            $ADObjects = Get-MatrixADNamesHC @ADObjectParams

            $FirstTimeThrough = $false

            foreach ($M in ($Matrix | Select-Object -Skip 3)) {
                $Path = if ($M.P1) {($M.P1).Trim('\', ' ')} else {$null}
                $Parent = $false

                if (-not $FirstTimeThrough) {
                    $FirstTimeThrough = $true                                        
                    $Parent = $true
                }

                $M | Get-Member | where {($_.Name -ne ($Matrix[0].PSObject.Properties).Name[0]) -and 
                ($_.MemberType -EQ 'NoteProperty')} | ForEach-Object {
                    $Obj = [PSCustomObject]@{
                        Path     = $Path
                        ADObject = $ADObjects | where Path -EQ $_.Name | Select * -ExcludeProperty Path
                        Ace      = $M.($_.Name)
                        Parent   = $Parent
                    }

                    $Obj
                    Write-Verbose "Permission '$($Obj.ACE)' on '$($Obj.Path)' for '$($Obj.ADObject.Name)'"
                }
            }
        }
        Catch {
            throw "Failed getting the permissions from the matrix with GroupName '$GroupName' and SiteCode '$SiteCode': $_"
        }
    }
}

[PSCustomObject]循环中显示部分foreach至关重要。