cls
Add-Type -AssemblyName System.Windows.Forms
$form = New-Object System.Windows.Forms.Form
$form.Text = 'New Admin User'
$form.Size = New-Object System.Drawing.Size(400,250) # Reduced form size
$form.StartPosition = "CenterScreen"
# Status label
$statusLabel = New-Object System.Windows.Forms.Label
$statusLabel.Location = New-Object System.Drawing.Point(10,10)
$statusLabel.Size = New-Object System.Drawing.Size(360,150) # Reduced the height here
$statusLabel.Font = New-Object System.Drawing.Font("Arial",12,[System.Drawing.FontStyle]::Bold)
$statusLabel.ForeColor = [System.Drawing.Color]::DarkBlue
$statusLabel.Text = ""
$form.Controls.Add($statusLabel)
# add button
$button = New-Object System.Windows.Forms.Button
$button.Size = New-Object System.Drawing.Size(120,40)
$button.Font = New-Object System.Drawing.Font("Arial",12)
$button.Text = 'Add User'
$button.Location = New-Object System.Drawing.Point([int](($form.ClientSize.Width - $button.Width) / 2), [int]($form.ClientSize.Height - $button.Height - 10)) # Change the margin here
$button.Add_Click({
$adminName = "LocalAdmin"
$adminPass = 'Password123'
#$adminPass = 'Pa$$word123'
$suffix = ""
while (Get-WmiObject Win32_UserAccount -filter "Name='$adminName$suffix'" -ErrorAction SilentlyContinue) {
if ($suffix -eq "") {
$suffix = 1
} else {
$suffix++
}
}
$adminName += $suffix
invoke-expression "net user /add $adminName $adminPass"
$user = New-Object System.Security.Principal.NTAccount($adminName)
$sid = $user.Translate([System.Security.Principal.SecurityIdentifier])
$sid = $sid.Value;
New-Item -Path "HKLM:\Software\Microsoft\Windows\CurrentVersion\SharedPC\Exemptions\$sid" -Force
$statusLabel.Text = "User $adminName`nhas been created."
})
$form.Add_Resize({
$button.Top = $form.ClientSize.Height - $button.Height - 10 # Change the margin here
$button.Left = ($form.ClientSize.Width - $button.Width) / 2
})
$form.Controls.Add($button)
# Check user on form load
$adminName = "LocalAdmin"
if (Get-WmiObject Win32_UserAccount -filter "Name='$adminName'" -ErrorAction SilentlyContinue) {
$statusLabel.Text = "User $adminName already exists. `nRecommended not to create."
} else {
$statusLabel.Text = "User $adminName does not exist."
}
$form.ShowDialog()
Комментариев нет:
Отправить комментарий