Files
LAPS/laps.ps1
Jason SECULA a0400a32ec Initial commit
2025-06-05 09:33:18 +02:00

244 lines
8.5 KiB
PowerShell

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$form = New-Object System.Windows.Forms.Form
$form.Text = 'LAPS'
$form.Size = New-Object System.Drawing.Size(380,180)
$form.StartPosition = 'CenterScreen'
$form.MaximizeBox = $false
$form.MinimizeBox = $false
$form.FormBorderStyle = 'Fixed3D'
$usernameLabel = New-Object System.Windows.Forms.Label
$usernameLabel.Location = New-Object System.Drawing.Point(30,20)
$usernameLabel.Text = 'Identifiant :'
$usernameTextbox = New-Object System.Windows.Forms.TextBox
$usernameTextbox.Location = New-Object System.Drawing.Point(120,20)
$usernameTextbox.Size = New-Object System.Drawing.Size(200,20)
#$usernameTextbox.PlaceholderText = 'admin-uid'
$passwordLabel = New-Object System.Windows.Forms.Label
$passwordLabel.Location = New-Object System.Drawing.Point(30,50)
$passwordLabel.Text = 'Mot de passe :'
$passwordTextbox = New-Object System.Windows.Forms.TextBox
$passwordTextbox.Location = New-Object System.Drawing.Point(120,50)
$passwordTextbox.Size = New-Object System.Drawing.Size(200,20)
$passwordTextbox.PasswordChar = '*'
$errorLabel = New-Object System.Windows.Forms.Label
$errorLabel.Location = New-Object System.Drawing.Point(30,100)
$errorLabel.Size = New-Object System.Drawing.Size(200,20)
$errorLabel.Visible = $false
$ValidateCreds = {
try{
if($usernameTextbox.Text -eq ''){
$errorLabel.Text = 'Identifiant manquant'
$errorLabel.ForeColor = 'DarkRed'
$errorLabel.Visible = $true
return
}
if($passwordTextbox.text -eq ''){
$errorLabel.Text = 'Mot de passe manquant'
$errorLabel.ForeColor = 'DarkRed'
$errorLabel.Visible = $true
return
}
$password = ConvertTo-SecureString $passwordTextbox.text -AsPlainText -Force
$creds = New-Object System.Management.Automation.PsCredential "ac-bureautique\$($usernameTextbox.Text)", $password
Get-LapsADPassword -Identity $env:COMPUTERNAME -Credential $creds
$form.DialogResult = 'OK'
$form.Close()
}catch{
$exitCode = $_.exception.errorcode
$errorLabel.Visible = $true
$errorLabel.ForeColor = 'DarkRed'
if($exitCode -eq 49){
$errorLabel.Text = 'Mauvais identifiant/mot de passe'
}else{
$errorLabel.Text = $_
}
}
}
$generateLapsButton = New-Object System.Windows.Forms.Button
$generateLapsButton.Text = 'OK'
$generateLapsButton.Location = New-Object System.Drawing.Point(240,100)
$generateLapsButton.Add_Click($ValidateCreds)
$form.AcceptButton = $generateLapsButton
$form.Controls.add($usernameTextbox)
$form.Controls.add($passwordTextbox)
$form.Controls.add($passwordLabel)
$form.Controls.add($errorLabel)
$form.Controls.add($usernameLabel)
$form.Controls.add($generateLapsButton)
$result = $form.ShowDialog()
#$result = 'OK'
if($result -ne 'OK'){
exit 0
}
$password = ConvertTo-SecureString $passwordTextbox.text -AsPlainText -Force
$creds = New-Object System.Management.Automation.PsCredential "ac-bureautique\$($usernameTextbox.Text)", $password
$form.dispose()
$machineForm = New-Object System.Windows.Forms.Form
$machineForm.Text = 'LAPS'
$machineForm.Size = New-Object System.Drawing.Size(470,270)
$machineForm.StartPosition = 'CenterScreen'
$machineForm.MaximizeBox = $false
$machineForm.MinimizeBox = $false
$machineForm.FormBorderStyle = 'Fixed3D'
# computer fields
# label
$computerNameLabel = New-Object System.Windows.Forms.Label
$computerNameLabel.Location = New-Object System.Drawing.Point(30,20)
$computerNameLabel.Size = New-Object System.Drawing.Size(120,20)
$computerNameLabel.Text = "Nom de l'ordinateur"
# textbox
$computerNameTextbox = New-Object System.Windows.Forms.TextBox
$computerNameTextbox.Location = New-Object System.Drawing.Point(150,20)
$computerNameTextbox.Size = New-Object System.Drawing.Size(200,20)
# calendar
# label
$calendarLabel = New-Object System.Windows.Forms.Label
$calendarLabel.Location = New-Object System.Drawing.Point(30,70)
$calendarLabel.Text = "Date d'expiration"
$calendarLabel.Visible = $false
# calendar
$calendar = New-Object System.Windows.Forms.DateTimePicker
$calendar.Format = [windows.forms.datetimepickerFormat]::custom
$calendar.CustomFormat = "dd/MM/yyyy HH:mm:ss"
#$calendar.MinDate = [DateTime]::Now
$calendar.Location = New-Object System.Drawing.Point(150,70)
$calendar.Visible = $false
# laps fields
# label
$lapsPasswordLabel = New-Object System.Windows.Forms.Label
$lapsPasswordlabel.Location = New-Object System.Drawing.Point(30,120)
$lapsPasswordlabel.Size = New-Object System.Drawing.Size(120,20)
$lapsPasswordlabel.Text = "Mot de passe LAPS"
$lapsPasswordlabel.Visible = $false
# textbox
$lapsPasswordTextbox = New-Object System.Windows.Forms.TextBox
$lapsPasswordTextbox.Location = New-Object System.Drawing.Point(150,120)
$lapsPasswordTextbox.Size = New-Object System.Drawing.Size(200,20)
$lapsPasswordTextbox.ReadOnly = $true
$lapsPasswordTextbox.Visible = $false
$copyPassword = {
if($lapsPasswordTextbox.text -ne ''){
$lapsPasswordTextbox.text | Set-Clipboard
}
}
$copyButton = New-Object System.Windows.Forms.Button
$copyButton.Location = New-Object System.Drawing.Point(360,120)
$copyButton.Text = 'Copier'
$copyButton.Add_Click($copyPassword)
$copyButton.visible = $false
$copyButton.Enabled = $false
$errorLabel = New-Object System.Windows.Forms.Label
$errorLabel.Location = New-Object System.Drawing.Point(30,170)
$errorLabel.Size = New-Object System.Drawing.Size(240,60)
$errorLabel.Visible = $false
$generateLaps = {
$identity = $computerNameTextbox.Text
$effective = $calendar.Value
try{
$var = Set-LapsADPasswordExpirationTime -Identity $identity -Credential $creds -WhenEffective $effective -ErrorAction Stop
$laps = Get-LapsADPassword -Identity $identity -DecryptionCredential $creds -Credential $creds -AsPlainText -ErrorAction Stop
$lapsPasswordTextbox.text = $laps.Password
$errorLabel.Visible = $false
}catch{
switch($_){
"Cannot bind argument to parameter 'Identity' because it is an empty string." { $errorLabel.text = "Nom d'ordinateur manquant" }
"Failed to find the '$($identity)' computer in AD" { $errorLabel.text = "Impossible de trouver l'ordinateur $($identity) dans l'AD" }
default { $errorLabel.text = "$_" }
}
$lapsPasswordTextbox.text = ''
$errorLabel.ForeColor = 'DarkRed'
$errorLabel.Visible = $true
}
}
# label
# generate button
$generateLapsButton = New-Object System.Windows.Forms.Button
$generateLapsButton.Add_Click($generateLaps)
$generateLapsButton.Location = New-Object System.Drawing.Point(280,170)
$generateLapsButton.Size = New-Object System.Drawing.Size(80,20)
$generateLapsButton.text = 'Mettre à jour'
$generateLapsButton.Visible = $false
$showLaps = {
$identity = $computerNameTextbox.Text
$copyButton.Visible = $false
$lapsPasswordTextbox.Visible = $false
$lapsPasswordLabel.Visible = $false
$calendarLabel.Visible = $false
$calendar.Visible = $false
$generateLapsButton.Visible = $false
$calendar.MinDate = [DateTime]'01/01/1970'
try{
$laps = Get-LapsADPassword -Identity $identity -DecryptionCredential $creds -Credential $creds -AsPlainText -ErrorAction Stop
$lapsPasswordTextbox.text = $laps.Password
if($laps.Password -eq ''){
$copyButton.Enabled = $false
}else{
$copyButton.Enabled = $true
}
$errorLabel.Visible = $false
$copyButton.Visible = $true
$calendar.Value = [DateTime]$laps.ExpirationTimestamp
$calendar.MinDate = [DateTime]::Now
$lapsPasswordTextbox.Visible = $true
$lapsPasswordLabel.Visible = $true
$calendarLabel.Visible = $true
$calendar.Visible = $true
$generateLapsButton.Visible = $true
}catch{
switch($_){
"Cannot bind argument to parameter 'Identity' because it is an empty string." { $errorLabel.text = "Nom d'ordinateur manquant" }
"Failed to find the '$($identity)' computer in AD" { $errorLabel.text = "Impossible de trouver l'ordinateur $($identity) dans l'AD" }
default { $errorLabel.text = "$_" }
}
$lapsPasswordTextbox.text = ''
$errorLabel.ForeColor = 'DarkRed'
$errorLabel.Visible = $true
}
}
$showLapsButton = New-Object System.Windows.Forms.Button
$showLapsButton.Location = New-Object System.Drawing.Point(360,18)
$showLapsButton.Text = 'Chercher'
$showLapsButton.Add_Click($showLaps)
$machineForm.Controls.add($computerNameLabel)
$machineForm.Controls.add($computerNameTextbox)
$machineForm.Controls.add($showLapsButton)
$machineForm.Controls.add($calendarLabel)
$machineForm.Controls.add($calendar)
$machineForm.Controls.add($lapsPasswordLabel)
$machineForm.Controls.add($lapsPasswordTextbox)
$machineForm.Controls.add($copyButton)
$machineForm.Controls.add($errorLabel)
$machineForm.Controls.add($generateLapsButton)
$result = $machineForm.ShowDialog()
$machineForm.dispose()
exit 0