2016-03-29 11 views
2
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} 

} 

私が問題になっているのは、どの番号を選択してもIFのステートメントが最後のオプションを選択しているということですか? (チームリーダーのOU)。誰かが私のIFステートメントに間違っていることを知っていますか?Ifステートメント - Powershell

+0

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} } 

は今、あなたはスイッチで機能を使用することができます'='は比較しない値を割り当てることを意味します。 – Martin

答えて

2

Koryギルは、すでにあなたのif文の中に問題を発見参照してください。ただし、PowerShell関数のパラメーターを使用することを検討してください。例:2、3、4、5よりもif文、理由の間に、実際には、あなたの$ contacttypeが1に設定されている

Get-ContactOu -Admin