請教各位前輩,因為要做自動同步處理,目前我寫的語法如下,
先解釋一下,我是將公司各課別從其他地方同步到AD的Company欄位,
但依照下面的寫法,不同的名字就寫一組,我總共要寫50幾組,實在是令我頭痛
所以我該怎麼寫,可以將使用者的company欄位內的名字,經過比對後,加入到對應的群組,簡化Script寫法呢?
$ou = Get-ADUser -Filter * -Properties Department,company,title
$a1 = $ou | Where-Object {$_.company -eq "123"}
Add-ADGroupMember -Identity 123 -Members $a1
$a2 = $ou | Where-Object {$_.company -eq "456"}
Add-ADGroupMember -Identity 456 -Members $a2
$a3 = $ou | Where-Object {$_.company -eq "789"}
Add-ADGroupMember -Identity 789 -Members $a3
$ou = Get-ADUser -Filter * -Properties Department,company,title
$ou 變數不變,改用Foreach迴圈,
自訂一個迴圈變數,EX: $companyDate
將Company資料存成字串變數 EX: $companylist
$companylist = @("123","456","789")
$ou = Get-ADUser -Filter * -Properties Department,company,title
foreach($companyDate in $companylist)
{
foreach($oudate in $ou)
{
if($oudate | Where-Object {$_ -eq $companyDate})
{Add-ADGroupMember -Identity $companyDate -Members $ou.name}
}
}
-Members 要的是SAM帳戶,$ou.name如果跟SAM值不一樣的話,你再自己調整吧