Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Problem with function returning a hash table

$
0
0

Hi all

I'm puzzling over a function that sometimes returns an array rather than a hash table.

Here's my Powershell script

# Import the module containing the function
ipmo C:\Util\Powershell\StepsT4.psm1

$omail = "coxad@contoso.com"
$osam = "coxad"
$oou = "ou=test,dc=fabrikam,dc=com"
$ocn = "Adam Cox"
$ofirst = "Adam"
$olast = "Cox"

$S1Result = Step1 $omail $osam $oou $ocn $ofirst $olast
$S1Result.gettype()

The following is steps.psm1

## Step1 Function 
function Step1 { 
Param($mail,$sam,$ou,$cn,$fname,$lname) 

    write-log -path $logfile -message "Contact for $sam will be created in: $ou"
    # Create the contact object
    write-log -path $logfile -message "Performing STEP 1: Creation of Contact object for: $sam"
    $Alias = $mail -replace "[^a-zA-Z0-9]",""
    $newMC = New-MailContact -DomainController $dc -name $cn -DisplayName $cn -Alias `
    $Alias -ExternalEmailAddress $mail -PrimarySmtpAddress $mail `
    -FirstName $fname -LastName $lname -OrganizationalUnit $ou
    # Validation of results to determine whether to continue or not
    [hashtable]$Return = @{}
    If ($newMC.guid) {
        write-log -path $logfile -message "Step 1 results validation check passed for user $sam"
        $Return.Success = $True
    } # End If
    Else {
        write-log -path $logfile -message "Step 1 results validation check failed for user $sam"
        $Return.FailedValiditycheck = $sam
        $Return.Success = $False
    } # End Else
    Return $Return
} # End of Step1 function

The following is stepst4.psm1

## Step1 Function 
function Step1 { 
Param($mail,$sam,$ou,$cn,$fname,$lname) 
    write-log -path $logfile -message "Contact for $sam will be created in: $ou"
    # Create the contact object
    write-log -path $logfile -message "Performing STEP 1: Creation of Contact object for: $sam"
    $Alias = $mail -replace "[^a-zA-Z0-9]",""
    $newMC = New-MailContact -DomainController $dc -name $cn -DisplayName $cn -Alias `
    $Alias -ExternalEmailAddress $mail -PrimarySmtpAddress $mail `
    -FirstName $fname -LastName $lname -OrganizationalUnit $ou
    # Validation of results to determine whether to continue or not
    [hashtable]$Return = @{}
    If ($newMC.guid) {
        write-log -path $logfile -message "Step 1 results validation check passed for user $sam"
        $Return.Success = $True
    } # End If
    Else {
        write-log -path $logfile -message "Step 1 results validation check failed for user $sam"
        $Return.FailedValiditycheck = $sam
        $Return.Success = $False
    } # End Else
    Return $Return
} # End of Step1 function

Both actually look the same to me, but this is what I get when I run the script with the steps.psm1 loaded:

PS C:\Users\administrator> C:\Util\Powershell\Tester4.ps1
Performing STEP 1: Creation of Contact object for: coxad
Step 1 results validation check passed for user coxad

IsPublic IsSerial Name                                     BaseType                                                                                                                       
-------- -------- ----                                     --------                                                                                                                       
True     True     Object[]                                 System.Array

...and this is what I get when I run the script with the stepst4.psm1 loaded:

PS C:\Users\administrator> C:\Util\Powershell\Tester4.ps1
Contact for coxad will be created in: ou=test,dc=fabrikam,dc=com
Performing STEP 1: Creation of Contact object for: coxad
Step 1 results validation check passed for user coxad

IsPublic IsSerial Name                                     BaseType                                                                                                                       
-------- -------- ----                                     --------                                                                                                                       
True     True     Hashtable                                System.Object  

I'm tearing my hair out as I really can't see why one returns an array while the other returns a hashtable. 

Any thoughts?


Tony www.activedir.org blog:www.open-a-socket.com


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>