personio/functions/activedirectory_function.ps1
2024-07-26 19:37:06 +02:00

146 lines
7.8 KiB
PowerShell

# Active Directory Modul laden
Import-Module ActiveDirectory
function SYNC-ADUser {
[CmdletBinding()]
param(
[Parameter(Position = 1, Mandatory = $true)]
$USERNAME,
[Parameter(Position = 2, Mandatory = $true)]
$Position,
[Parameter(Position = 3, Mandatory = $true)]
$Abteilung,
[Parameter(Position = 4, Mandatory = $true)]
$Tel,
[Parameter(Position = 5, Mandatory = $true)]
$Fax,
[Parameter(Position = 6, Mandatory = $false)]
$Manager,
[Parameter(Position = 7, Mandatory = $true)]
$Mobile,
[Parameter(Position = 8, Mandatory = $true)]
$HomeMobile
)
Begin {
Write-Verbose "Start SYNC-ADUser"
}
Process {
try {
$ADUSER = Get-AdUser $USERNAME -Properties Title, telephoneNumber, SamAccountName, OfficePhone, Manager, facsimileTelephoneNumber, Department, mehrkanalHomemobile, mobile, ipphone
if ($?) {
if ($ADUSER.Enabled -eq $true) {
if ($ADUSER.telephoneNumber -ne $Tel.trim()) {
if ($Tel.trim() -eq "") {
Set-ADuser -identity $USERNAME -Clear 'telephoneNumber'
}
else {
Set-ADuser -identity $USERNAME -Replace @{'telephoneNumber' = $Tel.trim()}
}
Write-Log -Message "$($USERNAME): Tel angepasst $($Tel.trim())" -Level Warn
}
if ($null -eq $ADUSER.ipphone) {
if ( $Tel -match "020127303-") {
$IPPHONE = $tel.Replace("020127303-", "")
Write-Log -Message "$($USERNAME): IPPhone angepasst $($IPPHONE.trim())" -Level Warn
Set-ADuser -identity $USERNAME -Replace @{'ipphone' = $IPPHONE.trim()}
}elseif ( $Tel -match "0201-27303-") {
$IPPHONE = $tel.Replace("0201-27303-", "")
Write-Log -Message "$($USERNAME): IPPhone angepasst $($IPPHONE.trim())" -Level Warn
Set-ADuser -identity $USERNAME -Replace @{'ipphone' = $IPPHONE.trim()}
}elseif ( $Tel -match "0201 27303-") {
$IPPHONE = $tel.Replace("0201 27303-", "")
Write-Log -Message "$($USERNAME): IPPhone angepasst $($IPPHONE.trim())" -Level Warn
Set-ADuser -identity $USERNAME -Replace @{'ipphone' = $IPPHONE.trim()}
}elseif ( $Tel -match "0201273034") {
$IPPHONE = $tel.Replace("020127303", "")
Write-Log -Message "$($USERNAME): IPPhone angepasst $($IPPHONE.trim())" -Level Warn
Set-ADuser -identity $USERNAME -Replace @{'ipphone' = $IPPHONE.trim()}
}
Write-Log -Message "$($USERNAME): IP-Phone angepasst $($Tel.trim())" -Level Warn
}
if ($ADUSER.facsimileTelephoneNumber -ne $Fax.trim()) {
if ($Fax.trim() -eq "") {
Set-ADuser -identity $USERNAME -Clear 'facsimileTelephoneNumber'
}
else {
Set-ADuser -identity $USERNAME -Replace @{'facsimileTelephoneNumber' = $Fax.trim()}
}
Write-Log -Message "$($USERNAME): Fax angepasst $($Fax.trim())" -Level Warn
}
if ($ADUSER.department -ne $Abteilung.trim()) {
if ($Abteilung.trim() -eq "") {
Set-ADuser -identity $USERNAME -Clear 'department'
}
else {
Set-ADuser -identity $USERNAME -Replace @{'department' = $Abteilung.trim()}
}
Write-Log -Message "$($USERNAME): Abteilung angepasst $($Abteilung.trim())" -Level Warn
}
if ($ADUSER.Title -ne $Position.trim()) {
if ($Position.trim() -eq "") {
Set-ADuser -identity $USERNAME -Clear 'Title'
Write-Log -Message "$($USERNAME): Position entfernt" -Level Warn
}
else {
Set-ADuser -identity $USERNAME -Replace @{'Title' = $Position.trim()}
Write-Log -Message "$($USERNAME): Position angepasst $($Position.trim())" -Level Warn
}
}
if ($MANAGER -ne $null) {
$GETCURMANAGER = (Get-ADUser $USERNAME -properties manager).manager
if ( $GETCURMANAGER -ne $null ){
$CURRENTMANAGER = $(Get-ADUser (Get-ADUser $USERNAME -properties manager).manager -ErrorAction SilentlyContinue).UserPrincipalName
}else{
$CURRENTMANAGER = ""
}
if ($CURRENTMANAGER -ne $MANAGER) {
$NEWMANAGERDN = $(Get-ADUser -filter { UserPrincipalName -eq $Manager }).distinguishedName
Set-ADuser -identity $USERNAME -Replace @{'Manager' = $NEWMANAGERDN}
Write-Log -Message "$($USERNAME): MANAGER angepasst $($NEWMANAGERDN)" -Level Warn
}
}
else {
Write-Log -Message "$($USERNAME): Kein MANAGER vorhanden" -Level Info
}
if ($ADUSER.mobile -ne $Mobile.trim()) {
if ($Mobile.trim() -eq "") {
Set-ADuser -identity $USERNAME -Clear 'mobile'
Write-Log -Message "$($USERNAME): Kein mobile vorhanden" -Level Info
}
else {
Set-ADuser -identity $USERNAME -Replace @{'mobile' = $Mobile.trim()}
Write-Log -Message "$($USERNAME): Mobile angepasst $($Mobile.trim())" -Level Warn
}
}
if ($ADUSER.mehrkanalHomemobile -ne $HomeMobile.trim()) {
if ( [string]$ADUSER.mehrkanalHomemobile -ne [string]$HomeMobile.trim()) {
if ($HomeMobile.trim() -eq "") {
Set-ADuser -identity $USERNAME -Clear 'mehrkanalHomemobile'
}
else {
Set-ADuser -identity $USERNAME -Replace @{'mehrkanalHomemobile' = $HomeMobile.trim()}
}
Write-Log -Message "$($USERNAME): HomeMobile angepasst $($HomeMobile.trim())" -Level Warn
}
}
}
#echo "$($USERNAME);$($ADUSER.telephoneNumber);$($Tel.trim());$($ADUSER.facsimileTelephoneNumber);$($Fax.trim());$($ADUSER.department);$($Abteilung.trim());$($ADUSER.Title);$($Position.trim());$($CURRENTMANAGER);$($Manager);$($ADUSER.mobile);$($Mobile.trim());$($ADUSER.mehrkanalHomemobile);$($HomeMobile.trim())"
# Return $true
}
}
catch {
Write-Log -Message "Error: $($_.Exception.Message) - SYNC-ADUser - Line Number: $($_.InvocationInfo.ScriptLineNumber)" -Level Error
Return $false
}
}
End {
Write-Verbose "Stop SYNC-ADUser"
}
}