ProjectManagementScripts/AddUser.ps1

67 lines
1.4 KiB
PowerShell
Raw Permalink Normal View History

[CmdletBinding()]
param (
[string]
$Identity,
[string]
$Server,
[string]
2024-07-14 19:39:45 +02:00
$ProjectName,
[string]
$Type = "RO"
)
2024-07-14 19:59:42 +02:00
$User = $null
$Group = $null
if(!$PSBoundParameters.ContainsKey("ProjectName")){
throw "ProjectName Parameter not specified, aborting"
return
}
if(!$PSBoundParameters.ContainsKey("Identity")){
throw "Identity Parameter not specified, aborting"
return
}
if($PSBoundParameters.ContainsKey("Server")){
$User = Get-ADUser -Server $Server -Identity $Identity
} else {
$User = Get-ADUser -Identity $Identity
}
2024-07-14 19:39:45 +02:00
if($null -eq $User){
throw "Didn't find the user, aborting"
return
}
if($PSBoundParameters.ContainsKey("Server")){
$Group = . .\GetProjects.ps1 -Server $Server
} else {
$Group = . .\GetProjects.ps1
}
2024-07-14 19:39:45 +02:00
if($null -eq $Group){
throw "Can't find Project, aborting"
return
}
$targeGroup =$null
if($PSBoundParameters.ContainsKey("Server")){
$targeGroup = . .\GetRoles.ps1 -Server $Server -ProjectName $ProjectName | Where-Object Name -Match ".*-$($Type)"
2024-07-14 19:39:45 +02:00
} else {
$targeGroup = . .\GetRoles.ps1 -ProjectName $ProjectName | Where-Object Name -Match ".*-$($Type)"
2024-07-14 19:39:45 +02:00
}
if($null -eq $Group){
throw "Filter for Type was empty, aborting"
return
}
if($PSBoundParameters.ContainsKey("Server")){
2024-07-14 19:59:42 +02:00
Add-ADGroupMember -Identity $targeGroup -Members $Identity -Server $Server
} else {
2024-07-14 19:59:42 +02:00
Add-ADGroupMember -Identity $targeGroup -Members $Identity
}