powershell gui creating local users - powershell

i need some help with my powershell gui. i want to create a gui for creating local useraccounts with some parameters provided in the checkbox i need to click. the gui is running but i need some help for the code lines for the checkboxes. what do i need to add that the code is running fone when i click the checkboxes? for example if i just click checkbox 1 i get an error the username is to long even i name the testaccount "testuser"
$PSDefaultParameterValues['*:Encoding'] = 'ascii'
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
#create form
$form = New-Object System.Windows.Forms.Form
$form.Width = 500
$form.Height = 400
$form.MaximizeBox = $false
$form.TopMost = $true
$objLabel = New-Object System.Windows.Forms.label
$objLabel.Location = New-Object System.Drawing.Size(10,20)
$objLabel.Size = New-Object System.Drawing.Size(130,15)
$objLabel.BackColor = "Transparent"
$objLabel.ForeColor = "Black"
$objLabel.Text = "name"
$Form.Controls.Add($objLabel)
#textbox with choosen user name
$txtBox = New-Object System.Windows.Forms.TextBox
$txtBox.Location = New-Object System.Drawing.Point (180, 20)
$txtBox.Size = New-Object System.Drawing.Size(280,100)
$form.Controls.Add($txtBox)
$objLabel2 = New-Object System.Windows.Forms.label
$objLabel2.Location = New-Object System.Drawing.Size(10,50)
$objLabel2.Size = New-Object System.Drawing.Size(130,15)
$objLabel2.BackColor = "Transparent"
$objLabel2.ForeColor = "Black"
$objLabel2.Text = "password"
$Form.Controls.Add($objLabel2)
#textbox with choosen password
$txtBox2 = New-Object System.Windows.Forms.TextBox
$txtBox2.Location = New-Object System.Drawing.Point (180, 50)
$txtBox2.Size = New-Object System.Drawing.Size(280,100)
$form.Controls.Add($txtBox2)
#create checkbox1
$checkBox = New-Object System.Windows.Forms.CheckBox
$checkBox.Location = New-Object System.Drawing.Point (10, 100)
$checkBox.Size = New-Object System.Drawing.Size(350,30)
$checkBox.Text = "PasswordNeverExpires"
$form.Controls.Add($checkBox)
#create checkbox2
$checkBox2 = New-Object System.Windows.Forms.CheckBox
$checkBox2.Location = New-Object System.Drawing.Point (10, 150)
$checkBox2.Size = New-Object System.Drawing.Size(350,30)
$checkBox2.Text = "UserMayChangePassword"
$form.Controls.Add($checkBox2)
#create checkbox2
$checkBox3 = New-Object System.Windows.Forms.CheckBox
$checkBox3.Location = New-Object System.Drawing.Point (10, 200)
$checkBox3.Size = New-Object System.Drawing.Size(350,30)
$checkBox3.Text = "AccountNeverExpires"
$form.Controls.Add($checkBox3)
#create user button
$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(10,250)
$Button.Size = New-Object System.Drawing.Size(150,50)
$Button.Text = "create user"
$Button.Add_Click({
if(($checkBox.Checked -eq $false) -and ($checkBox2.Checked -eq $false) -and ($checkBox3.Checked -eq $false)) {
[System.Windows.Forms.Messagebox]::Show("No CheckBox checked")
}
#checkbox1 action
if ($checkBox.Checked -eq $true) {
$adminName = $txtBox
$securePassword = $txtBox2
$newUser = New-LocalUser -Name $adminName -Password $securePassword -Description $adminName -FullName $adminName #-ErrorAction Stop
$newUser | Set-LocalUser <#-PasswordNeverExpires $true#> -UserMayChangePassword $false <#-AccountNeverExpires#> #-ErrorAction Stop
Add-LocalGroupMember -Group "Administrators" -Member $adminName #-ErrorAction Stop
if(-not $?) {[System.Windows.Forms.MessageBox]::Show( "no success",'','OK',"Error")}
else {[System.Windows.Forms.MessageBox]::Show( "success",'','OK',"Information")}
}
#checkbox2 action
if ($checkBox2.Checked -eq $true) {
if(-not $?) {[System.Windows.Forms.MessageBox]::Show( "no success",'','OK',"Error")}
else {[System.Windows.Forms.MessageBox]::Show( "success",'','OK',"Information")}
}
#checkbox3 action
if ($checkBox3.Checked -eq $true) {
if(-not $?) {[System.Windows.Forms.MessageBox]::Show( "no success",'','OK',"Error")}
else {[System.Windows.Forms.MessageBox]::Show( "success",'','OK',"Information")}
}
})
$form.Controls.Add($Button2)
#end
[void]$form.ShowDialog()

Related

How to remove checked margin in tray icon context menu

I've created this little script running in tray. I've noticed that contex menu text is not placed completely to the left. After a little investigation I found out that it is because there is a space left for .checked state indicator. I found out that it can be removed by .showcheckedmargin property of ContextMenuStrip but have no idea how to implement that.
Please advise.
add-type -AssemblyName 'System.Windows.Forms'
Add-Type -AssemblyName 'System.Drawing'
Add-Type -AssemblyName 'PresentationFramework'
$2 = "BASE64 CODE"
$bitmap = New-Object System.Windows.Media.Imaging.BitmapImage
$bitmap.BeginInit()
$bitmap.StreamSource = [System.IO.MemoryStream][System.Convert]::FromBase64String($2)
$bitmap.EndInit()
$bitmap.Freeze()
$image = [System.Drawing.Bitmap][System.Drawing.Image]::FromStream($bitmap.StreamSource)
$icon = [System.Drawing.Icon]::FromHandle($image.GetHicon())
$app = New-Object System.Windows.Forms.NotifyIcon
$app.Text = ""
$app.Icon = $icon
$app.Visible = $true
$Zenek = New-Object System.Windows.Forms.MenuItem
$Zenek.Text = "Zenek"
$NetExt = New-Object System.Windows.Forms.MenuItem
$NetExt.Text = "NetExt"
$Busy = New-Object System.Windows.Forms.MenuItem
$Busy.Text = "BUSY"
$Time = New-Object System.Windows.Forms.MenuItem
$Time.Text = "Time"
$Exit = New-object System.Windows.Forms.MenuItem
$Exit.Text = "Exit"
$context = New-Object System.Windows.Forms.ContextMenu
$app.ContextMenu = $context
$app.ContextMenu.MenuItems.AddRange(#($Zenek, $NetExt, $Busy, $Time, $Exit))
$keepAwakeScript = {
while (1) {
$wsh = New-Object -ComObject WScript.Shell
$wsh.SendKeys('+{F15}')
Start-Sleep -seconds 59
}
}
Start-Job -ScriptBlock $keepAwakeScript -Name "keepAwake"|out-null
$Zenek.Add_Click({Set-Clipboard -Value "SOME TEXT"})
$NetExt.Add_Click({Set-Clipboard -Value "SOME OTHER TEXT"})
$Busy.Add_Click({
$running = Get-Job|?{$_.Name -like 'KAPS'}
$icon = new-object System.Drawing.Icon("MY PRIVATE LOCATION\red.ico")
$app.Icon = $icon
if($running -eq $null){
Start-Job -Name KAPS -ScriptBlock{
while ($true){
$shell = New-Object -ComObject WScript.Shell
$shell.SendKeys('{CAPSLOCK}')
Start-Sleep 1
}
}
}else{
Stop-Job KAPS
Remove-Job KAPS
$icon = [System.Drawing.Icon]::FromHandle($image.GetHicon())
$app.Icon = $icon
}
})
$Time.Add_Click({
$main = New-Object System.Windows.Forms.Form
$main.ClientSize = '200,200'
$main.MinimizeBox = $false
$main.MaximizeBox = $false
$label = New-Object System.Windows.Forms.Label
$label.Size = '190,30'
$label.Location = '10,10'
$label.Text = 'Ile minut?'
$font = New-Object System.Drawing.Font("Arial", 23)
$label.Font = $font
$textbox = New-Object System.Windows.Forms.TextBox
$textbox.Size = '50,100'
$textbox.Location = '10,70'
$button = New-Object System.Windows.Forms.Button
$button.Size = '50,50'
$button.Location = '10,120'
$button.Text = 'GO'
$button.Add_Click({
$timer.Start()
$textbox.Visible =$false
$button.Visible = $false
$label.Visible = $false
$script:1 = New-TimeSpan -minutes ([int]$textbox.Text)
$label2.Visible = $true
$label2.Text = $1.ToString("mm\:ss")
})
$timer = New-Object System.Windows.Forms.Timer
$timer.Enabled = $false
$timer.Interval = '1000'
$second = New-TimeSpan -Seconds 1
$timer.Add_Tick({
$script:1 = $1.Subtract($second)
$label2.Text = $1.ToString("mm\:ss")
if($script:1.TotalSeconds -le 0){$timer.Stop(); $timer.Dispose(); [System.Windows.Forms.Application]::SetSuspendState("Hibernate", $true, $false); $main.Close(); $main.Dispose()}
})
$label2 = New-Object System.Windows.Forms.Label
$label2.Size = '190,30'
$label2.Location = '10,10'
$label2.Text = $1
$font = New-Object System.Drawing.Font("Arial", 23)
$label2.Font = $font
$label2.Visible = $false
$main.Controls.Add($textbox)
$main.Controls.Add($button)
$main.Controls.Add($label)
$main.Controls.Add($label2)
$main.ShowDialog()
})
$Exit.Add_Click({
$app.Visible = $false
Stop-Job -Name "keepAwake"
$appContext.ExitThread()
Stop-Process -Id $PID
})
$appContext = New-Object System.Windows.Forms.ApplicationContext
[void][System.Windows.Forms.Application]::Run($appContext)
For anyone with the same problem. I've managed to figure it out.
Another post from SO allowed me to do so.
I had to replace .ContextMenu with .ContextMenuStrip for the Menu itself,
replace .MenuItem with .ToolStripMenuItem, add .ShowImageMargin to ContextMenuStrip variable, then finall add items via .Items.AddRange(#()).
Before
After
In the end the code looks like this and works as intended.
add-type -AssemblyName 'System.Windows.Forms'
Add-Type -AssemblyName 'System.Drawing'
Add-Type -AssemblyName 'PresentationFramework'
$2 =
"BASE 64 CODE"
$bitmap = New-Object System.Windows.Media.Imaging.BitmapImage
$bitmap.BeginInit()
$bitmap.StreamSource = [System.IO.MemoryStream][System.Convert]::FromBase64String($2)
$bitmap.EndInit()
$bitmap.Freeze()
$image = [System.Drawing.Bitmap][System.Drawing.Image]::FromStream($bitmap.StreamSource)
$icon = [System.Drawing.Icon]::FromHandle($image.GetHicon())
$app = New-Object System.Windows.Forms.NotifyIcon
$app.Text = ""
$app.Icon = $icon
$app.Visible = $true
$Zenek = New-Object System.Windows.Forms.ToolStripMenuItem
$Zenek.Text = "Zenek"
$Zenek.Checked = $true
$NetExt = New-Object System.Windows.Forms.ToolStripMenuItem
$NetExt.Text = "NetExt"
$Busy = New-Object System.Windows.Forms.ToolStripMenuItem
$Busy.Text = "BUSY"
$Time = New-Object System.Windows.Forms.ToolStripMenuItem
$Time.Text = "Time"
$Exit = New-object System.Windows.Forms.ToolStripMenuItem
$Exit.Text = "Exit"
$sep = New-Object System.Windows.Forms.ToolStripSeparator
$context = New-Object System.Windows.Forms.ContextMenuStrip
$context.ShowImageMargin = $false
$context.Items.AddRange(#($Zenek, $NetExt, $Busy, $Time, $sep, $Exit))
$app.ContextMenuStrip = $context
$keepAwakeScript = {
while (1) {
$wsh = New-Object -ComObject WScript.Shell
$wsh.SendKeys('+{F15}')
Start-Sleep -seconds 59
}
}
Start-Job -ScriptBlock $keepAwakeScript -Name "keepAwake"|out-null
$Zenek.Add_Click({Set-Clipboard -Value "SOME TEXT"})
$NetExt.Add_Click({Set-Clipboard -Value "SOME OTHER TEXT"})
$Busy.Add_Click({
$running = Get-Job|?{$_.Name -like 'KAPS'}
$icon = new-object System.Drawing.Icon("MY PRIVATE LOCATION\red.ico")
$app.Icon = $icon
if($running -eq $null){
Start-Job -Name KAPS -ScriptBlock{
while ($true){
$shell = New-Object -ComObject WScript.Shell
$shell.SendKeys('{CAPSLOCK}')
Start-Sleep 1
}
}
}else{
Stop-Job KAPS
Remove-Job KAPS
$icon = [System.Drawing.Icon]::FromHandle($image.GetHicon())
$app.Icon = $icon
}
})
$Time.Add_Click({
$main = New-Object System.Windows.Forms.Form
$main.ClientSize = '200,200'
$main.MinimizeBox = $false
$main.MaximizeBox = $false
$label = New-Object System.Windows.Forms.Label
$label.Size = '190,30'
$label.Location = '10,10'
$label.Text = 'Ile minut?'
$font = New-Object System.Drawing.Font("Arial", 23)
$label.Font = $font
$textbox = New-Object System.Windows.Forms.TextBox
$textbox.Size = '50,100'
$textbox.Location = '10,70'
$button = New-Object System.Windows.Forms.Button
$button.Size = '50,50'
$button.Location = '10,120'
$button.Text = 'GO'
$button.Add_Click({
$timer.Start()
$textbox.Visible =$false
$button.Visible = $false
$label.Visible = $false
$script:1 = New-TimeSpan -minutes ([int]$textbox.Text)
$label2.Visible = $true
$label2.Text = $1.ToString("mm\:ss")
})
$timer = New-Object System.Windows.Forms.Timer
$timer.Enabled = $false
$timer.Interval = '1000'
$second = New-TimeSpan -Seconds 1
$timer.Add_Tick({
$script:1 = $1.Subtract($second)
$label2.Text = $1.ToString("mm\:ss")
if($script:1.TotalSeconds -le 0){$timer.Stop(); $timer.Dispose(); [System.Windows.Forms.Application]::SetSuspendState("Hibernate", $true, $false); $main.Close(); $main.Dispose()}
})
$label2 = New-Object System.Windows.Forms.Label
$label2.Size = '190,30'
$label2.Location = '10,10'
$label2.Text = $1
$font = New-Object System.Drawing.Font("Arial", 23)
$label2.Font = $font
$label2.Visible = $false
$main.Controls.Add($textbox)
$main.Controls.Add($button)
$main.Controls.Add($label)
$main.Controls.Add($label2)
$main.ShowDialog()
})
$Exit.Add_Click({
$app.Visible = $false
Stop-Job -Name "keepAwake"
$appContext.ExitThread()
#Stop-Process -Id $PID
})
$appContext = New-Object System.Windows.Forms.ApplicationContext
[void][System.Windows.Forms.Application]::Run($appContext)

Need help gui background-job double-hop issue and script freezing

I'm having an issue with double hopping scriptblock for background job that either does nothing or freezes the script. Explenation below.
The idea for the script is to get a list of files (very large count, ~200k) and then process them in three individual ways (get hashcodes and get true duplicates, compare filenames and get pseudo duplicates, get zero length files). At this moment I'm working on hashes. The result of $job1 is a string (array?) that cannot be piped to get-filehash. This results in $job2 looping through each string in order to get hashes. As background-jobs work on scriptblocks and foreach loop has scriptblock component a double hop is created. I've worked around that by using invoke-command combined with [scriptblock]::Create() method inside job scriptblock. That unfortunately freezes the form. My question to you is as follows. How do I force $job1 result to be of system.io.fileinfo type OR how do I unfreeze script while Invoke-command is in progress?
Add-Type -AssemblyName System.Windows.Forms
add-type -AssemblyName system.drawing
[System.Windows.Forms.Application]::EnableVisualStyles()
$path = "C:\"
$window = New-Object system.Windows.Forms.Form
$window.Size = New-Object system.Drawing.Size #(400,400)
$window.StartPosition = "CenterScreen"
$window.Font = New-Object System.Drawing.Font("Calibri",11,[System.Drawing.FontStyle]::Bold)
$window.Text = "STARTING UP"
$ProgressBar1 = New-Object System.Windows.Forms.ProgressBar
$ProgressBar1.Location = New-Object System.Drawing.Point(10, 10)
$ProgressBar1.Size = New-Object System.Drawing.Size(365, 20)
$ProgressBar1.Style = "Marquee"
$ProgressBar1.MarqueeAnimationSpeed = 20
$ProgressBar1.UseWaitCursor = $true
$ProgressBar1.Visible = $false
$button = New-Object System.Windows.Forms.Button
$button.size = New-Object system.drawing.size #(50,50)
$button.Location = New-Object System.Drawing.Point(20, 70)
$button.Text = "TEST"
$window.Controls.add($button)
$label = New-Object System.Windows.Forms.Label
$label.Size = New-Object System.Drawing.Size #(100, 50)
$label.Location = New-Object System.Drawing.Point (80, 70)
$label.BorderStyle = 'Fixed3D'
$label.ForeColor = 'green'
$label.TextAlign = 'middlecenter'
$window.Controls.Add($label)
$timer = New-Object System.Windows.Forms.Timer
$timer.Interval = 1000
$timer.add_Tick({$script:time2 =((get-date)-$script:time1).ToString("hh\:mm\:ss")
$label2.text = $script:time2
})
$button.add_Click(
{$1 = Get-Date
$Script:time1 = Get-Date
$timer.start()
$ProgressBar1.BringToFront()
$ProgressBar1.Show()
$this.Enabled = $false
$job = Start-Job -ScriptBlock {
Get-ChildItem -File -recurse $HOME -ErrorAction SilentlyContinue|select -ExpandProperty FullName
}
while($job.State -eq 'Running') {
[System.Windows.Forms.Application]::DoEvents()
}
$script:asd = $job | Receive-Job #-AutoRemoveJob -Wait
$ProgressBar1.Hide()
$this.Enabled = $true
$2 = ((get-date) - $1).ToString("hh\:mm\:ss")
$label.text = $2
$timer.stop()
}
)
$button2 = New-Object System.Windows.Forms.Button
$button2.Size = New-Object System.Drawing.Size #(50, 50)
$button2.Location = New-Object System.Drawing.Point (200, 70)
$button2.Text = 'Exit'
$window.Controls.Add($button2)
$button2.add_Click({$window.Close()})
$button3 = new-object System.Windows.Forms.Button
$button3.Size = New-Object System.Drawing.Size #(50, 50)
$button3.Location = New-Object System.Drawing.Point (20, 200)
$button3.Text = "Start"
$window.Controls.Add($button3)
$button3.add_Click({
$sb = foreach ($qwe in $Script:asd){Get-FileHash $qwe -Algorithm MD5|select -ExpandProperty Hash -OutVariable +Script:tester}
$ProgressBar2.BringToFront()
$ProgressBar2.Show()
$this.Enabled = $false
$job2 = start-job -Name 'asd' -scriptblock {
param($sb)
$sb = [scriptblock]::Create($sb)
Invoke-command -ScriptBlock $sb
}
while($job2.State -eq 'Running') {
[System.Windows.Forms.Application]::DoEvents()
}
Write-Host $Script:tester
$ProgressBar2.Hide()
$this.Enabled = $true
})
$label2 = New-Object System.Windows.Forms.Label
$label2.size = New-Object System.Drawing.Size #(100, 50)
$label2.Location = New-Object System.Drawing.Point (80, 200)
$label2.BorderStyle = 'Fixed3D'
$label2.ForeColor = 'green'
$label2.TextAlign = 'middlecenter'
$window.Controls.Add($label2)
$ProgressBar2 = New-Object System.Windows.Forms.ProgressBar
$ProgressBar2.Location = New-Object System.Drawing.Point(10, 330)
$ProgressBar2.Size = New-Object System.Drawing.Size(365, 20)
$ProgressBar2.Style = "Marquee"
$ProgressBar2.MarqueeAnimationSpeed = 20
$ProgressBar2.UseWaitCursor = $true
$ProgressBar2.Visible = $false
$window.Controls.Add($ProgressBar2)
$groupbox = New-Object System.Windows.Forms.GroupBox
$groupbox.size = New-Object system.drawing.size #(377, 365)
$groupbox.Location = New-Object System.Drawing.point (4, -5)
$window.Controls.Add($groupbox)
$window.Controls.Add($ProgressBar1)
$window.ShowDialog()|out-null
For anyone interested. The answer was piping $job results to set-variable - $job|Wait-job|Receive-Job|Set-Variable -Name JOB_RESULT -Scope Script.
Then using $using: scope in $job2 scriptblock - $job2 = start-job -scriptblock { $using:JOB_RESULT|Get-FileHash -Algorithm MD5}

Powershell GUI script: read variable from textbox

I'm trying to pass the input of the two textboxes to the logic part of the script at the bottom. But that's not happening and I can't figure out the problem... I tried rewriting the code from [here][1].
Add-Type -AssemblyName System.Drawing
$form = New-Object System.Windows.Forms.Form
$form.Text = 'CompareGroups'
$form.Size = New-Object System.Drawing.Size(300,250)
$form.StartPosition = 'CenterScreen'
$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(75,170)
$okButton.Size = New-Object System.Drawing.Size(75,23)
$okButton.Text = 'OK'
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $okButton
$form.Controls.Add($okButton)
$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Point(150,170)
$cancelButton.Size = New-Object System.Drawing.Size(75,23)
$cancelButton.Text = 'Cancel'
$cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $cancelButton
$form.Controls.Add($cancelButton)
$label1 = New-Object System.Windows.Forms.Label
$label1.Location = New-Object System.Drawing.Point(10,20)
$label1.Size = New-Object System.Drawing.Size(280,20)
$label1.Text = 'Source-PC:'
$form.Controls.Add($label1)
$label2 = New-Object System.Windows.Forms.Label
$label2.Location = New-Object System.Drawing.Point(10,80)
$label2.Size = New-Object System.Drawing.Size(280,20)
$label2.Text = 'Target-PC:'
$form.Controls.Add($label2)
$textBox1 = New-Object System.Windows.Forms.TextBox
$textBox1.Location = New-Object System.Drawing.Point(10,40)
$textBox1.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBox1)
$textBox2 = New-Object System.Windows.Forms.TextBox
$textBox2.Location = New-Object System.Drawing.Point(10,100)
$textBox2.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBox2)
$form.Topmost = $true
$form.Add_Shown({$textBox1.Select()})
$result = $form.ShowDialog()
if (($result -eq [System.Windows.Forms.DialogResult]::OK)) {
$sourcePc = $textBox.Text
$targetPc = $textBox2.Text
$CopyFromPC = Get-ADComputer $sourcePc -Properties MemberOf
$CopyToPC = Get-ADComputer $targetPc -Properties MemberOf
if (($CopyFromPC.MemberOf | out-string) -eq ($CopyTopc.MemberOf | out-string)) {
Write-Host "Groups are the same."
} else {
$CopyFromPC.MemberOf | Where {$CopyTopc.MemberOf -notcontains $_} | Out-GridView -PassThru | Add-ADGroupMember -Members $CopyTopc -verbose
[System.Windows.Forms.MessageBox]::Show("Groups got copied successfully","CompareGroups",0)
}
}
[1]: https://learn.microsoft.com/en-us/powershell/scripting/samples/creating-a-custom-input-box?view=powershell-7
You added a textbox called textbox1 and not textbox.
Change the variablename from $sourcePc = $textBox.Text to $sourcePc = $textBox1.Text

ComboBox.SelectedItem is null

I populated a combobox with AD SamAccountName items. When selecting one of the items, one can push a button in order to retrieve information from that account. However, when clicking the button I receive the below error:
Cannot validate argument on parameter 'Identity'. The argument is null. Provide a valid value for the argument, and then try running the command again.
The command the error refers to is:
$Label_User_ItemContent.Text = (Get-ADUser -Identity $ComboBox.SelectedItem).SamAccountName
The crucial part of the code is:
$ComboBox = New-Object -TypeName System.Windows.Forms.ComboBox
$ComboBox.Width = 300
$ComboBox.Location = New-Object -TypeName System.Drawing.Point(250, 25)
$Users = Get-ADUser -Filter * | Where-Object {$_.SamAccountName -match '^adminA'}
ForEach ($User in $Users){
$ComboBox.Items.Add($User.SamAccountName)
}
$ComboBox.SelectedIndex = 1
$MainWindow.Controls.Add($ComboBox)
$Button_Check_TEST = New-Object -TypeName System.Windows.Forms.Button
$Button_Check_TEST.Location = New-Object -TypeName System.Drawing.Size(350, 150)
$Button_Check_TEST.Size = New-Object -TypeName System.Drawing.Size(150, 50)
$Button_Check_TEST.Text = 'Check'
$MainWindow.Controls.Add($Button_Check_TEST)
$Button_Check_TEST.Add_Click({
Try{
$Label_User_ItemContent.Text = (Get-ADUser -Identity $ComboBox.SelectedItem).SamAccountName
}
Catch{
Write-Verbose -Verbose $_.Exception.Message
}
})
The problem is, that I need two layers. Basically, there should be a menu with four different options, one of them is 'User'. When clicking on 'User' the ComboBox and the 'Click'-Button shall appear.
Using the code above without the 'User'-Button works fine.
Question: Why does the ComboBox.SelectedItem not work when I use a button to 'create' the ComboBox?
The complete code is below:
$font = New-Object -TypeName System.Drawing.Font("Times New Roman", 18, [System.Drawing.FontStyle]::Bold)
$MainWindow = New-Object -TypeName System.Windows.Forms.Form
$MainWindow.Text = 'PIM v10 Administrator Window'
$MainWindow.Width = 600
$MainWindow.Height = 555
$MainWindow.AutoSize = $true
$Button_User = New-Object -TypeName System.Windows.Forms.Button
$Button_User.Location = New-Object -TypeName System.Drawing.Size(25, 25)
$Button_User.Size = New-Object -TypeName System.Drawing.Size(200, 75)
$Button_User.Text = 'User'
$Button_User.Font = $font
$MainWindow.Controls.Add($Button_User)
$Button_User.Add_Click({
$Label_User = New-Object -TypeName System.Windows.Forms.Label
$Label_User.Text = 'Given Name:'
$Label_User.Location = New-Object -TypeName System.Drawing.Point(250, 50)
$Label_User.AutoSize = $true
$MainWindow.Controls.Add($Label_User)
$Label_User_ItemContent = New-Object -TypeName System.Windows.Forms.Label
$Label_User_ItemContent.Text = ''
$Label_User_ItemContent.Location = New-Object -TypeName System.Drawing.Point(250, 100)
$MainWindow.Controls.Add($Label_User_ItemContent)
$ComboBox = New-Object -TypeName System.Windows.Forms.ComboBox
$ComboBox.Width = 300
$ComboBox.Location = New-Object -TypeName System.Drawing.Point(250, 25)
$Users = Get-ADUser -Filter * | Where-Object {$_.SamAccountName -match '^adminA'}
ForEach ($User in $Users){
$ComboBox.Items.Add($User.SamAccountName)
}
$ComboBox.SelectedIndex = 1
$MainWindow.Controls.Add($ComboBox)
$Button_Check_TEST = New-Object -TypeName System.Windows.Forms.Button
$Button_Check_TEST.Location = New-Object -TypeName System.Drawing.Size(350, 150)
$Button_Check_TEST.Size = New-Object -TypeName System.Drawing.Size(150, 50)
$Button_Check_TEST.Text = 'Check'
$MainWindow.Controls.Add($Button_Check_TEST)
$Button_Check_TEST.Add_Click({
Try{
$Label_User_ItemContent.Text = (Get-ADUser -Identity $ComboBox.SelectedItem).SamAccountName
}
Catch{
Write-Verbose -Verbose $_.Exception.Message
}
})
if (-not ($ComboBox.SelectedItem -eq $null)){
$Label_User_ItemContent.Text = (Get-ADUser -Identity $ComboBox.SelectedItem).SamAccountName
}
else {
Write-Host -Object "Object is null"
}
})
$MainWindow.ShowDialog()
So whats happening is you are creating variables in the wrong scope
$Button_User.Add_Click({
$ComboBox = New-Object -TypeName System.Windows.Forms.ComboBox
$Label_User_ItemContent = New-Object -TypeName System.Windows.Forms.Label
$MainWindow.Controls.Add($ComboBox)
$Label_User_ItemContent = New-Object -TypeName System.Windows.Forms.Label
$Button_Check_TEST.Add_Click({
$Label_User_ItemContent.Text = (Get-ADUser -Identity
$ComboBox.SelectedItem).SamAccountName
)}
})
Since you are creating the Combo and the Lable inside a Add_click action. Those Values only exist when the action is taken and in the $MainWindows.Controls. The items are then cleared from memory
When you run the next action $Button_Check_TEST.Add_Click() since the variables are cleared then $ComboBox and $Label_User_ItemContent equal nothing.
A fix would be to place them outside the $Button_User.Add_Click() event
$ComboBox = New-Object -TypeName System.Windows.Forms.ComboBox
$Label_User_ItemContent = New-Object -TypeName System.Windows.Forms.Label
$Button_User.Add_Click({
$MainWindow.Controls.Add($ComboBox)
$Label_User_ItemContent = New-Object -TypeName System.Windows.Forms.Label
$Button_Check_TEST.Add_Click({
$Label_User_ItemContent.Text = (Get-ADUser -Identity
$ComboBox.SelectedItem).SamAccountName
)}
})
Here is the whole script in working condition now
$font = New-Object -TypeName System.Drawing.Font("Times New Roman", 18, [System.Drawing.FontStyle]::Bold)
$MainWindow = New-Object -TypeName System.Windows.Forms.Form
$MainWindow.Text = 'PIM v10 Administrator Window'
$MainWindow.Width = 600
$MainWindow.Height = 555
$MainWindow.AutoSize = $true
$Button_User = New-Object -TypeName System.Windows.Forms.Button
$Button_User.Location = New-Object -TypeName System.Drawing.Size(25, 25)
$Button_User.Size = New-Object -TypeName System.Drawing.Size(200, 75)
$Button_User.Text = 'User'
$Button_User.Font = $font
$MainWindow.Controls.Add($Button_User)
$ComboBox = New-Object -TypeName System.Windows.Forms.ComboBox
$Label_User_ItemContent = New-Object -TypeName System.Windows.Forms.Label
$Button_User.Add_Click({
$Label_User = New-Object -TypeName System.Windows.Forms.Label
$Label_User.Text = 'Given Name:'
$Label_User.Location = New-Object -TypeName System.Drawing.Point(250, 50)
$Label_User.AutoSize = $true
$MainWindow.Controls.Add($Label_User)
$Label_User_ItemContent.Text = ''
$Label_User_ItemContent.Location = New-Object -TypeName System.Drawing.Point(250, 100)
$MainWindow.Controls.Add($Label_User_ItemContent)
$ComboBox.Width = 300
$ComboBox.Location = New-Object -TypeName System.Drawing.Point(250, 25)
$Users = Get-ADUser -Filter * | Where-Object {$_.SamAccountName -match '^adminA'}
$MainWindow.Controls.Add($ComboBox)
ForEach ($User in $Users){
$ComboBox.Items.Add($User.SamAccountName)
}
$MainWindow.Controls.Add($ComboBox)
$ComboBox.SelectedIndex = 0
$Button_Check_TEST = New-Object -TypeName System.Windows.Forms.Button
$Button_Check_TEST.Location = New-Object -TypeName System.Drawing.Size(350, 150)
$Button_Check_TEST.Size = New-Object -TypeName System.Drawing.Size(150, 50)
$Button_Check_TEST.Text = 'Check'
$MainWindow.Controls.Add($Button_Check_TEST)
$Button_Check_TEST.Add_Click({
Try{
$Label_User_ItemContent.Text = (Get-ADUser -Identity $ComboBox.SelectedItem).SamAccountName
}
Catch{
Write-Verbose -Verbose $_.Exception.Message
}
})
if (-not ($ComboBox.SelectedItem -eq $null)){
$Label_User_ItemContent.Text = (Get-ADUser -Identity $ComboBox.SelectedItem).SamAccountName
}
else {
Write-Host -Object "Object is null"
}
})
$MainWindow.ShowDialog()

Use Powershell to simulate right-clicking an Active Directory Group's Properties

Just for clarification, I want the GUI to appear, like the actual window. This is for a side-project I've been trying to work on for a while now the goal was to make a faster way to link a group of users and folder. Since this is a process focusing on saving time, I wanted to have this method of going straight to the properties without having to actively search out the folder.
Ask if more info is needed
Code can be provided (Not sure why you'd need it Id prefer not, but I can be arranged)
I'd REALLY prefer to not use any kind of outside sources or installs. The main goal was to have this powershell script capable of just being put on any ol' computer and have it work. (Not really any old computer but you get my point)
I looked into RUNDLL32 dsquery,OpenQueryWindow as an option, but it didn't give me what I needed.
TL:DR - Pretty much what the title says. I want to be able to run this command and have a specific Security Group's Properties Window appear as if I was in Active Directory myself right clicking on properties.
#######Paths#########
param (
$MYDRIVE_PATH = "\\------\KSD\",
$LOG_FILE_PATH = "C:\Users\Documents\Test_Docs\",
$UserListFile = "C:\Users\Documents\Test_Docs\brenda.txt"
)
##########################
Add-PSSnapin Quest.ActiveRoles.ADManagement
#import-module activedirectory
$LogFile = $LOG_FILE_PATH + "\CreateMyDriveFolderAndPermissions_" + (Get-Date -Format "MM-d-y.h.m.s.ms") + ".log"
$groupRights = [System.Security.AccessControl.FileSystemRights]"ListDirectory, ReadData, WriteData, CreateFiles, CreateDirectories, AppendData, ReadExtendedAttributes, WriteExtendedAttributes, Traverse, ExecuteFile, DeleteSubdirectoriesAndFiles, ReadAttributes, WriteAttributes, Write, ReadPermissions, Read, ReadAndExecute, Synchronize"
$InheritanceFlag = ([System.Security.AccessControl.InheritanceFlags]::ContainerInherit -bor [System.Security.AccessControl.InheritanceFlags]::ObjectInherit)
$PropagationFlag = [System.Security.AccessControl.PropagationFlags]::None
$ErrorCount = 0
$DestinationCheck = 1
$FolderNameCheck = 1
####################################### Option Box #######################################
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Options"
$objForm.Size = New-Object System.Drawing.Size(300,200)
$objForm.StartPosition = "CenterScreen"
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
{$x=$objTextBox.Text;$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
{$objForm.Close()}})
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(75,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click({$x=$objTextBox.Text;$objForm.Close()})
$objForm.Controls.Add($OKButton)
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(150,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close();})
$objForm.Controls.Add($CancelButton)
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,20)
$objLabel.Size = New-Object System.Drawing.Size(280,20)
$objLabel.Text = "Options:"
$objForm.Controls.Add($objLabel)
$objBrenda = New-Object System.Windows.Forms.checkbox
$objBrenda.Location = New-Object System.Drawing.Size(10,50)
$objBrenda.Size = New-Object System.Drawing.Size(200,20)
$objBrenda.Checked = $false
$objBrenda.Text = "Brenda Fernandez"
$objForm.Controls.Add($objBrenda)
$objForm.Topmost = $True
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
##############################################################################################
####################################### Destination Box #######################################
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
##### Main Box Creation #####
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "New Folder Destination"
$objForm.Size = New-Object System.Drawing.Size(300,200)
$objForm.StartPosition = "CenterScreen"
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
{$x=$objListBox.SelectedItem;$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
{$objForm.Close()}})
###################
##### "Ok" Button Creation #####
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(75,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click({$x=$objListBox.SelectedItem;$objForm.Close();$DestinationCheck = 0})
$objForm.Controls.Add($OKButton)
###################
##### "Cancel" Button Creation #####
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(150,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close();[System.Environment]::Exit(0)})
$objForm.Controls.Add($CancelButton)
###################
##### Message Right Above Selection Box #####
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,20)
$objLabel.Size = New-Object System.Drawing.Size(280,20)
$objLabel.Text = "Select a Folder:"
$objForm.Controls.Add($objLabel)
###################
##### List Selection #####
$objListBox = New-Object System.Windows.Forms.ListBox
$objListBox.Location = New-Object System.Drawing.Size(10,40)
$objListBox.Size = New-Object System.Drawing.Size(260,20)
$objListBox.Height = 80
##### Options for Path #####
[void] $objListBox.Items.Add("Department")
[void] $objListBox.Items.Add("Engagement")
[void] $objListBox.Items.Add("Project")
[void] $objListBox.Items.Add("Opportunity")
###################
$objForm.Controls.Add($objListBox)
$objForm.Topmost = $True
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
###Path Creation###
$MYDRIVE_PATH = $MYDRIVE_PATH + $x
###################
$ParentFolder = $x
################################################################################################
####################################### Name Box #######################################
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Data Entry Form"
$objForm.Size = New-Object System.Drawing.Size(300,200)
$objForm.StartPosition = "CenterScreen"
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
{$x=$objTextBox.Text;$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
{$objForm.Close()}})
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(75,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click({$x=$objTextBox.Text;$objForm.Close();$FolderNameCheck = 0})
$objForm.Controls.Add($OKButton)
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(150,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close();[System.Environment]::Exit(0)})
$objForm.Controls.Add($CancelButton)
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,20)
$objLabel.Size = New-Object System.Drawing.Size(280,20)
$objLabel.Text = "Enter File Name:"
$objForm.Controls.Add($objLabel)
$objTextBox = New-Object System.Windows.Forms.TextBox
$objTextBox.Location = New-Object System.Drawing.Size(10,40)
$objTextBox.Size = New-Object System.Drawing.Size(260,20)
$objForm.Controls.Add($objTextBox)
$objForm.Topmost = $True
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
$FolderName = $x
#if([string]::IsNullOrEmpty($FolderName)){$FolderNameCheck = 1}
$SecurityName = "US-SG " + $FolderName
################################################################################################
if( $objBrenda.Checked -eq $true ) # Checks if it is a Brenda Fernandez folder and if so adds _QPC to the security group name
{
$SecurityName = $SecurityName + "_QPC"
}
if($FolderNameCheck -eq 0 -and $DestinationCheck -eq 0 ) #Failsafe to make sure all information has been filled out
{
#####Create Security Group#####
$Description = $ParentFolder + "\" + $FolderName
New-QADGroup -Name $SecurityName -samAccountName $SecurityName -Description $Description -GroupScope DomainLocal -ParentContainer "------------"
##################################
$Ar = New-Object system.security.accesscontrol.filesystemaccessrule($SecurityName,$groupRights,$InheritanceFlag, $PropagationFlag,"Allow") #Adds all the security options to the variable $Ar
$UserFolder = New-Item -Path ('{0}\{1}' -f $MYDRIVE_PATH,$FolderName) -ItemType Directory # Creates New Folder
$Acl = get-acl -Path $UserFolder.FullName #Finds location of Folder's Security group
while(1) { #Waits for Security Group to be created and appear in Active Directory
if( $Acl.SetAccessRule($Ar) -eq $NULL ){ break; } ########### Once Completed it will set the $Ar to $Acl and break out of the loop
Start-Sleep -s 1
}
$Acl | Set-Acl $UserFolder.FullName #####Set security group to user folder
##################### Notification Popup ##########################
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$objNotifyIcon = New-Object System.Windows.Forms.NotifyIcon
$objNotifyIcon.Icon = "C:\Users\Downloads\success.ico"
$objNotifyIcon.BalloonTipIcon = "Info"
$objNotifyIcon.BalloonTipText = "The folder " + $FolderName + " has been successfully created."
$objNotifyIcon.BalloonTipTitle = "Proccess Complete"
$objNotifyIcon.Visible = $True
$objNotifyIcon.ShowBalloonTip(10000)
Start-Sleep -s 1
$objNotifyIcon.Dispose()
##############################################################################################
if( $objBrenda.Checked -eq $true )
{
$DPText = "US\" + $SecurityName + "_QPC;"
Add-Content $UserListFile $DPText
}
}
if ($ErrorCount -eq 0)
{
Write-Host "Done." -ForegroundColor Green
}
else{
Write-Host "Done. ($ErrorCount)" -ForegroundColor Red
}
<#
if(() -ne $Null)
{
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$objNotifyIcon = New-Object System.Windows.Forms.NotifyIcon
$objNotifyIcon.Icon = "C:\Users\Downloads\success.ico"
$objNotifyIcon.BalloonTipIcon = "Error"
$objNotifyIcon.BalloonTipText = "The security group " + $FolderName + " already exists."
$objNotifyIcon.BalloonTipTitle = "Something went horribly wrong!"
$objNotifyIcon.Visible = $True
$objNotifyIcon.ShowBalloonTip(10000)
Start-Sleep -s 1
$objNotifyIcon.Dispose()
exit
}
#>
#[Windows.Clipboard]::SetText("Meow")
I think you're getting WAY too complicated. I would try using the ActiveDirectory module installed with RSAT (Remote Server Administration Tools). Don't be afraid to leave the GUI, it's not that scary once you get used to it. :)
Really, I mean you could probably do this with a one-liner.