Luke
Luke

Reputation: 31

If statements - Powershell

function contactOU
{
    #This selects which OU to place the contact in.
    write-host
    write-host '~Contact type~'
    write-host '1. Admin'
    write-host '2. Assistant Owner'
    write-host '3. Owner Partner'
    write-host '4. Owner'
    write-host '5. Team Leader'
    write-host
    $contacttype = (Read-host -prompt 'Which type of contact')
    if($contacttype = "1") {$contactOU = "OU=Admins,OU=Marketing Companies,OU=Contacts,DC=company,DC=com,DC=au"}
    if($contacttype = "2"){$contactOU = "OU=Assistant Owners,OU=Marketing Companies,OU=Contacts,DC=company,DC=com,DC=au"}
    if($contacttype = "3"){$contactOU = "OU=Owner Partner,OU=Marketing Companies,OU=Contacts,DC=company,DC=com,DC=au"}
    if($contacttype = "4"){$contactOU = "OU=Owners,OU=Marketing Companies,OU=Contacts,DC=company,DC=com,DC=au"}
    if($contacttype = "5"){$contactOU = "OU=Team Leaders,OU=Marketing Companies,OU=Contacts,DC=company,DC=com,DC=au"}
    else{write-host 'Please select a valid number',contactOU}

    #For testing
    write-host $contactOU

    #May put this in an individual function?
    New-ADObject -name $contactname -type Contact -Path $contactOU -OtherAttributes @{displayName=$contactname;mail=$emailaddress;targetAddress=$targetaddress}

}

The issue I'm having is, regardless of what number I select the IF statments choose the last option? (Team Leader OU). Does anyone know what is wrong with my IF statements?

Upvotes: 2

Views: 132

Answers (2)

Martin Brandl
Martin Brandl

Reputation: 58931

Kory Gill already found the problem within your if statements. However, consider to use PowerShell function parameters. Example:

function Get-ContactOu
{
    Param(
        [Parameter(Mandatory=$false, Position=0, ParameterSetName='Admin')]
        [switch]$Admin,

        [Parameter(Mandatory=$false, Position=0, ParameterSetName='AssistantOwner')]
        [switch]$AssistantOwner,

        [Parameter(Mandatory=$false, Position=0, ParameterSetName='OwnerPartner')]
        [switch]$OwnerPartner,

        [Parameter(Mandatory=$false, Position=0, ParameterSetName='Owner')]
        [switch]$Owner,

        [Parameter(Mandatory=$false, Position=0, ParameterSetName='TeamLeader')]
        [switch]$TeamLeader
        )


    $ou = '';

    if ($Admin) { $ou = 'Admins' } 
    if ($AssistantOwner) { $ou = 'Assistant Owners' } 
    if ($OwnerPartner) { $ou = 'Owner Partner' } 
    if ($Owner) { $ou = 'Owners' } 
    if ($TeamLeader) { $ou = 'Team Leaders' } 

    $path = 'OU={0},OU=Marketing Companies,OU=Contacts,DC=company,DC=com,DC=au' -f $ou
    New-ADObject -name $contactname -type Contact -Path $path -OtherAttributes @{displayName=$contactname;mail=$emailaddress;targetAddress=$targetaddress}
}

Now you can use the function with a switch:

Get-ContactOu -Admin

Upvotes: 2

Kory Gill
Kory Gill

Reputation: 7163

Use -eq in your if statements.

if($contacttype -eq "1")

See about_Comparison_Operators

Upvotes: 4

Related Questions