140 lines
6.3 KiB
PowerShell
140 lines
6.3 KiB
PowerShell
|
<#
|
||
|
.SYNOPSIS
|
||
|
Personio Sync Script (Personio -> MEHRKANAL AD)
|
||
|
.DESCRIPTION
|
||
|
Dieses Script übernimmt Personenbezogene Daten aus Personio in die Active Directory
|
||
|
.EXAMPLE
|
||
|
PS C:\POSTV>.\mk.ps1
|
||
|
<Startet den Sync für alle User>
|
||
|
.NOTES
|
||
|
Author: Sebastian Mendyka <mendyka@mehrkanal.com>
|
||
|
Date: Sep 13, 2017
|
||
|
.Link
|
||
|
URL: https://developer.personio.de/v1.0/reference
|
||
|
#>
|
||
|
|
||
|
|
||
|
######################################################################################
|
||
|
######################################################################################
|
||
|
# Variablen setzen
|
||
|
######################################################################################
|
||
|
######################################################################################
|
||
|
$application = "https://api.personio.de";
|
||
|
$company = "5989";
|
||
|
|
||
|
# Define the user credentials
|
||
|
$username = "#TOREPLACE#";
|
||
|
$password = "#TOREPLACE#";
|
||
|
|
||
|
# Force TLS1.2
|
||
|
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
|
||
|
|
||
|
$LOGDATE = Get-Date -Format "yyyy-MM-dd_"
|
||
|
$OWNPID = $([System.Diagnostics.Process]::GetCurrentProcess()).ID
|
||
|
$ScriptDir = [System.IO.Path]::GetDirectoryName($myInvocation.MyCommand.Definition)
|
||
|
$ini = .\functions\functions.ps1 "$ScriptDir\config.ini"
|
||
|
|
||
|
$IMPERSONATEADMIN = $ini.exchange.impersonateuser
|
||
|
|
||
|
######################################################################################
|
||
|
######################################################################################
|
||
|
# Functions
|
||
|
######################################################################################
|
||
|
######################################################################################
|
||
|
# Active Directory Modul laden
|
||
|
. .\functions\json_function.ps1
|
||
|
|
||
|
# Active Directory Modul laden
|
||
|
. .\functions\activedirectory_function.ps1
|
||
|
|
||
|
# Füge Log Klasse hinzu, Inspiriert von https://gist.github.com/9to5IT/9620565
|
||
|
. .\functions\log_function.ps1
|
||
|
|
||
|
# Füge Exchange Klassen hinzu
|
||
|
. .\functions\exchange_function.ps1
|
||
|
|
||
|
# Füge Personio Klassen hinzu
|
||
|
. .\functions\personio.ps1
|
||
|
|
||
|
# Füge SQLITE Klassen hinzu
|
||
|
. .\functions\sqlite.ps1
|
||
|
|
||
|
# http://www.spech.de/2016/06/sqlite-mit-der-powershell-nutzen/
|
||
|
if (! $(Get-Command Invoke-SqliteQuery -ErrorAction SilentlyContinue)) {
|
||
|
Write-Log -Message "Aktiviere SQLITE Modul" -Level Warn
|
||
|
Import-Module "$($ScriptDir)\modules\sqlite\PSSQLite.psm1"
|
||
|
}
|
||
|
|
||
|
|
||
|
######################################################################################
|
||
|
######################################################################################
|
||
|
# Script
|
||
|
######################################################################################
|
||
|
######################################################################################
|
||
|
# Starte Logging
|
||
|
Write-Log -Message "Starte Picture Import" -Level Info
|
||
|
|
||
|
######################################################################################
|
||
|
######################################################################################
|
||
|
# Prüfe auf "User" Daten
|
||
|
######################################################################################
|
||
|
######################################################################################
|
||
|
try {
|
||
|
$getallapiuserdata = $(Get-UserInfo).content | ConvertFrom-Json | Select-Object -expand "data"
|
||
|
}
|
||
|
catch {
|
||
|
Write-Log -Message "Error: $_.Exception.Message - check_user_change - Line Number: $($_.InvocationInfo.ScriptLineNumber)" -Level Error
|
||
|
Send-ErrorMail -Mail "Error: get User-Data from API: $($_.Exception.Message)" -VACSTART "-" -VACENDE "-" -VACID "-"
|
||
|
exit
|
||
|
}
|
||
|
|
||
|
$getallapiuserdata | foreach {
|
||
|
[PSCustomObject]@{
|
||
|
FirstName = $_.attributes.first_name.value | Select-Object -First 1
|
||
|
LastName = $_.attributes.last_name.value | Select-Object -First 1
|
||
|
Email = $_.attributes.email.value | Select-Object -First 1
|
||
|
Position = $_.attributes.position.value | Select-Object -First 1
|
||
|
Abteilung = $_.attributes.department.value.attributes.name | Select-Object -First 1
|
||
|
Tel = $_.attributes.dynamic_99874.value | Select-Object -First 1
|
||
|
Status = $_.attributes.status.value | Select-Object -First 1
|
||
|
Kostenstelle = $_.attributes.cost_centers.value.attributes.name | Select-Object -First 1
|
||
|
Vorgesetzter = $_.attributes.supervisor.value.attributes.email.value | Select-Object -First 1
|
||
|
Fax = $_.attributes.dynamic_137780.value | Select-Object -First 1
|
||
|
ID = $_.attributes.dynamic_99886.value | Select-Object -First 1
|
||
|
PersonioID = $_.attributes.id.value | Select-Object -First 1
|
||
|
Mobile = $_.attributes.dynamic_99877.value | Select-Object -First 1
|
||
|
HomeMobile = $_.attributes.dynamic_130998.value | Select-Object -First 1
|
||
|
} | Where-Object { $_.Status -notLike "inactive" } | ForEach-Object {
|
||
|
if ( ! $_.ID -eq "$null" ) {
|
||
|
$photo = (Get-UserPicture $_.PersonioID).Content 2> $null
|
||
|
Write-Output "Prüfe: $($_.ID)"
|
||
|
$PhotoFile = "C:\Aufgaben\personio\pictures\$($_.ID).jpg"
|
||
|
if (! [string]::IsNullOrWhiteSpace($photo)) {
|
||
|
[System.IO.File]::WriteAllBytes($PhotoFile, $photo)
|
||
|
$file = [io.file]::ReadAllBytes($PhotoFile)
|
||
|
If ($file.length -gt 10000) {
|
||
|
Write-host " - Foto ist zu groß für AAD" -ForegroundColor Red
|
||
|
}
|
||
|
$Current = (Get-ADUser "$($_.ID)" -Properties thumbnailPhoto).thumbnailPhoto
|
||
|
if ($current -eq $null) {
|
||
|
Write-Host " - hat bislang kein Foto... setze" -ForegroundColor Yellow
|
||
|
Set-ADUser -Identity "$($_.ID)" -Replace @{thumbnailPhoto = $file }
|
||
|
}
|
||
|
elseif (Compare-Object $photo $current) {
|
||
|
Write-Host " - hat ein neues Foto... setze" -ForegroundColor Yellow
|
||
|
Set-ADUser -Identity "$($_.ID)" -Replace @{thumbnailPhoto = $file }
|
||
|
}
|
||
|
else {
|
||
|
Write-Host " - Bild bereits aktuell" -ForegroundColor Green
|
||
|
}
|
||
|
}
|
||
|
else {
|
||
|
Write-host " - Kein Foto in Personio vorhanden" -ForegroundColor Yellow
|
||
|
Set-ADUser -Identity "$($_.ID)" -Clear thumbnailPhoto
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
# Starte Logging
|
||
|
Write-Log -Message "Beende Picture Import" -Level Info
|