Sorry everyone; I know something so simple must be here already, but I can’t see it
I have CSV and I want to add it to a set of groups as part of a larger onboarding script. I want to know if I can just add it like this or I need to list everything separately?
#Function SetGroups ($sam)
{
Try
{
Get-ADUser $sam | Add-ADGroupMember -Identity “Group1,Group2,Group3” -Members $sam
$OutPut = "Success: Added $sam to Correct Groups "
Write-Host $OutPut -ForegroundColor Green
WriteToLog $LogFile $OutPut
}
Catch
{
$OutPut = "FAIL: DID NOT Set $sam in to the Correct Groups"
Write-Host $OutPut -ForegroundColor Red
WriteToLog $LogFile $OutPut
}
}#end Fn
You have not set the ErrorAction to Stop, so the Try\Catch logic will not work
You have hard-coded groups in the script, so it only adds specific groups. If there is a param, then it provides flexibility for Role-Based Access Control after you build a generic user
If you do set an ErrorAction, then anything can break the process. If you find the user and the first group isn't found, it will never process the other two groups
As kvprasoon said, this may not be the best solution, but we would need to see the entire script. This wrapper is basically the same thing as Add-ADGroupMember, so you are not really gaining anything by putting a wrapper around it.
Please validate my script and give me a suggestion. While ran the script getting error from -identity line.[/quote]
Please do not hijack other peoples threads. If you have a question create a new post for yourself and place a link to the other post if it’s related and helpful for your question.