为什么我的对象数组加倍结果?

时间:2018-07-10 22:19:51

标签: arrays powershell object

下面的代码集,除1件我无法弄清楚的东西外,其他所有东西都起作用。 Get-CSUserStandardsValidatedSet处理数据时,对象计数数组保持原样。无论数组中有1个对象还是100个对象,计数都很好。但是,当Get-CSUserStandardsValidatedSet通过return $return将对象数组传递回调用者函数Invoke-MRC_CSSkypeObjects_ImportCSV时。每次对象计数加倍。 如果我传递参数Select -unique,则实际上会丢失我要处理的记录。

有什么想法吗?

Function Get-CSUserStandardsValidatedSet
{
    param ($users)

    [array]$UserInfoArray = @()
    $DNStandardGlobalCpAnswer = $null
    $DNStandardGlobalCuAnswer = $null
    $SIPStandardGlobalCpAnswer = $null
    $UMMStandardGlobalAnswer = $null

    foreach ($user in $users)
        {
            $PhoneNumberIsInUse = $false

            $UserInfoCopy = New-UserInfo 
            ######################################################## #Still Need to validate the correct OU Location Based on AccountTypes#  ########################################################
            Write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
            Write-Host "$($user.DisplayName)          $($user.CSVtelephoneNumber)                                                      $user"
            Write-host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
            #This must be populated for OU Validation, and Cu DisplayName Validation
            $UserInfoCopy.AllowRecordToConinueNoErrorsFound = $true

            Write-Host 'SiteCode'
            $UserInfoCopy.CSVSiteCode = $user.SiteCode
            Write-host  "$($UserInfoCopy.CSVSiteCode)"

            $UserInfoCopy.CSVPrivateSiteCode = $user.SiteCode -replace "[^0-9]" #To be removed and replaced with CSVSiteCode
            Write-host "$($UserInfoCopy.CSVPrivateSiteCode)"

            #if ($user.SiteCode -match "^\d+$") 
            #    { $user.SiteCode -replace "[^0-9]"} 
            #    else { write-host "Site Code is not numeric or does not exist:  $($user.PrivateSiteCode)";end}

            Write-Host 'Acct Type (U, Cu, Cp, F, H)'
            $UserInfoCopy.CSVAccountTypes = 
                    Switch ($user.AccountTypes)
                    {
                        'U'     {$user.AccountTypes}
                        'F'     {$user.AccountTypes}
                        'Cu'    {$user.AccountTypes}
                        'Cp'    {$user.AccountTypes}
                        'H'     {$user.AccountTypes}
                        default {"Unknown Type of Skype Account to create: $($user.AccountTypes) for $($UserInfoCopy.DisplayName) ";stop}
                    }
               Write-host  "$($UserInfoCopy.CSVAccountTypes)"

            Write-Host 'Private or Public'
            $UserInfoCopy.CSVPhoneExtensionType_S_P = if ($user.PhoneExtensionType_S_P -eq 'S') {$true} else {$false}
            Write-host "$($UserInfoCopy.CSVPhoneExtensionType_S_P)"

            Write-Host 'OU'
            $UserInfoCopy.CSVOU = Get-VerifyOUbyTypeForADLocation -Type $UserInfoCopy.CSVAccountTypes -SiteCode $UserInfoCopy.CSVSiteCode -OU $user.CSVOU
            Write-host "$($UserInfoCopy.CSVOU)"

    #region PHONE NUMBER AND POLICY OR PLAN AND POOL         

            Write-Host 'Telephone Number'
            $UserInfoCopy.CSVtelephoneNumber = 
                if ((Exists-PhoneNumberisUnUsed -PhoneNumber ($user.telephoneNumber -replace "[^0-9]")))
                {$user.telephoneNumber -replace "[^0-9]"}
                else
                {
                    #write-host "Telephone Number $($User.telephoneNumber) is already in use. Please remove the number from active use or change the phone number being assigned."; break
                    $PhoneNumberIsInUse = $True
                    $user.telephoneNumber -replace "[^0-9]"
                }
            Write-host  $UserInfoCopy.CSVtelephoneNumber

            Write-Host 'Ext'
            $UserInfoCopy.CSVExt = $user.Ext  -replace "[^0-9]"
            Write-host  $UserInfoCopy.CSVExt

            Write-Host 'Dial Plan'
            $UserInfoCopy.CSVDialPlan = if (Exists-VoiceOrDataPlanTorF -Exists_Plan $User.DialPlan -VoiceSearch $false) 
                {$user.DialPlan} 
                else {write-host "User: $($user.DisplayName) DialPlan: $($user.DialPlan)  -  DOES NOT EXIST"; 
                    if (($user.AccountTypes -eq 'U') -or ($user.AccountTypes -eq 'Cu') -or ($user.AccountTypes -eq 'Cp')) 
                    {Break} 
                    else {write-host ' Not a User/Common Area Phone Account, Allowed to Continue'}  #Do not proceed if Dial Plan can not be found
                }
            Write-host  $UserInfoCopy.CSVDialPlan

            Write-Host 'Voice Policy'
            $UserInfoCopy.CSVVoicePolicy = if (Exists-VoiceOrDataPlanTorF -Exists_Plan $user.VoicePolicy -VoiceSearch $true)  
                {$user.VoicePolicy} 
                else {write-host "User: $($user.DisplayName) VoicePolicy: $($user.VoicePolicy)  -  DOES NOT EXIST"; 
                    if (($user.AccountTypes -eq 'U') -or ($user.AccountTypes -eq 'Cu') -or ($user.AccountTypes -eq 'Cp')) 
                    {Break}  
                    else {write-host ' Not a User/Common Area Phone Account, Allowed to Continue'}  #Do not proceed if Voice Policy can not be found
                }
            Write-host  $UserInfoCopy.CSVVoicePolicy

            Write-Host 'UM Mailbox Policy'
            $UserInfoCopy.CSVUMMailboxPolicy = Get-UMMailPolicyAssigned -Type $UserInfoCopy.CSVAccountTypes -RequestUMMailboxPolicyAssign $user.UMMailboxPolicy
            Write-host  $UserInfoCopy.CSVUMMailboxPolicy

            ############################################################################################################################
    #REGISTRAR POOL
            Write-Host 'RegistrarPool'
             $UserInfoCopy.CSVRegistrarPool = if ((Exists-MRCCSSkypeRegistrarPool -PoolCheck $user.RegistrarPool) -or ($user.AccountTypes -eq 'U'))
                {$user.RegistrarPool}
                else {write-host "User: $($user.DisplayName) RegistrarPool: $($user.RegistrarPool)  -  DOES NOT EXIST";
                    if (($user.AccountTypes -eq 'Cu') -or ($user.AccountTypes -eq 'Cp'))
                    {Break}
                    else {write-host 'Not a User Account, Allowed to Continue'}
                }
            Write-host $UserInfoCopy.CSVRegistrarPool

    #PHONE NUMBER
            #Private -replace "[^0-9]"
            #$UserInfoCopy.PrivateExtension = $PrivSiteExtNumber + $UserInfoCopy.CSVExt
            Write-Host 'Private Extension'
            $UserInfoCopy.PrivateExtension = $UserInfoCopy.CSVExt -replace "[^0-9]"
            Write-host $UserInfoCopy.PrivateExtension
                    #New Logic
                        #Due to the variable ways private extensions are assigned, it will be up to the user to put the correct private extension into the csv file
                    #Old Logic
                        #if (($user.SiteCode -match "^\d+$") -and ($UserInfoCopy.CSVExt -match "^\d+$")) {($user.SiteCode -replace "[^0-9]") + ($UserInfoCopy.CSVExt -replace "[^0-9]")} else {" Site Code is not a numeric number $($user.SiteCode) or Private Extension is not a numeric number $($user.CSVExt)";break}

            Write-Host 'LineURI Private'
            $UserInfoCopy.LineuriPrivate = [String]::Format('tel:+{0:###########};ext={1:####}',([int64]$UserInfoCopy.CSVtelephoneNumber -replace "[^0-9]"),[int64]$UserInfoCopy.PrivateExtension -replace "[^0-9]")
            Write-host $UserInfoCopy.LineuriPrivate

            Write-Host 'Telephone Number Private'
            $UserInfoCopy.telephoneNumberPrivate_Format1 = [String]::Format('tel:{0:+#-###-###-####};ext={1:####}',[int64]$UserInfoCopy.CSVtelephoneNumber,[int64]$UserInfoCopy.PrivateExtension -replace "[^0-9]")
            Write-host $UserInfoCopy.telephoneNumberPrivate_Format1
            $UserInfoCopy.telephoneNumberPrivate_Format2 = [String]::Format('{0:(###) ###-####};ext={1:####}',([int64]$UserInfoCopy.CSVtelephoneNumber - 10000000000),[int64]$UserInfoCopy.PrivateExtension -replace "[^0-9]")
            Write-host $UserInfoCopy.telephoneNumberPrivate_Format2

            #Not Private
            Write-Host 'LineURI Public'    
            $UserInfoCopy.Lineuri = [String]::Format('tel:+{0:###########};ext={1:####}',([int64]$UserInfoCopy.CSVtelephoneNumber),$UserInfoCopy.CSVtelephoneNumber.Substring(($UserInfoCopy.CSVtelephoneNumber.Length-4)))
            Write-host $UserInfoCopy.Lineuri

            Write-Host 'telephone Number Public'
            $UserInfoCopy.telephoneNumber_Format1 = [String]::Format('{0:+#-###-###-####}',[int64]$UserInfoCopy.CSVtelephoneNumber)
            Write-host $UserInfoCopy.telephoneNumber_Format1
            $UserInfoCopy.telephoneNumber_Format2 = [String]::Format('{0:(###) ###-####}',([int64]$UserInfoCopy.CSVtelephoneNumber - 10000000000))
            Write-host $UserInfoCopy.telephoneNumber_Format2

    #endregion






    #region NAME SECTION
            #First and Last name we only care about if Cu since AD User account is being Created
            #Last name must exist for Cp to generate the sip
            Write-Host 'Last Name'
            $UserInfoCopy.CSVLastName = 
                if ((($user.AccountTypes -eq 'Cu') -or ($user.AccountTypes -eq 'Cp')) -and ($user.LastName -eq '')) 
                    {
                        if($CSVPhoneExtensionType_S_P)
                        {$UserInfoCopy.telephoneNumber_Format2}
                        else
                        {$UserInfoCopy.telephoneNumberPrivate_Format2}
                        #"Error no Last name for $($user.DisplayName)";stop
                    }
                else {$user.LastName} 
            Write-host $UserInfoCopy.CSVLastName

            Write-Host 'First Name'
            $UserInfoCopy.CSVFirstName = 
                if (($user.AccountTypes -eq 'Cu') -or ($user.AccountTypes -eq 'Cp')) 
                    {
                        if ($UserInfoCopy.CSVSiteCode -match "^\d+$")
                        {
                            $MySiteCode = $UserInfoCopy.CSVSiteCode  -replace "[^0-9]"
                            "BR$($MySiteCode.ToString("000"))"
                        }
                        Else 
                        {
                            "BR$($UserInfoCopy.CSVSiteCode)"
                        } 
                    } 
                else {$user.FirstName}
            Write-host $UserInfoCopy.CSVFirstName

            Write-Host 'Display Name'
            $UserInfoCopy.CSVDisplayName = Get-DisplayName -Type $UserInfoCopy.CSVAccountTypes -DisplayNameRequest $user.DisplayName `
                -IsSiteNotPrivateExtension $UserInfoCopy.CSVPhoneExtensionType_S_P -Extension $UserInfoCopy.PrivateExtension `
                -PublicNumber $UserInfoCopy.CSVtelephoneNumber -PrivateNumber $UserInfoCopy.CSVtelephoneNumber `
                -SiteCode $UserInfoCopy.CSVSiteCode -LastName $UserInfoCopy.CSVLastName
            Write-host $UserInfoCopy.CSVDisplayName
    #endregion



    #region SAM ACCOUNT
        Write-Host 'SAM Account Name'   
        $MySAM = Get-UserSAMAccountName -Type $UserInfoCopy.CSVAccountTypes -SAM $User.SamAccountName -DisplayName $UserInfoCopy.CSVDisplayName -LastName $UserInfoCopy.CSVLastName -FirstName $UserInfoCopy.CSVFirstName
        $UserInfoCopy.CSVSamAccountNameFound = if ($MySAM.SamAccountName.Length -ne 0) {$true} else {$false} 
        Write-host 'AD Account Found: ' + $UserInfoCopy.CSVSamAccountNameFound

        $UserInfoCopy.CSVSamAccountName = $MySAM.SamAccountName
        Write-host $UserInfoCopy.CSVSamAccountName

        #Phone Number is already in use
        if (($PhoneNumberIsInUse) -and ($UserInfoCopy.CSVSamAccountNameFound))
        {
            If
                ( 
                    ( 
                        ( (get-csuser $UserInfoCopy.CSVSamAccountName).lineuri -eq $UserInfoCopy.LineuriPrivate ) -and ($UserInfoCopy.CSVPhoneExtensionType_S_P -eq $False) 
                    ) `
                    -or
                    ( 
                        ( (get-csuser $UserInfoCopy.CSVSamAccountName).lineuri -eq $UserInfoCopy.Lineuri ) -and ($UserInfoCopy.CSVPhoneExtensionType_S_P -eq $True) 
                    )
                )

                {
                    #The current user is assigned the phone number and we are allowed to Proceed
                    #Nothing needs done to allow this
                }
                else
                {
                    "Telephone Number $($User.telephoneNumber) is already in use. Please remove the number from active use or change the phone number being assigned."; "This record will not be Processed"
                    #Phone number is in use
                    #We do not want the script to stop rather ignore the user account
                    $UserInfoCopy.CSVSamAccountNameFound = $false  #This is a partial solution as it will only stop User Account Modifications not the others
                    $UserInfoCopy.AllowRecordToConinueNoErrorsFound = $false  #This will be a new parameter used
                }


             #write-host "Telephone Number $($User.telephoneNumber) is already in use. Please remove the number from active use or change the phone number being assigned."; break
        }


        $UserInfoCopy.telephoneNumberOriginal = $MySAM.telephoneNumberOrigional          
        Write-host $UserInfoCopy.telephoneNumberOriginal

        $UserInfoCopy.CSVMail = $MySAM.mail
        Write-host $UserInfoCopy.CSVMail

        $UserInfoCopy.info = $MySAM.info
        Write-host $UserInfoCopy.info

        Write-Host 'SIP'
        $UserInfoCopy.CSVSIP =             
            if ($UserInfoCopy.CSVPhoneExtensionType_S_P)
            {
                Get-SIPAddress -Type $UserInfoCopy.CSVAccountTypes -SAM $UserInfoCopy.CSVSamAccountName -LastName $UserInfoCopy.CSVLastName -FirstName $UserInfoCopy.CSVFirstName -SiteCode $UserInfoCopy.CSVSiteCode -LineURI $UserInfoCopy.Lineuri
            }
            else
            {
                Get-SIPAddress -Type $UserInfoCopy.CSVAccountTypes -SAM $UserInfoCopy.CSVSamAccountName -LastName $UserInfoCopy.CSVLastName -FirstName $UserInfoCopy.CSVFirstName -SiteCode $UserInfoCopy.CSVSiteCode -LineURI $UserInfoCopy.LineuriPrivate
            }
        Write-host $UserInfoCopy.CSVSIP

    #endregion
            if ($User.ExtensionAttribute7.length -ne 0)
            {
            $UserInfoCopy.CSVExtensionAttribute7 = [String]::Format('+{0:#-###-###-####}',([int64]($User.ExtensionAttribute7 -replace "[^0-9]")))
            }
            Write-host $UserInfoCopy.CSVExtensionAttribute7

            $UserInfoCopy | Select-Object -Property * 
            Write-Host '#####################################################'
            $UserInfoCopy | Select-Object -Property * | Write-Host
            Write-Host '#####################################################'
            Write-Host ($UserInfoCopy | Select-Object -Property *)

            $UserInfoArray += $UserInfoCopy
        }

    $UserInfoArray | Export-CSV $LOG_csv
    #$UserInfoArray | Export-CSV $LOG_csv
    $return = $UserInfoArray

    Return $return
}





    Function Invoke-MRC_CSSkypeObjects_ImportCSV
{
    param ($Import_CSVFile = $Import_CSV, [bool]$ValidateOnly = $false, $Export_ValidatedObjectCSVFile = $LOG_csv)
    $FileNameTranscript = "C:\ScriptOut\Transcript-SkypeObject-CompanyPolicies-$((get-date).toString(‘yyyyMMdd-HHmmss’)).log"
    Start-Transcript -Path $FileNameTranscript  -Append 

        ########################################################################################################
        ###                    IMPORT CSV AND VALIDATE INFORMATION ACCORDING TO STANDARDS                    ###
        ########################################################################################################


        Write-Host '---------------------------------------------------------------------------------------------------------------------------------------------'
        Write-Host '                                                            Validating the CSV File'
        Write-Host '---------------------------------------------------------------------------------------------------------------------------------------------'

        $UserInfoArraySet = ''
        $CSVusers=Import-Csv $Import_CSVFile 
        $UserInfoArraySet = (Get-CSUserStandardsValidatedSet -users $CSVusers | Select -Unique)
        $UserInfoArraySet | Export-CSV $Export_ValidatedObjectCSVFile 

        if ($ValidateOnly -eq $false)
        {
            Set-MRC_SkypeWorkFlow_ProcessObjectModification -users $UserInfoArraySet
        }
        Else
        {   #Return the Validate object Details
            Return $UserInfoArraySet
        }

    Stop-Transcript 
}

0 个答案:

没有答案