自定义对象导出数组并添加到数组

时间:2018-09-10 15:19:27

标签: arrays powershell object

我很难理解自己的缺失。 我正在尝试做2件事情,我正在尝试构建自定义对象数组,然后再将其添加到具有更多数据的自定义对象数组中。然后,最后将完整的自定义对象数组导出到csv。我知道我已经接近了,但是我得到的只是自定义对象数组的“对象”。

对象在Add-ArraySiteDefaultvsUserSettings中完整显示,当它离开该函数时,但是我无法在该对象上构建对象,因此很有用。

    Function Add-DefaultsValidation 
{
    #New-Object psobject -Property @{IdenTity = '';SiteDefault = '';UserSetting = '';IssueAlert =''}
    $DefaultsObject = New-Object Object -TypeName PSObject
    Add-Member -MemberType NoteProperty -Name SiteCode -Value "" -InputObject $DefaultsObject  
    Add-Member -MemberType NoteProperty -Name Identity -Value "" -InputObject $DefaultsObject
    Add-Member -MemberType NoteProperty -Name OU -Value "" -InputObject $DefaultsObject    
    Add-Member -MemberType NoteProperty -Name SiteDefault -Value "" -InputObject $DefaultsObject  
    Add-Member -MemberType NoteProperty -Name UserSetting -Value "" -InputObject $DefaultsObject  
    Add-Member -MemberType NoteProperty -Name IssueAlert -Value "" -InputObject $DefaultsObject  
    $DefaultsObject
}

Function Add-ArraySiteDefaultvsUserSettings
{
    Param ( $OU, $Identity, $SiteCode, $SiteDefaultValue, $UserCurrentSettings, $AlertType )

    #SiteDefaultvsUserSettings
    [array]$DefaultvsUserSettings = @()
    $addObject = Add-DefaultsValidation
    $addObject.SiteCode = $SiteCode
    $addObject.Identity = $Identity
    $addObject.OU = $OU
    $addObject.SiteDefault = $SiteDefaultValue
    $addObject.UserSetting = $UserCurrentSettings
    $addObject.IssueAlert = $AlertType
    $DefaultvsUserSettings += $addObject
    #Write-Output $SiteDefaultvsUserSettings

    Return $DefaultvsUserSettings
}

foreach ($SkypeSiteDefault in $SkypeSiteDefaults)
{

    if ($SkypeSiteDefault.OU -ne $null)
    {
        $SkypeSiteDefault.'Site Code'
        $SkypeSiteDefault.RegistrarPool
            $SkypeUsersRegistrarInvalid = Get-CsUser -ResultSize unlimited -ou $SkypeSiteDefault.OU | Where-Object {$_.RegistrarPool.FriendlyName -notlike "*$($SkypeSiteDefault.RegistrarPool)*"} | Select SamAccountName, Registrarpool 
            if ($SkypeUsersRegistrarInvalid.count -ne 0)
            {
                foreach ($SkypeUserRegistrarInvalid in $SkypeUsersRegistrarInvalid)
                {
                    $myOU = $SkypeUserRegistrarInvalid.Identity | ForEach-Object{($_ -split "," | Select-Object -Skip 2)} 
                    $UserOU = $myOU -join ','

                    [array]$SkypeUserInvalidObjectAttribute = @()
                    $SkypeUserInvalidObjectAttribute = 
                        Add-ArraySiteDefaultvsUserSettings `
                            -SiteCode $SkypeSiteDefault.'Site Code' `
                            -Identity $SkypeUserRegistrarInvalid.SamAccountName `
                            -OU $UserOU `
                            -SiteDefaultValue $SkypeSiteDefault.RegistrarPool `
                            -UserCurrentSettings $SkypeUserRegistrarInvalid.Registrarpool.FriendlyName `
                            -AlertType 'INVALID - REGISTRAR POOL' 


                    If ($SkypeUsersInvalidObjectAttribute.count -eq 0)
                    {
                        $SkypeUsersInvalidObjectAttribute = $SkypeUserInvalidObjectAttribute
                    }
                    else
                    {
                        $SkypeUsersInvalidObjectAttribute += $SkypeUserInvalidObjectAttribute
                    }

                }
            }


            #Get-CsUser -ResultSize unlimited -ou $SkypeSiteDefault.OU | Where-Object {$_.Registrarpool -ne $SkypeSiteDefault.RegistrarPool} | Select SamAccountName, Registrarpool


        $SkypeSiteDefault.'Site Code'
        $SkypeSiteDefault.'Dial Plan'

        $SkypeSiteDefault.'Site Code'
        $SkypeSiteDefault.'Voice Policy'

        Write-Host ''
        Write-Host ''

    }
}

$SkypeUsersInvalidObjectAttribute | Export-CSV "C:\Scriptout\Test.csv"

1 个答案:

答案 0 :(得分:0)

Parrish-非常感谢您的回答:Class正是我需要的解决方案。

    Class ObjectCompare
{
    [String] $SiteCode
    [String] $Identity
    [String] $OU
    [String] $SiteDefault
    [String] $UserSetting
    [String] $IssueAlert
}


Function Add-ArraySiteDefaultvsUserSettings
{
    Param ( $OU, $Identity, $SiteCode, $SiteDefaultValue, $UserCurrentSettings, $AlertType )

    #SiteDefaultvsUserSettings
    [array]$DefaultvsUserSettings = @()
    $addObject = New-Object ObjectCompare # Add-DefaultsValidation
    $addObject.SiteCode = $SiteCode
    $addObject.Identity = $Identity
    $addObject.OU = $OU
    $addObject.SiteDefault = $SiteDefaultValue
    $addObject.UserSetting = $UserCurrentSettings
    $addObject.IssueAlert = $AlertType
    $DefaultvsUserSettings += $addObject
    #Write-Output $SiteDefaultvsUserSettings

    Return $DefaultvsUserSettings
}