Powershell Integration - Benutzer aus ActiveDirectory (AD) anlegen
Alle antony Dienste (vor allem der Mainservice) müssen beendet sein (nur die Datenbank nicht)
Das Script muss als Administrator ausgeführt werden
Es müssen vorher auf dem Maschine die AD-Tools in den Windows Features installiert werden!Add-WindowsCapability -online -Name Rsat.ActiveDirectory.DS-LDS.Tools~~~~0.0.1.0
Ebenso muss der MySQL .NET Treiber installiert werden
$server = "db-server"
$database = "antony"
$username = "user"
$password = "password"
#Add-WindowsCapability -online -Name Rsat.ActiveDirectory.DS-LDS.Tools~~~~0.0.1.0
Import-Module ActiveDirectory
[void][System.Reflection.Assembly]::LoadWithPartialName("MySql.Data")
$connectionString = "server=$server;uid=$username;pwd=$password;database=$database;"
$connection = New-Object MySql.Data.MySqlClient.MySqlConnection
$connection.ConnectionString = $connectionString
$connection.Open()
$users = Get-ADUser -Filter * -Property GivenName,Surname,EmailAddress,TelephoneNumber,Department,SamAccountName,SID
foreach ($user in $users) {
$sqlCommand = $connection.CreateCommand()
$sqlCommand.CommandText = "INSERT INTO user SET username = '@Benutzer', firstname = '@Vorname', lastname = '@Nachname', shortcut = '@Shortcut', division = '@Abteilung', position = '@Position', phone = '@Telefonnummer', email = '@Email', windows_username = '@Benutzer', sid = '@SID';"
$sqlCommand.Parameters.Clear()
$sqlCommand.Parameters.Add((New-Object MySql.Data.MySqlClient.MySqlParameter("@Benutzer", $user.SamAccountName)))
$sqlCommand.Parameters.Add((New-Object MySql.Data.MySqlClient.MySqlParameter("@Vorname", $user.GivenName)))
$sqlCommand.Parameters.Add((New-Object MySql.Data.MySqlClient.MySqlParameter("@Nachname", $user.Surname)))
$sqlCommand.Parameters.Add((New-Object MySql.Data.MySqlClient.MySqlParameter("@Shortcut", $user.SamAccountName)))
$sqlCommand.Parameters.Add((New-Object MySql.Data.MySqlClient.MySqlParameter("@Abteilung", $user.Department)))
$sqlCommand.Parameters.Add((New-Object MySql.Data.MySqlClient.MySqlParameter("@Position", $user.Position.Department)))
$sqlCommand.Parameters.Add((New-Object MySql.Data.MySqlClient.MySqlParameter("@Email", $user.EmailAddress)))
$sqlCommand.Parameters.Add((New-Object MySql.Data.MySqlClient.MySqlParameter("@Telefonnummer", $user.TelephoneNumber)))
$sqlCommand.Parameters.Add((New-Object MySql.Data.MySqlClient.MySqlParameter("@SID", $user.SID.ToString())))
$sqlCommand.ExecuteNonQuery()
}
$connection.Close()