通过 GUI 改进进行密码重置
Password Reset with GUI Improvements
我正在研究带有 GUI 的密码重置脚本,有 3 个问题如果有人能帮助我 :) 前提是当我的用户扫描他们的卡时,它会显示他们的用户名和密码,它起作用因为它应该是我想进一步发展它。
- 如果框为空并且选择了“确定”,那么它会使用 CSV 中的每一行,有没有办法设置它,以便在输入内容之前无法按下“确定”按钮。使用的 RFID 扫描仪总是命中 return.
- 弹出框显示后,我想在它关闭前添加一个 15 秒的计时器,然后当弹出框消失时,它会要求下一个人扫描他们的卡片。就目前而言,当消息框出现时,它会关闭原始表单。有什么办法让我保持输入表单打开而不是关闭,并在表单顶部显示消息框?我基本上想在向用户提供详细信息后重置表单。
- 当我在顶部创建了表单后,我可以对其进行样式设置。我是否可以对消息框执行相同的操作以便对其进行样式设置?非常感谢您。
如有任何帮助,我们将不胜感激。我还在学习 PowerShell 的基础知识
代码
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$Spreadsheet = Import-CSV "C:\Desktop\Password Reset\Passwords.csv" -Header First,Second,Username,Password,RFID
$form = New-Object System.Windows.Forms.Form
$form.Text = 'Password Reset System'
$form.Size = New-Object System.Drawing.Size(800,600) #Box Size
$form.StartPosition = 'CenterScreen'
$Form.BackColor = "#A4F9F4"
$Form.ForeColor = "#000000"
$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(350,350) #OK Button Location
$okButton.Size = New-Object System.Drawing.Size(100,24) #OK Button Size
$okButton.Text = 'OK'
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $okButton
$form.Controls.Add($okButton)
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(270,300) #Label Location
$label.Size = New-Object System.Drawing.Size(280,20) #Label Size
$label.Text = 'Scan your Card:'
$form.Controls.Add($label)
$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(270,320) #Textbox Location
$textBox.Size = New-Object System.Drawing.Size(260,20) #Textbox Size
$form.Controls.Add($textBox)
$form.Topmost = $true
$form.Add_Shown({$textBox.Select()})
$result = $form.ShowDialog()
if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
$resetCardNumber = $textBox.Text
$targetUser = $Spreadsheet | Where-Object RFID -eq $resetCardNumber
}
if($targetUser){
Set-ADAccountPassword $targetUser.Username -Reset -NewPassword (Convertto-Securestring -AsPlainText $targetUser.Password -Force)
#Write-Host "Password reset to $($targetUser.Password) for $($targetUser.First) $($targetUser.Second) "($($targetUser.Username))" with card number $($targetUser.RFID)"
[System.Windows.MessageBox]::Show("Your Username: $($targetUser.Username) `nYour Password: $($targetUser.Password) ")
}
else {
#Write-Host "User with Card Number '$resetCardNumber' could not be found!"
[System.Windows.MessageBox]::Show("Your Username could not be found in the system")
}
CSV 文件如下:
First,Second,Username,Password,RFID
John,Smith,JS219401,YourPassword,84192011
Barry,Henry,BH219401,YourPassword,92832839
希望下面(阅读内联评论)解释您的问题 1 和 2。
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
[System.Windows.Forms.Application]::EnableVisualStyles()
$Spreadsheet = Import-CSV "C:\Desktop\Password Reset\Passwords.csv" -Header First,Second,Username,Password,RFID
$form = New-Object System.Windows.Forms.Form
$form.Text = 'Password Reset System'
$form.Size = New-Object System.Drawing.Size(800,600) #Box Size
$form.StartPosition = 'CenterScreen'
$Form.BackColor = "#A4F9F4"
$Form.ForeColor = "#000000"
$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(350,350) #OK Button Location
$okButton.Size = New-Object System.Drawing.Size(100,24) #OK Button Size
$okButton.Text = 'OK'
# remove the next line if you want the mainform to stay open
# $okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
# initialize the button's Enabled status to $false
$okButton.Enabled = $false
# add event handler code for the button's Click event
# this can only be the case if the button was Enabled
$okButton.Add_Click({
$resetCardNumber = $textBox.Text
$targetUser = $Spreadsheet | Where-Object {$_.RFID -eq $resetCardNumber}
if($targetUser){
Set-ADAccountPassword $targetUser.Username -Reset -NewPassword (Convertto-Securestring -AsPlainText $targetUser.Password -Force)
#Write-Host "Password reset to $($targetUser.Password) for $($targetUser.First) $($targetUser.Second) "($($targetUser.Username))" with card number $($targetUser.RFID)"
[System.Windows.MessageBox]::Show("Your Username: $($targetUser.Username) `nYour Password: $($targetUser.Password) ")
}
else {
#Write-Host "User with Card Number '$resetCardNumber' could not be found!"
[System.Windows.MessageBox]::Show("Your Username could not be found in the system")
}
# reset the form by clearing out the textbox
$textBox.Text = '' # this also triggers the OK button to become disabled
})
$form.AcceptButton = $okButton
$form.Controls.Add($okButton)
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(270,300) #Label Location
$label.Size = New-Object System.Drawing.Size(280,20) #Label Size
$label.Text = 'Scan your Card:'
$form.Controls.Add($label)
$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(270,320) #Textbox Location
$textBox.Size = New-Object System.Drawing.Size(260,20) #Textbox Size
# add a TextChanged event handler to toggle the OK button's Enabled status
$textBox.Add_TextChanged({
$okButton.Enabled = (![string]::IsNullOrWhiteSpace($this.Text))
})
$form.Controls.Add($textBox)
$form.Topmost = $true
$form.Add_Shown({$textBox.Select()})
$result = $form.ShowDialog()
# Important, remove the form from memory when done
$form.Dispose()
虽然我不太确定你对第三个问题的意思:“我可以对消息框做同样的事情以便可以设置样式吗?”
由于您使用的是 System.Windows.MessageBox
,因此您无法对其默认外观进行任何更改,因为 消息框是预制的模态对话框,可以向用户显示文本消息用户。
如果你想要一个自定义消息框,你必须自己创建一个。
我正在研究带有 GUI 的密码重置脚本,有 3 个问题如果有人能帮助我 :) 前提是当我的用户扫描他们的卡时,它会显示他们的用户名和密码,它起作用因为它应该是我想进一步发展它。
- 如果框为空并且选择了“确定”,那么它会使用 CSV 中的每一行,有没有办法设置它,以便在输入内容之前无法按下“确定”按钮。使用的 RFID 扫描仪总是命中 return.
- 弹出框显示后,我想在它关闭前添加一个 15 秒的计时器,然后当弹出框消失时,它会要求下一个人扫描他们的卡片。就目前而言,当消息框出现时,它会关闭原始表单。有什么办法让我保持输入表单打开而不是关闭,并在表单顶部显示消息框?我基本上想在向用户提供详细信息后重置表单。
- 当我在顶部创建了表单后,我可以对其进行样式设置。我是否可以对消息框执行相同的操作以便对其进行样式设置?非常感谢您。
如有任何帮助,我们将不胜感激。我还在学习 PowerShell 的基础知识
代码
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$Spreadsheet = Import-CSV "C:\Desktop\Password Reset\Passwords.csv" -Header First,Second,Username,Password,RFID
$form = New-Object System.Windows.Forms.Form
$form.Text = 'Password Reset System'
$form.Size = New-Object System.Drawing.Size(800,600) #Box Size
$form.StartPosition = 'CenterScreen'
$Form.BackColor = "#A4F9F4"
$Form.ForeColor = "#000000"
$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(350,350) #OK Button Location
$okButton.Size = New-Object System.Drawing.Size(100,24) #OK Button Size
$okButton.Text = 'OK'
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $okButton
$form.Controls.Add($okButton)
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(270,300) #Label Location
$label.Size = New-Object System.Drawing.Size(280,20) #Label Size
$label.Text = 'Scan your Card:'
$form.Controls.Add($label)
$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(270,320) #Textbox Location
$textBox.Size = New-Object System.Drawing.Size(260,20) #Textbox Size
$form.Controls.Add($textBox)
$form.Topmost = $true
$form.Add_Shown({$textBox.Select()})
$result = $form.ShowDialog()
if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
$resetCardNumber = $textBox.Text
$targetUser = $Spreadsheet | Where-Object RFID -eq $resetCardNumber
}
if($targetUser){
Set-ADAccountPassword $targetUser.Username -Reset -NewPassword (Convertto-Securestring -AsPlainText $targetUser.Password -Force)
#Write-Host "Password reset to $($targetUser.Password) for $($targetUser.First) $($targetUser.Second) "($($targetUser.Username))" with card number $($targetUser.RFID)"
[System.Windows.MessageBox]::Show("Your Username: $($targetUser.Username) `nYour Password: $($targetUser.Password) ")
}
else {
#Write-Host "User with Card Number '$resetCardNumber' could not be found!"
[System.Windows.MessageBox]::Show("Your Username could not be found in the system")
}
CSV 文件如下:
First,Second,Username,Password,RFID
John,Smith,JS219401,YourPassword,84192011
Barry,Henry,BH219401,YourPassword,92832839
希望下面(阅读内联评论)解释您的问题 1 和 2。
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
[System.Windows.Forms.Application]::EnableVisualStyles()
$Spreadsheet = Import-CSV "C:\Desktop\Password Reset\Passwords.csv" -Header First,Second,Username,Password,RFID
$form = New-Object System.Windows.Forms.Form
$form.Text = 'Password Reset System'
$form.Size = New-Object System.Drawing.Size(800,600) #Box Size
$form.StartPosition = 'CenterScreen'
$Form.BackColor = "#A4F9F4"
$Form.ForeColor = "#000000"
$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(350,350) #OK Button Location
$okButton.Size = New-Object System.Drawing.Size(100,24) #OK Button Size
$okButton.Text = 'OK'
# remove the next line if you want the mainform to stay open
# $okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
# initialize the button's Enabled status to $false
$okButton.Enabled = $false
# add event handler code for the button's Click event
# this can only be the case if the button was Enabled
$okButton.Add_Click({
$resetCardNumber = $textBox.Text
$targetUser = $Spreadsheet | Where-Object {$_.RFID -eq $resetCardNumber}
if($targetUser){
Set-ADAccountPassword $targetUser.Username -Reset -NewPassword (Convertto-Securestring -AsPlainText $targetUser.Password -Force)
#Write-Host "Password reset to $($targetUser.Password) for $($targetUser.First) $($targetUser.Second) "($($targetUser.Username))" with card number $($targetUser.RFID)"
[System.Windows.MessageBox]::Show("Your Username: $($targetUser.Username) `nYour Password: $($targetUser.Password) ")
}
else {
#Write-Host "User with Card Number '$resetCardNumber' could not be found!"
[System.Windows.MessageBox]::Show("Your Username could not be found in the system")
}
# reset the form by clearing out the textbox
$textBox.Text = '' # this also triggers the OK button to become disabled
})
$form.AcceptButton = $okButton
$form.Controls.Add($okButton)
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(270,300) #Label Location
$label.Size = New-Object System.Drawing.Size(280,20) #Label Size
$label.Text = 'Scan your Card:'
$form.Controls.Add($label)
$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(270,320) #Textbox Location
$textBox.Size = New-Object System.Drawing.Size(260,20) #Textbox Size
# add a TextChanged event handler to toggle the OK button's Enabled status
$textBox.Add_TextChanged({
$okButton.Enabled = (![string]::IsNullOrWhiteSpace($this.Text))
})
$form.Controls.Add($textBox)
$form.Topmost = $true
$form.Add_Shown({$textBox.Select()})
$result = $form.ShowDialog()
# Important, remove the form from memory when done
$form.Dispose()
虽然我不太确定你对第三个问题的意思:“我可以对消息框做同样的事情以便可以设置样式吗?”
由于您使用的是 System.Windows.MessageBox
,因此您无法对其默认外观进行任何更改,因为 消息框是预制的模态对话框,可以向用户显示文本消息用户。
如果你想要一个自定义消息框,你必须自己创建一个。