ASKER
ASKER
ASKER
Import-Module ActiveDirectory
$user = Read-Host -Prompt "Enter Kürzel"
If ($ADUser = Get-ADUser -Identity $user -Properties Department, MemberOf, Title) {
$Groups = $null
If (($ADUser.Department -eq 'Team Z') -and ($ADUser.Title -eq "Director")) {
$Groups = "w", "w1"
} ElseIf (($ADUser.Department -eq "Team Z") -and ($ADUser.Title -eq "Manager")) {
$Groups = "w3", "w4"
} Else {
Write-Host "User does not meet the requirements of this script." -ForegroundColor Yellow
}
If ($Groups) {
$ADuser.memberOf | ForEach-Object {
Write-Host "[$($ADUser.SamAccountName)] Removing from $($_ -replace '\ACN=(.+?),(CN|DC)=.*', '$1') ..." -ForegroundColor White
Remove-ADGroupMember -Identity $_ -Members $ADUser.SamAccountName
}
$Groups | ForEach-Object {
Write-Host "[$($ADUser.SamAccountName)] Adding to $($_) ..." -ForegroundColor White -NoNewline
Try {
Add-ADGroupMember -Identity $_ -Members $ADUser.SamAccountName -ErrorAction Stop
Write-Host " OK" -ForegroundColor Green
} Catch {
Write-Host $_.Exception.Message -ForegroundColor Yellow
}
}
}
}
ASKER
ASKER
ASKER
ASKER
ASKER
ASKER
Write-Host "[$($ADUser.SamAccountName)] Removing from $($_ -replace '\ACN=(.+?),(CN|DC)=.*', '$1') ..." -ForegroundColor White
ASKER
ASKER
(1 -eq 2) -and (2 -eq 2) -or (3 -eq 3)
It would need to be (($ADUser.Department -eq "Team Z") -and (($ADUser.Title -eq "Praktikant") -or ($ADUser.Title -eq "Consultant")))
But that's not easy to read. Use an array instead and check for membership in the array:(($ADUser.Department -eq "Team Z") -and ("Consultant", "Praktikant", "Praktikantin" -contains $ADUser.Title))
ASKER
Windows PowerShell is a task automation and configuration management framework from Microsoft, consisting of a command-line shell and associated scripting language built on the .NET Framework. PowerShell provides full access to the Component Object Model (COM) and Windows Management Instrumentation (WMI), enabling administrators to perform administrative tasks on both local and remote Windows systems as well as WS-Management and Common Information Model (CIM) enabling management of remote Linux systems and network devices.
TRUSTED BY
But you shouldn't retrieve the same user twice, including all properties, just to then only use one single property.
Try it like this:
Open in new window