Blog
Hier poste ich dinge zu Themen die mich beschäftigten.
Suchen
2023-04-30
Powershell Script User anlage mit gui
Hier ist das vollständige PowerShell-Skript mit einer GUI zum Erstellen von Benutzern in einer bestimmten Ziel-OU:
“`powershell
# Import necessary modules
Import-Module ActiveDirectory
Add-Type -AssemblyName PresentationFramework
# Create WPF Window
[xml]$xaml = @”
<Window xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”
xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”
Title=”Benutzeranlage” Height=”400” Width=”400” ResizeMode=”NoResize”>
<Grid>
…
<Button x:Name=”CreateUserButton” Content=”Benutzer anlegen” HorizontalAlignment=”Left” Margin=”10,290,0,0” VerticalAlignment=”Top” Width=”350” Height=”30”/>
</Grid>
</Window>
“@
# Load XAML and create variables for the controls
$reader = (New-Object System.Xml.XmlNodeReader $xaml)
$window = [Windows.Markup.XamlReader]::Load($reader)
$controls = $window.FindName -Name * -Force | ?{ $_ -is [System.Windows.Controls.Control] } | %{
New-Variable -Name $_.Name -Value $_ -Scope 2 -Force
}
# Create the user in the specified OU
function CreateUser {
$name = $NameBox.Text
$vorname = $VornameBox.Text
$username = $UsernameBox.Text
$stadt = $StadtBox.Text
$land = $LandBox.Text
$telefonnummer = $TelefonnummerBox.Text
$mobilnummer = $MobilnummerBox.Text
$emailadresse = $EmailadresseBox.Text
# You need to replace ‘OU=YourOU,DC=domain,DC=com’ with the distinguished name of your target OU
$ou = “OU=YourOU,DC=domain,DC=com”
$params = @{
‘GivenName’ = $vorname
‘Name’ = $name
‘SamAccountName’ = $username
‘City’ = $stadt
‘Country’ = $land
‘OfficePhone’ = $telefonnummer
‘MobilePhone’ = $mobilnummer
‘EmailAddress’ = $emailadresse
‘AccountPassword’ = (ConvertTo-SecureString “TempPass123!” -AsPlainText -Force)
‘Enabled’ = $true
‘Path’ = $ou
}
try {
New-ADUser @params
[System.Windows.MessageBox]::Show(”Benutzer ‘$username’ wurde erfolgreich erstellt.”)
} catch {
[System.Windows.MessageBox]::Show(”Fehler beim Erstellen des Benutzers: $_”)
}
}
# Assign the function to the button click event
$CreateUserButton.Add_Click({ CreateUser })
# Show the window
$window.ShowDialog() | Out-Null
“`
Bitte ersetzen Sie `’OU=YourOU,DC=domain,DC=com’` durch den Distinguished Name Ihrer Ziel-OU.
Das Skript erstellt Benutzer mit einem temporären Passwort `TempPass123!`. Sie sollten dies ändern, um den Anforderungen Ihrer Organisation zu entsprechen.
Admin - 19:05:59 @ Projekte, Powershell-Skripte | Kommentar hinzufügen