2016-03-08 88 views
0

我創建了一個表單來創建新的AD帳戶。該腳本的一部分決定了新用戶將被添加到該組基於其在下面的代碼在下拉拾取框的形式捕捉自己的角色(醫生,護士,管理或其他):PowerShell - 將新用戶添加到AD組的選擇

Write-Host "Based on this information" $FFN "has been added to the following Active Directory Groups:" 
Write-Host 

$ADGroup01 = Get-ADGroup "_XA_App_XenApp" |select -expandproperty name -first 1 
Write-Host $ADGroup01 
$ADGroup02 = Get-ADGroup "Web Proxy Users" |select -expandproperty name -first 1 
Write-Host $ADGroup02 
if($RadioButton1.Checked -eq $true) 
    { 
    $ADGroup03 = Get-ADGroup "allrot" |select -expandproperty name -first 1 
    Write-Host $ADGroup03 
    } 
Else 
    { 
    $ADGroup03 = Get-ADGroup "alltpo" |select -expandproperty name -first 1 
    Write-Host $ADGroup03 
    } 
if ($Role -eq "Doctor" -Or $Role -eq "Nurse") 
    { 
    $ADGroup04 = Get-ADGroup "PACS Web Access" |select -expandproperty name -first 1 
    Write-Host $ADGroup04 
    } 
if ($Role -eq "Doctor") 
    { 
    $ADGroup05 = Get-ADGroup "CH-MFD" |select -expandproperty name -first 1 
    Write-Host $ADGroup05 
    $ADGroup06 = Get-ADGroup "ED-MFP" |select -expandproperty name -first 1 
    Write-Host $ADGroup06 
    $ADGroup07 = Get-ADGroup "SU-MFD" |select -expandproperty name -first 1 
    Write-Host $ADGroup07 
    } 
Write-Host 

而且在腳本這段代碼是在實際的帳戶創建過程叫做:

Add-ADPrincipalGroupMembership -Identity $UN -memberof $ADGroup01, $ADGroup02, $ADGroup03, $ADGroup04, $ADGroup05, $ADGroup06, $ADGroup07 

我現在面臨的問題是,如果用戶選擇我收到以下錯誤護士,管理員或其他:

「Add-ADPrincipalGroupMembership:Can not validat參數'MemberO f'的參數。參數爲null,空或參數集合conta的一個元素爲空值。提供一個不包含任何空值的集合,然後再嘗試該命令。「

我知道這是因爲在最後一個$ ADGroup [x]中沒有捕獲任何值並且沒有創建一堆if檢查語句如果每個廣告組$包含數據,我不知道是否有一個更優雅的解決方案

和往常一樣,感謝您抽出寶貴的時間審查和高興,如果需要提供更多的信息

更新。 - 根據@ Martin的建議,我已將以下代碼實施到我的腳本中

$UN = "zooz" 
$Role = "Nurse" 
$Department = "Surgical" 

If ($Role -eq "Doctor" -and $Department -eq "Surgical") 
{ 
$ADGroups = @(
"PACS Web Access" 
"CH-MFD" 
"ED-MFP" 
"SU-MFD" 
) 
} 

If ($Role -eq "Nurse" -and $Department -eq "Surgical") 
{ 
$ADGroups = @(
"_XA_App_XenApp" 
"Web Proxy Users" 
"allrot" 
) 
} 

for ($i=0; $i -lt $ADGroups.length; $i++) { 
Add-ADPrincipalGroupMembership -Identity $UN -memberof $adgroups[$i] 
} 

回答

0

創建對象$adgroups並向其中添加所需的組。

$adgroups = @() 

在最後使用foreach循環:

$adgroups | Add-ADPrincipalGroupMembership -Identity $UN或(天氣或不喜歡cmdlet的管道輸入)

$adgroups | % { Add-ADPrincipalGroupMembership -Identity $UN -memberof $_ } 
+0

感謝@馬丁一個非常整潔的解決方案 – VernonW

+0

你歡迎。 – Martin

相關問題