Powershell password GUI (easy) - powershell

I'm looking for a simple script for a password-check in a Powershell GUI.
I have tried different types of codes but sometimes it can't read the textbox and sometimes it can't register the clicks.
So I have two buttons and a textbox.
If I have the right password and press enter I want it to do something.
If I have the wrong password I can try again.
But I have no idea how to do this with buttons in a GUI.
So I wonder I anyone has done something similar?
Heres what I have now about the "if" statment:
if ($Result = [System.Windows.Forms.DialogResult]::OK) {
$OKButton.Add_Click( { $OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK })
$OKButton.Add_Enter( { $OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK })
$CancelButton.Add_Click( { $CancelButton.Add_Click( { $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel }) })
if (([string]::IsNullOrEmpty($MaskedTextBox)) -or ([string]::IsNullOrEmpty($Pword))) {
write-host "Passwords can not be NULL or empty"
}
else {
if ($MaskedTextBox -cne $PWord) {
write-host "Fel"
}
else {
($MaskedTextBox -eq $PWord)
Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\USBSTOR\" -Name "start" -Value 3
write-host "Rätt"
}
}
}

This should do the trick:
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$password = "Test"
$script:isMatch = $false
[System.Windows.Forms.Application]::EnableVisualStyles()
$form = New-Object System.Windows.Forms.Form
$form.Visible = $false
[void]$form.SuspendLayout()
$form.Text = 'Password Window'
$form.ClientSize = New-Object System.Drawing.Size(160,120)
$form.StartPosition = 'CenterScreen'
$form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle
$CTRL_label1 = New-Object System.Windows.Forms.Label
$CTRL_label1.Location = New-Object System.Drawing.Point(10,10)
$CTRL_label1.Size = New-Object System.Drawing.Size(280,20)
$CTRL_label1.Text = 'Please enter password:'
$CTRL_label1.Name = 'Label1'
[void]$form.Controls.Add($CTRL_label1)
$CTRL_password = New-Object System.Windows.Forms.TextBox
$CTRL_password.Location = New-Object System.Drawing.Point(10,30)
$CTRL_password.PasswordChar = '*'
$CTRL_password.Size = New-Object System.Drawing.Size(140,20)
$CTRL_password.MaxLength = 20
$CTRL_password.Name = 'Password'
[void]$form.Controls.Add($CTRL_password)
$CTRL_label2 = New-Object System.Windows.Forms.Label
$CTRL_label2.Location = New-Object System.Drawing.Point(10,60)
$CTRL_label2.Size = New-Object System.Drawing.Size(280,20)
$CTRL_label2.ForeColor = [System.Drawing.Color]::Red
$CTRL_label2.Text = ''
$CTRL_label2.Name = 'Label2'
[void]$form.Controls.Add($CTRL_label2)
$CTRL_Button = New-Object System.Windows.Forms.Button
$CTRL_Button.Location = New-Object System.Drawing.Point(10,90)
$CTRL_Button.Size = New-Object System.Drawing.Size(140,23)
$CTRL_Button.Text = 'OK'
$CTRL_Button.Name = 'OK'
$CTRL_Button.Add_Click( {
if ( $CTRL_password.Text.Trim() -ceq $password ) {
$script:isMatch = $true
[void]$form.Close()
[void]$form.Dispose()
}
else {
$CTRL_label2.Text = 'wrong password!'
}
} )
[void]$form.Controls.Add($CTRL_Button)
[void]$form.ResumeLayout()
$userInput = $form.ShowDialog()
if( $script:isMatch ) {
# do anything => passwort is ok!
"OK!"
}

Related

Custom button logic Powershell

I'm creating a script for silent installation of some applications, with a choice box!
I also created a custom button, but I need help to create the button logic.
I thank everyone!
Add-Type -AssemblyName System.Windows.Forms
$Form = New-Object System.Windows.Forms.Form
$Form.Text ='Softwares Padrão'
$Form.Width = 500
$Form.Height = 300
$Form.AutoSize = $true
$Painel = New-Object System.Windows.Forms.Panel
$Painel.Height = 200
$Painel.Width = 154
$Painel.Location = New-Object System.Drawing.Point(24,45)
$Painel.BackColor = [System.Drawing.ColorTranslator]::FromHtml("#ffffff")
$CheckBox_Selec = New-Object System.Windows.Forms.CheckBox
$CheckBox_Selec.Text = "Selecionar Todos"
$CheckBox_Selec.Width = 200
$CheckBox_Selec.Height = 20
$CheckBox_Selec.AutoSize = $true
$CheckBox_Selec.Location = New-Object System.Drawing.Size(34, 20)
$CheckBox_Selec.Checked = $false
$Form.Controls.Add($CheckBox_Selec)
$CheckBox_Selec1 = New-Object System.Windows.Forms.CheckBox
$CheckBox_Selec1.Text = "Winrar"
$CheckBox_Selec1.AutoSize = $true
$CheckBox_Selec1.Location = New-Object System.Drawing.Size(10, 10)
$CheckBox_Selec1.Checked = $false
$Form.Controls.Add($CheckBox_Selec1)
$CheckBox_Selec2 = New-Object System.Windows.Forms.CheckBox
$CheckBox_Selec2.Text = "Java"
$CheckBox_Selec2.AutoSize = $true
$CheckBox_Selec2.Location = New-Object System.Drawing.Size(10, 30)
$CheckBox_Selec2.Checked = $false
$Form.Controls.Add($CheckBox_Selec2)
$CheckBox_Selec3 = New-Object System.Windows.Forms.CheckBox
$CheckBox_Selec3.Text = "Adobe Reader"
$CheckBox_Selec3.AutoSize = $true
$CheckBox_Selec3.Location = New-Object System.Drawing.Size(10, 50)
$CheckBox_Selec3.Checked = $false
$Form.Controls.Add($CheckBox_Selec3)
$CheckBox_Selec4 = New-Object System.Windows.Forms.CheckBox
$CheckBox_Selec4.Text = "Office 2013x32"
$CheckBox_Selec4.AutoSize = $true
$CheckBox_Selec4.Location = New-Object System.Drawing.Size(10, 70)
$CheckBox_Selec4.Checked = $false
$Form.Controls.Add($CheckBox_Selec4)
$CheckBox_Selec5 = New-Object System.Windows.Forms.CheckBox
$CheckBox_Selec5.Text = "Google Chrome"
$CheckBox_Selec5.AutoSize = $true
$CheckBox_Selec5.Location = New-Object System.Drawing.Size(10, 90)
$CheckBox_Selec5.Checked = $false
$Form.Controls.Add($CheckBox_Selec5)
$CheckBox_Selec6 = New-Object System.Windows.Forms.CheckBox
$CheckBox_Selec6.Text = "CutePdf"
$CheckBox_Selec6.AutoSize = $true
$CheckBox_Selec6.Location = New-Object System.Drawing.Size(10, 110)
$CheckBox_Selec6.Checked = $false
$Form.Controls.Add($CheckBox_Selec6)
$CheckBox_Selec7 = New-Object System.Windows.Forms.CheckBox
$CheckBox_Selec7.Text = "Oracle11gx32"
$CheckBox_Selec7.AutoSize = $true
$CheckBox_Selec7.Location = New-Object System.Drawing.Size(10, 130)
$CheckBox_Selec7.Checked = $false
$Form.Controls.Add($CheckBox_Selec7)
$CheckBox_Selec8 = New-Object System.Windows.Forms.CheckBox
$CheckBox_Selec8.Text = "Piramide"
$CheckBox_Selec8.AutoSize = $true
$CheckBox_Selec8.Location = New-Object System.Drawing.Size(10, 150)
$CheckBox_Selec8.Checked = $false
$Form.Controls.Add($CheckBox_Selec8)
$CheckBox_Selec9 = New-Object System.Windows.Forms.CheckBox
$CheckBox_Selec9.Text = "Rh3"
$CheckBox_Selec9.AutoSize = $true
$CheckBox_Selec9.Location = New-Object System.Drawing.Size(10, 170)
$CheckBox_Selec9.Checked = $false
$Form.Controls.Add($CheckBox_Selec9)
$Button1 = New-Object System.Windows.Forms.Button
$Button1.Text = "Instalar"
$Button1.AutoSize = $true
$Button1.Width = 60
$Button1.Height = 30
$Button1.Location = New-Object System.Drawing.Point(290,215)
$Button1.Font = New-Object System.Drawing.Font('Microsoft Sans Serif', 10)
$Button1.Enabled = $false
$Form.Controls.Add($Button1)
$Form.controls.AddRange(#($Painel,$CheckBox_Selec,$Button1))
$Painel.controls.AddRange(#($CheckBox_Selec1,$CheckBox_Selec2,$CheckBox_Selec3,$CheckBox_Selec4,$CheckBox_Selec5,$CheckBox_Selec6,$CheckBox_Selec7,$CheckBox_Selec8,$CheckBox_Selec9))
$CheckBox_Selec.Add_CheckStateChanged({$CheckBox_Selec1.Checked = $CheckBox_Selec.Checked})
$CheckBox_Selec.Add_CheckStateChanged({$CheckBox_Selec2.Checked = $CheckBox_Selec.Checked})
$CheckBox_Selec.Add_CheckStateChanged({$CheckBox_Selec3.Checked = $CheckBox_Selec.Checked})
$CheckBox_Selec.Add_CheckStateChanged({$CheckBox_Selec4.Checked = $CheckBox_Selec.Checked})
$CheckBox_Selec.Add_CheckStateChanged({$CheckBox_Selec5.Checked = $CheckBox_Selec.Checked})
$CheckBox_Selec.Add_CheckStateChanged({$CheckBox_Selec6.Checked = $CheckBox_Selec.Checked})
$CheckBox_Selec.Add_CheckStateChanged({$CheckBox_Selec7.Checked = $CheckBox_Selec.Checked})
$CheckBox_Selec.Add_CheckStateChanged({$CheckBox_Selec8.Checked = $CheckBox_Selec.Checked})
$CheckBox_Selec.Add_CheckStateChanged({$CheckBox_Selec9.Checked = $CheckBox_Selec.Checked})
Function Test-AnyButtonCheked {
if (
$CheckBox_Selec.Checked -or
$CheckBox_Selec1.Checked -or
$CheckBox_Selec2.Checked -or
$CheckBox_Selec3.Checked -or
$CheckBox_Selec4.Checked -or
$CheckBox_Selec5.Checked -or
$CheckBox_Selec6.Checked -or
$CheckBox_Selec7.Checked -or
$CheckBox_Selec8.Checked -or
$CheckBox_Selec9.Checked
) {
$Button1.Enabled = $true
}
else {
$Button1.Enabled = $false
}
}
$CheckBox_Selec.Add_CheckedChanged( { Test-AnyButtonCheked})
$CheckBox_Selec1.Add_CheckedChanged( { Test-AnyButtonCheked})
$CheckBox_Selec2.Add_CheckedChanged( { Test-AnyButtonCheked})
$CheckBox_Selec3.Add_CheckedChanged( { Test-AnyButtonCheked})
$CheckBox_Selec4.Add_CheckedChanged( { Test-AnyButtonCheked})
$CheckBox_Selec5.Add_CheckedChanged( { Test-AnyButtonCheked})
$CheckBox_Selec6.Add_CheckedChanged( { Test-AnyButtonCheked})
$CheckBox_Selec7.Add_CheckedChanged( { Test-AnyButtonCheked})
$CheckBox_Selec8.Add_CheckedChanged( { Test-AnyButtonCheked})
$CheckBox_Selec9.Add_CheckedChanged( { Test-AnyButtonCheked})
[void] $Form.ShowDialog()
This is my structure, I know it's not in the best practices but it's running very well!
I would like to install the applications according to the checkbox chosen by the user!
I would like to configure the button so that if the user selects, "install all" he will install all applications or just the chosen checkbox.
Thank you for your help!

powershell progressbar in groupbox GUI

I'm working on a script but for some reason my ProgressBar won't appear in my groupbox.
I tried a lot of different things (like making the groupbox trensparent) but nothing really work.
Here is my code 'simplified' with just the problematic parts.
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object System.Windows.Forms.Form
$Form.ClientSize = ‘345,160’
$Form.Text = " bug progressbar "
$Form.StartPosition = "CenterScreen"
#Functions
Function BDDchoice(){
$BDD = $args[0]
$Label2.Text = "it takes a few seconds"
$ProgressBarcheck = New-Object System.Windows.Forms.ProgressBar
$ProgressBarcheck.Location = New-Object System.Drawing.Point(230,49)
$ProgressBarcheck.Size = New-Object System.Drawing.Size(98, 23)
$ProgressBarcheck.Style = "Marquee"
$ProgressBarcheck.MarqueeAnimationSpeed = 20
$ButtonNEXT1.Hide()
$Form.Controls.Add($ProgressBarcheck);
$job1 ={
sleep 10
$bdd_min = $args[0]
echo $bdd_min
}
Start-Job -ScriptBlock $job1 -ArgumentList $BDD -Name bdd
do { [System.Windows.Forms.Application]::DoEvents() } until ((Get-Job -Name bdd).State -eq "Completed")
Wait-Job -Name bdd
$resultat = Receive-Job -Name bdd -Keep
Get-Job | Remove-Job -Force
$TextBoxBDD.text = $resultat
$ProgressBarcheck.Hide()
$ButtonNEXT1.Visible = $true
$Label2.Text = "BDD:"
}
#variables
$BDDXS = #('SCHEM1','SCHEM2','SCHEM3','SCHEM4')
$BDD1 = 'BDD1'
$BDD2 = 'BDD2'
$BDD3 = 'BDD3'
$BDD4 = 'BDD4'
$BDD = $null
#groupbox
$GroupBoxCREATE = New-Object System.Windows.Forms.GroupBox
$GroupBoxCREATE.Location = New-Object System.Drawing.Point(10,5)
$GroupBoxCREATE.Width = 325
$GroupBoxCREATE.Height = 150
$GroupBoxCREATE.Text = " Create "
#combobox ZONE
$comboBoxTRIG = New-Object System.Windows.Forms.ComboBox
$comboBoxTRIG.Location = New-Object System.Drawing.Point(25, 50)
$comboBoxTRIG.Size = New-Object System.Drawing.Size(200, 200)
foreach($BDDX in $BDDXS)
{
$comboBoxTRIG.Items.add($BDDX)
}
$comboBoxTRIG.Text = "REGION"
$comboBoxTRIG.AutoCompleteMode = "SuggestAppend"
$comboBoxTRIG.AutoCompleteSource = "ListItems"
$comboBoxTRIG.SelectedIndex ="0"
#butons
$ButtonNEXT1 = New-Object System.Windows.Forms.Button
$ButtonNEXT1.Location = New-Object System.Drawing.Point(230,49)
$ButtonNEXT1.Size = New-Object System.Drawing.Size(98, 23)
$ButtonNEXT1.Text = "OK"
$ButtonNEXT1.add_Click({
$zone = $comboBoxTRIG.SelectedItem.ToString()
if ($zone -like "SCHEM1"){
$BDD = $BDD1
}
if ($zone -like "SCHEM2") {
$BDD = $BDD2
}
if ($zone -like "SCHEM3") {
$BDD = $BDD3
}
if ($zone -like "SCHEM4") {
$BDD = $BDD4
}
BDDchoice($BDD)
})
#labels
$Label1 = New-Object System.Windows.Forms.Label
$Label1.Location = New-Object System.Drawing.Point(25,25)
$Label1.Text = "REGION :"
$Label2 = New-Object System.Windows.Forms.Label
$Label2.Location = New-Object System.Drawing.Point(25,80)
$label2.Width = 200
$Label2.Text = "BDD :"
#textbox
$TextBoxBDD = New-Object System.Windows.Forms.TextBox
$TextBoxBDD.Location = New-Object System.Drawing.Point(25,100)
$TextBoxBDD.Width = 200
$TextBoxBDD.ReadOnly = $true
$TextBoxBDD.Text = ""
$TextboxBDD.BackColor = "white"
#list form's butons/lists/combobox
$Form.Controls.AddRange(#($TextBoxBDD))
$Form.controls.AddRange(#($Label1,$Label2))
$Form.controls.AddRange(#($ButtonNEXT1))
$Form.controls.AddRange(#($comboBoxTRIG))
$Form.controls.AddRange(#($GroupBoxCREATE))
$Form.ShowDialog()
i'm interested in any idea.
My coworkers could not find anything but I must admit we are far from expert (i'm still learning and so are they).
$Form.Controls.Add($ProgressBarcheck);
It looks like you're adding Progress Bar to your form, NOT the groupbox. Add progress bar to groupbox then add groupbox to form.
$GroupBoxCREATE.Controls.Add($ProgressBarcheck);
$Form.Controls.Add($GroupBoxCREATE);

Prevent Powershell GUI from closing

I want to prevent my Powershell Form from closing after a submit button is pressed. After the function "search_PC" there is no more code and the form closes. I want to still be able to write stuff in there after the function is finished. I have already tried the pause function and while($true) loops. I would be very happy about an answer.
Import-Module ActiveDirectory
# Assembly for GUI
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
# Create Form
$objForm = New-Object System.Windows.Forms.Form
$objForm.Size = New-Object System.Drawing.Size(400,500)
$objForm.Backcolor="white"
$objForm.StartPosition = "CenterScreen"
$objForm.Text = "Example Software"
$objForm.Icon="C:\Users\...\icon.ico"
# Label
$objLabel1 = New-Object System.Windows.Forms.Label
$objLabel1.Location = New-Object System.Drawing.Size(80,30)
$objLabel1.Size = New-Object System.Drawing.Size(60,20)
$objLabel1.Text = "UserID:"
$objForm.Controls.Add($objLabel1)
# Textbox
$objTextBox1 = New-Object System.Windows.Forms.TextBox
$objTextBox1.Location = New-Object System.Drawing.Size(160,28)
$objTextBox1.Size = New-Object System.Drawing.Size(100,20)
$objForm.Controls.Add($objTextBox1)
# Label
$objLabel2 = New-Object System.Windows.Forms.Label
$objLabel2.Location = New-Object System.Drawing.Size(80,72)
$objLabel2.Size = New-Object System.Drawing.Size(80,20)
$objLabel2.Text = "PC-Name:"
$objForm.Controls.Add($objLabel2)
# Textbox
$objTextBox2 = New-Object System.Windows.Forms.TextBox
$objTextBox2.Location = New-Object System.Drawing.Size(160,70)
$objTextBox2.Size = New-Object System.Drawing.Size(100,20)
$objForm.Controls.Add($objTextBox2)
# Button for closing
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(100,220)
$CancelButton.Size = New-Object System.Drawing.Size(163,25)
$CancelButton.Text = "Exit"
$CancelButton.Name = "Exit"
$CancelButton.DialogResult = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($CancelButton)
# Checkbox
$objTypeCheckbox1 = New-Object System.Windows.Forms.Checkbox
$objTypeCheckbox1.Location = New-Object System.Drawing.Size(80,120)
$objTypeCheckbox1.Size = New-Object System.Drawing.Size(500,20)
$objTypeCheckbox1.Text = "Internal Session Support"
$objTypeCheckbox1.TabIndex = 1
$objTypeCheckbox1.Add_Click({$objTypeCheckbox2.Checked = $false})
$objForm.Controls.Add($objTypeCheckbox1)
# Checkbox
$objTypeCheckbox2 = New-Object System.Windows.Forms.Checkbox
$objTypeCheckbox2.Location = New-Object System.Drawing.Size(80,140)
$objTypeCheckbox2.Size = New-Object System.Drawing.Size(500,20)
$objTypeCheckbox2.Text = "Session Support Internal & external"
$objTypeCheckbox2.TabIndex = 2
$objTypeCheckbox2.Add_Click({$objTypeCheckbox1.Checked = $false})
$objForm.Controls.Add($objTypeCheckbox2)
# Button fo checking the User Input
$SubmitButton = New-Object System.Windows.Forms.Button
$SubmitButton.Location = New-Object System.Drawing.Size(100,190)
$SubmitButton.Size = New-Object System.Drawing.Size(163,25)
$SubmitButton.Text = "Submit"
$SubmitButton.Name = "Submit"
$SubmitButton.DialogResult = "OK"
$objForm.AcceptButton = $SubmitButton
$SubmitButton.Add_Click({
$User = $objTextBox1.Text
$PC = $objTextBox2.Text
# Check if all User Inputs are filled out
if ( !($User.trim()) -or !($PC.trim()) ) {
[void] [Windows.Forms.MessageBox]::Show("Please fill out every value!", "Error", [Windows.Forms.MessageBoxButtons]::ok, [Windows.Forms.MessageBoxIcon]::Warning)}
if($objTypeCheckbox1.checked -eq $true) {$Software = "Internal"}
elseif($objTypeCheckbox2.checked -eq $true) {$Software = "Internal&External"}
else {[void] [Windows.Forms.MessageBox]::Show("Please fill out every value!", "Error", [Windows.Forms.MessageBoxButtons]::ok, [Windows.Forms.MessageBoxIcon]::Warning)}
search_user
})
$objForm.Controls.Add($SubmitButton)
$statusBar1 = New-Object System.Windows.Forms.StatusBar
$objForm.controls.add($statusBar1)
function createFile
{
$Date = Get-Date -Format d.M.yyyy
$Time = (Get-Date).ToString(„HH:mm:ss“)
$Time2 = (Get-Date).ToString(„HH-mm-ss“)
$Date2 = Get-Date -Format yyyy-M-d;
$FileName = $Time2 + " " + $Date2
$wrapper = New-Object PSObject -Property #{ 1 = $Date; 2 = $Time; 3 = $User; 4 = $PC; 5 = $Software }
Export-Csv -InputObject $wrapper -Path C:\Users\...\$FileName.csv -NoTypeInformation
[void] [Windows.Forms.MessageBox]::Show("Successful!")
}
function search_user
{
if (#(Get-ADUser -Filter {SamAccountName -eq $User}).Count -eq 0) {
$objTextBox1.BackColor = "Red";
$statusBar1.Text = "Couldn't find user!"
$objForm.Dispose()
}
else{$objTextBox1.BackColor = "Green"
search_PC}
}
function search_PC
{
$Client = $PC
if (#(Get-ADComputer -Filter {DNSHostName -eq $Client}).Count -eq 0) {
$objTextBox2.BackColor = "Red";
$statusBar1.Text = "Couldn't find PC!"
$objForm.Dispose()
}
else{$objTextBox2.BackColor = "Green"
createFile}
}
$objForm.ShowDialog() #Showing Form and elements
Screenshot of the form:
This line is your problem:
$SubmitButton.DialogResult = "OK"
It should be:
$SubmitButton.DialogResult = [System.Windows.Forms.DialogResult]::None
Or just be removed.
DialogResult

Powershell - List value of boxes checked

I have a Powershell scripts that creates 12 unique check boxes. Later in the script, it creates an email and enter information. I am trying to get the email to include the value of the boxes that are checked, and not the others. The variable name for each box is $CB1 up through $CB12.
Team,
Per ticket $SCTicket, $User is requesting access to GetPaid.
Details from RITM ticket $RITMTicket are below:
Access Type: $AccessType
Roll: $Roll
Responsibilities:
Thank You
Doing something such as $CB1,$CB2,$CB3, ect. does not look right, as it leaves a bunch of random commas. Putting each checkbox value on it's own line can work, but then there are potentially several blank lines for no reason.
If the user checks off, say, boxes 4, 7, and 10, how can I get just those values written??
Full code is below:
if ($startupvariables) { try {Remove-Variable -Name startupvariables -Scope Global -ErrorAction SilentlyContinue } catch { } }
New-Variable -force -name startupVariables -value ( Get-Variable | ForEach-Object { $_.Name } )
Write-Output "Don't close this window!"
Add-Type -AssemblyName PresentationCore,PresentationFramework
$ButtonType = [System.Windows.MessageBoxButton]::OKCancel
$MessageIcon = [System.Windows.MessageBoxImage]::Warning
$MessageTitle = "GetPaid - Access Request"
$MessageBody = "This script sends a GetPaid access request setup to xxxxxxxx and xxxxxx.`n`nTo use it, enter the below information:`n`n`n`tTicket Number`n`n`tUser's Email Address`n`n`tAccess Requested`n`n`nIf this is the script you want to use, click OK.`nIf not, click Cancel."
$Result = [System.Windows.MessageBox]::Show($MessageBody,$MessageTitle,$ButtonType,$MessageIcon)
if ($Result -eq "Cancel")
{
Exit-PSSession
}
else
{
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null
$Separator = ".", "#"
$SCTicket = [Microsoft.VisualBasic.Interaction]::InputBox("Enter the SCTask ticket number" , "Ticket Number")
$RITMTicket = [Microsoft.VisualBasic.Interaction]::InputBox("Enter the RITM ticket number" , "Ticket Number")
$UserID = [Microsoft.VisualBasic.Interaction]::InputBox("Enter the user's email address" , "User Email Address")
$User = $UserID.split($Separator)
$Firstname = $User[0].substring(0,1).toupper()+$User[0].substring(1).tolower()
$Lastname = $User[1].substring(0,1).toupper()+$User[1].substring(1).tolower()
$User = $Firstname, $Lastname
function Access_Type{
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$Form = New-Object System.Windows.Forms.Form
$Form.width = 225
$Form.height = 215
$Form.Text = ”Select Type of Access"
$Font = New-Object System.Drawing.Font("Verdana",11)
$Form.Font = $Font
$MyGroupBox = New-Object System.Windows.Forms.GroupBox
$MyGroupBox.Location = '5,5'
$MyGroupBox.size = '190,125'
$RadioButton1 = New-Object System.Windows.Forms.RadioButton
$RadioButton1.Location = '20,20'
$RadioButton1.size = '120,30'
$RadioButton1.Checked = $false
$RadioButton1.Text = "Grant"
$RB1 = "Grant"
$RadioButton2 = New-Object System.Windows.Forms.RadioButton
$RadioButton2.Location = '20,50'
$RadioButton2.size = '120,30'
$RadioButton2.Checked = $false
$RadioButton2.Text = "Change"
$RB2 = "Change"
$RadioButton3 = New-Object System.Windows.Forms.RadioButton
$RadioButton3.Location = '20,80'
$RadioButton3.size = '120,30'
$RadioButton3.Checked = $false
$RadioButton3.Text = "Revoke"
$RB3 = "Revoke"
$OKButton = new-object System.Windows.Forms.Button
$OKButton.Location = '5,135'
$OKButton.Size = '90,35'
$OKButton.Text = 'OK'
$OKButton.DialogResult=[System.Windows.Forms.DialogResult]::OK
$CancelButton = new-object System.Windows.Forms.Button
$CancelButton.Location = '105,135'
$CancelButton.Size = '90,35'
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$CancelButton.DialogResult=[System.Windows.Forms.DialogResult]::Cancel
$form.Controls.AddRange(#($MyGroupBox,$OKButton,$CancelButton))
$MyGroupBox.Controls.AddRange(#($Radiobutton1,$RadioButton2,$RadioButton3))
$form.AcceptButton = $OKButton
$form.CancelButton = $CancelButton
$form.Add_Shown({$form.Activate()})
$dialogResult = $form.ShowDialog()
if ($DialogResult -eq "OK")
{
if ($RadioButton1.Checked){$global:AccessType = $RB1}
elseif ($RadioButton2.Checked){$global:AccessType = $RB2}
elseif ($RadioButton3.Checked){$global:AccessType = $RB3}
}
elseif ($DialogResult -eq "Cancel")
{
break
}
}
Access_Type
function Access_Roll{
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$Form = New-Object System.Windows.Forms.Form
$Form.width = 225
$Form.height = 180
$Form.Text = ”Select Type of Access"
$Font = New-Object System.Drawing.Font("Verdana",11)
$Form.Font = $Font
$MyGroupBox = New-Object System.Windows.Forms.GroupBox
$MyGroupBox.Location = '5,5'
$MyGroupBox.size = '190,90'
$RadioButton1 = New-Object System.Windows.Forms.RadioButton
$RadioButton1.Location = '20,20'
$RadioButton1.size = '130,30'
$RadioButton1.Checked = $false
$RadioButton1.Text = "User"
$RB11 = "User"
$RadioButton2 = New-Object System.Windows.Forms.RadioButton
$RadioButton2.Location = '20,50'
$RadioButton2.size = '130,30'
$RadioButton2.Checked = $false
$RadioButton2.Text = "Administrator"
$RB22 = "Administrator"
$OKButton = new-object System.Windows.Forms.Button
$OKButton.Location = '5,100'
$OKButton.Size = '90,35'
$OKButton.Text = 'OK'
$OKButton.DialogResult=[System.Windows.Forms.DialogResult]::OK
$CancelButton = new-object System.Windows.Forms.Button
$CancelButton.Location = '105,100'
$CancelButton.Size = '90,35'
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$CancelButton.DialogResult=[System.Windows.Forms.DialogResult]::Cancel
$form.Controls.AddRange(#($MyGroupBox,$OKButton,$CancelButton))
$MyGroupBox.Controls.AddRange(#($Radiobutton1,$RadioButton2))
$form.AcceptButton = $OKButton
$form.CancelButton = $CancelButton
$form.Add_Shown({$form.Activate()})
$dialogResult = $form.ShowDialog()
if ($DialogResult -eq "OK")
{
if ($RadioButton1.Checked){$global:Roll = $RB11}
elseif ($RadioButton2.Checked){$global:Roll = $RB22}
}
elseif ($DialogResult -eq "Cancel")
{
break
}
}
Access_Roll
function Access_Responsibilities{
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$Form = New-Object System.Windows.Forms.Form
$Form.width = 265
$Form.height = 510
$Form.Text = ”Select Type of Access"
$Font = New-Object System.Drawing.Font("Verdana",11)
$Form.Font = $Font
$MyGroupBox = New-Object System.Windows.Forms.GroupBox
$MyGroupBox.Location = '5,5'
$MyGroupBox.size = '230,420'
$Checkbox1 = New-Object System.Windows.Forms.Checkbox
$Checkbox1.Location = '20,20'
$Checkbox1.size = '200,25'
$Checkbox1.Checked = $false
$Checkbox1.Text = "Collections"
$CB1 = "Collections"
$Checkbox2 = New-Object System.Windows.Forms.Checkbox
$Checkbox2.Location = '20,45'
$Checkbox2.size = '200,25'
$Checkbox2.Checked = $false
$Checkbox2.Text = "Credit Management"
$CB2 = "Credit Management"
$Checkbox3 = New-Object System.Windows.Forms.Checkbox
$Checkbox3.Location = '20,70'
$Checkbox3.size = '200,25'
$Checkbox3.Checked = $false
$Checkbox3.Text = "Cash Application"
$CB3 = "Cash Application"
$Checkbox4 = New-Object System.Windows.Forms.Checkbox
$Checkbox4.Location = '20,95'
$Checkbox4.size = '200,25'
$Checkbox4.Checked = $false
$Checkbox4.Text = "Sales and Service User"
$CB4 = "Sales and Service User"
$Checkbox5 = New-Object System.Windows.Forms.Checkbox
$Checkbox5.Location = '20,115'
$Checkbox5.size = '200,50'
$Checkbox5.Checked = $false
$Checkbox5.Text = "Controller (specify plant below)"
$CB5 = "Controller (specify plant below)"
$Checkbox6 = New-Object System.Windows.Forms.Checkbox
$Checkbox6.Location = '20,160'
$Checkbox6.size = '200,50'
$Checkbox6.Checked = $false
$Checkbox6.Text = "Plant (specify plant below)"
$CB6 = "Plant (specify plant below)"
$Checkbox7 = New-Object System.Windows.Forms.Checkbox
$Checkbox7.Location = '20,205'
$Checkbox7.size = '200,25'
$Checkbox7.Checked = $false
$Checkbox7.Text = "Sales"
$CB7 = "Sales"
$Checkbox8 = New-Object System.Windows.Forms.Checkbox
$Checkbox8.Location = '20,230'
$Checkbox8.size = '200,25'
$Checkbox8.Checked = $false
$Checkbox8.Text = "Pricing"
$CB8 = "Pricing"
$Checkbox9 = New-Object System.Windows.Forms.Checkbox
$Checkbox9.Location = '20,255'
$Checkbox9.size = '200,25'
$Checkbox9.Checked = $false
$Checkbox9.Text = "Warranty"
$CB9 = "Warranty"
$Checkbox10 = New-Object System.Windows.Forms.Checkbox
$Checkbox10.Location = '20,280'
$Checkbox10.size = '200,25'
$Checkbox10.Checked = $false
$Checkbox10.Text = "GL Accountant"
$CB10 = "GL Accountant"
$Checkbox11 = New-Object System.Windows.Forms.Checkbox
$Checkbox11.Location = '20,300'
$Checkbox11.size = '200,50'
$Checkbox11.Checked = $false
$Checkbox11.Text = "Mexico Billing (specific plant below)"
$CB11 = "Mexico Billing (specific plant below)"
$Checkbox12 = New-Object System.Windows.Forms.Checkbox
$Checkbox12.Location = '20,340'
$Checkbox12.size = '200,75'
$Checkbox12.Checked = $false
$Checkbox12.Text = "Non Problem Owner (To be used for Inquiry Only)"
$CB12 = "Non Problem Owner (To be used for Inquiry Only)"
$OKButton = new-object System.Windows.Forms.Button
$OKButton.Location = '10,430'
$OKButton.Size = '90,35'
$OKButton.Text = 'OK'
$OKButton.DialogResult=[System.Windows.Forms.DialogResult]::OK
$CancelButton = new-object System.Windows.Forms.Button
$CancelButton.Location = '110,430'
$CancelButton.Size = '90,35'
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$CancelButton.DialogResult=[System.Windows.Forms.DialogResult]::Cancel
$form.Controls.AddRange(#($MyGroupBox,$OKButton,$CancelButton))
$MyGroupBox.Controls.AddRange(#($Checkbox1,$Checkbox2,$Checkbox3,$Checkbox4,$Checkbox5,$Checkbox6,$Checkbox7,$Checkbox8,$Checkbox9,$Checkbox10,$Checkbox11,$Checkbox12))
$form.AcceptButton = $OKButton
$form.CancelButton = $CancelButton
$form.Add_Shown({$form.Activate()})
$dialogResult = $form.ShowDialog()
if ($DialogResult -eq "OK")
{
if ($Checkbox1.Checked){$global:AccessResponsibilities = $CB1}
elseif ($Checkbox2.Checked){$global:AccessResponsibilities = $CB2}
elseif ($Checkbox2.Checked){$global:AccessResponsibilities = $CB3}
elseif ($Checkbox2.Checked){$global:AccessResponsibilities = $CB4}
elseif ($Checkbox2.Checked){$global:AccessResponsibilities = $CB5}
elseif ($Checkbox2.Checked){$global:AccessResponsibilities = $CB6}
elseif ($Checkbox2.Checked){$global:AccessResponsibilities = $CB7}
elseif ($Checkbox2.Checked){$global:AccessResponsibilities = $CB8}
elseif ($Checkbox2.Checked){$global:AccessResponsibilities = $CB9}
elseif ($Checkbox2.Checked){$global:AccessResponsibilities = $CB10}
elseif ($Checkbox2.Checked){$global:AccessResponsibilities = $CB11}
elseif ($Checkbox2.Checked){$global:AccessResponsibilities = $CB12}
}
elseif ($DialogResult -eq "Cancel")
{
break
}
}
Access_Responsibilities
function Read-MultiLineInputBoxDialog([string]$Message, [string]$WindowTitle, [string]$DefaultText)
{
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName System.Windows.Forms
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Size(10,10)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.AutoSize = $true
$label.Text = $Message
$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Size(10,40)
$textBox.Size = New-Object System.Drawing.Size(575,200)
$textBox.AcceptsReturn = $true
$textBox.AcceptsTab = $false
$textBox.Multiline = $true
$textBox.ScrollBars = 'Both'
$textBox.Text = $DefaultText
$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Size(415,250)
$okButton.Size = New-Object System.Drawing.Size(75,25)
$okButton.Text = "OK"
$okButton.Add_Click({ $form.Tag = $textBox.Text; $form.Close() })
$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Size(510,250)
$cancelButton.Size = New-Object System.Drawing.Size(75,25)
$cancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$cancelButton.Add_Click({ $form.Tag = $null; $form.Close() })
$form = New-Object System.Windows.Forms.Form
$form.Text = $WindowTitle
$form.Size = New-Object System.Drawing.Size(610,320)
$form.FormBorderStyle = 'FixedSingle'
$form.StartPosition = "CenterScreen"
$form.AutoSizeMode = 'GrowAndShrink'
$form.Topmost = $True
$form.AcceptButton = $okButton
$form.CancelButton = $cancelButton
$form.ShowInTaskbar = $true
$form.Controls.Add($label)
$form.Controls.Add($textBox)
$form.Controls.Add($okButton)
$form.Controls.Add($cancelButton)
$form.Add_Shown({$form.Activate()})
$form.ShowDialog() > $null
return $form.Tag
}
$global:Comments = Read-MultiLineInputBoxDialog -Message "Enter any comments from the ticket" -WindowTitle "Comments"
$Username = [System.Environment]::UserName
$subject = "Ticket $SCTicket - $User's GetPaid Access Request"
$body = "
Team,
Per ticket $SCTicket, $User is requesting access to GetPaid.
Details from RITM ticket $RITMTicket are below:
Access Type: $AccessType
Roll: $Roll
Responsibilities:
Comments: $Comments
Thank You,
"
$ButtonType = [System.Windows.MessageBoxButton]::YesNo
$MessageIcon = [System.Windows.MessageBoxImage]::Warning
$MessageTitle = "GetPaid - Access Request"
$MessageBody = "The information you have entered is show below:`n`n`nTicket Number: $Ticket`n`nUser's Email Address: $UserID`n`nAccess Type: $AccessType`n`nRoll: $Roll`n`nResponsibilities: `n`nComments: $Comments`n`n`nIf you would like to send the email, click Yes.`nOtherwise, click No."
$Result = [System.Windows.MessageBox]::Show($MessageBody,$MessageTitle,$ButtonType,$MessageIcon)
if ($Result -eq "No")
{
Exit-PSSession
}
else
{
Send-MailMessage -To "<$Username#xxx.com>" -bcc "<$Username#xxx.com>" -from "<itservicedesk#xxx.com>" -Subject $subject -SmtpServer "mailrelay.xxx.com" -body $body
}
}
Function Clean-Memory {
Get-Variable |
Where-Object { $startupVariables -notcontains $_.Name } |
ForEach-Object {
try { Remove-Variable -Name "$($_.Name)" -Force -Scope "global" -ErrorAction SilentlyContinue -WarningAction SilentlyContinue}
catch { }
}
}
Here's a procedural way to retrieve the matching values.
First, we retrieve all variables with $Checkbox in the name, then grab only ones with a CheckState value. Then, for each, we substring out the number and append that to the string CB to retrieve the value of the corresponding $CB# variable.
Then we store that in an array list and join with a comma at the end, for the output you see below.
$properties = New-object System.Collections.ArrayList
$CheckedBoxes = Get-Variable checkbox* | Where-object {$_.Value.CheckState -eq 'Checked'}
ForEach ($Checked in $CheckedBoxes){
$CBNumber = $CheckBox.Name.Split('Checkbox')[-1]
$Properties.Add((Get-variable "CB$CBNumber").Value) | out-null
}
$Properties -join ','
>GL Accountant, Pricing, Credit Management
All of this code should be used where you have this code today:
if ($Checkbox1.Checked){$global:AccessResponsibilities = $CB1}
#Insert new code here <---------------
elseif ($Checkbox2.Checked){$global:AccessResponsibilities = $CB2}
elseif ($Checkbox2.Checked){$global:AccessResponsibilities = $CB3}
Just from a preliminary look through the code and the question that you are asking, this is what I have come up with.
Basically, if the checkbox is checked, it appends the $CB(1-12) string to the $global:AccessResponsibilities and then in your email just reference $global:AccessResponsibilities and it should have all the strings of the checkboxes that were checked
if ($Checkbox1.Checked){$global:AccessResponsibilities += $CB1 + "`r`n"}
if ($Checkbox2.Checked){$global:AccessResponsibilities += $CB2 + "`r`n"}
if ($Checkbox3.Checked){$global:AccessResponsibilities += $CB3 + "`r`n"}
if ($Checkbox4.Checked){$global:AccessResponsibilities += $CB4 + "`r`n"}
if ($Checkbox5.Checked){$global:AccessResponsibilities += $CB5 + "`r`n"}
if ($Checkbox6.Checked){$global:AccessResponsibilities += $CB6 + "`r`n"}
if ($Checkbox7.Checked){$global:AccessResponsibilities += $CB7 + "`r`n"}
if ($Checkbox8.Checked){$global:AccessResponsibilities += $CB8 + "`r`n"}
if ($Checkbox9.Checked){$global:AccessResponsibilities += $CB9 + "`r`n"}
if ($Checkbox10.Checked){$global:AccessResponsibilities += $CB10 + "`r`n"}
if ($Checkbox11.Checked){$global:AccessResponsibilities += $CB11 + "`r`n"}
if ($Checkbox12.Checked){$global:AccessResponsibilities += $CB12 + "`r`n"}
heres a small loop that should work for HeedfulCrayon's answer.
foreach ($i in 1..12)
{
if ((Get-Variable -Name Checkbox$i).Value.Checked)
{
$Global:AccessResponsibilities += ((Get-Variable -Name CB$i).Value + "`r`n")
}
}

i want to create a GUI powershell script which should perform the following operation,

i want to create a GUI powershell script which should perform the following operation, if i give a name it should search in a specific list (may be in AD or in a database list) and if it found the exact name there then the name should be copied and the same name should be used to provide permission to a folder which is available in a path i mentioned.
mY CODE
function selectShare{
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
####################################################################
$Form = New-Object System.Windows.Forms.Form #
#
$Form.width = 300 # Main Window Section
$Form.height = 200 #
$Form.Text = ”File Sharing” #
#######################################################################################
#below section function of "Select an item button"
$Button = new-object System.Windows.Forms.Button
$Button.Location = new-object System.Drawing.Size(100,100)
$Button.Size = new-object System.Drawing.Size(105,25)
$Button.Text = "New Folder"
$Button.Add_Click({New-Item “h:\shan" –type directory
NET SHARE shan = h:\shan /GRANT:everyone`,FULL})
###################
################### button section
$Button1 = new-object System.Windows.Forms.Button
$Button1.Location = new-object System.Drawing.Size(100,50)
$Button1.Size = new-object System.Drawing.Size(105,25)
$Button1.Text = "Existing Folder"
$Button1.Add_Click(
{
function Find-Folders
{
[Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
[System.Windows.Forms.Application]::EnableVisualStyles()
$FolderBrowser = New-Object System.Windows.Forms.FolderBrowserDialog
$browse = New-Object System.Windows.Forms.FolderBrowserDialog
$browse.SelectedPath = "C:\"
$browse.ShowNewFolderButton = $true
$browse.Description = "Select a Folder"
$loop = $true
while($loop)
{
if ($browse.ShowDialog() -eq "OK")
{
$loop = $false
$FolderBrowser.SelectedPath
#Insert your script here
} else
{
$res = [System.Windows.Forms.MessageBox]::Show("You clicked Cancel. Would you like to try again or exit?", "Select a location", [System.Windows.Forms.MessageBoxButtons]::RetryCancel)
if($res -eq "Cancel")
{
#Ends script
return
}
}
}
$browse.SelectedPath
$browse.Dispose()
} Find-Folders})
$form.Controls.Add($Button)
$form.Controls.Add($Button1)
$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()
return $script:choice
}
$share = selectShare
write-host $share