2016-03-29 107 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語句有什麼問題嗎?如果語句 - Powershell

+0

其實,你的$ contacttype設置爲1比2,3,4,5中的if語句,因爲'='意味着分配一個值,而不是進行比較。 – Martin

回答

2

Kory吉爾已經找到你的if語句中的問題。但是,請考慮使用PowerShell功能參數。例如:

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

現在你可以使用該功能與交換機:

Get-ContactOu -Admin