I have built a little gui for getting the acl permissions for folders. with the path button i want to specify the folder path with a folder browser dialog and with the permissions button i want to get the acl. unfortunately the permissions button don't work because it can't get the folder path from the get-folder function. what's wrong with the function?
#################################################### Functions #######################################################
$path = Function Get-Folder ($initialDirectory)
{
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")|Out-Null
$Ordnername = New-Object System.Windows.Forms.FolderBrowserDialog
$Ordnername.Description = "Ordner auswählen"
$Ordnername.rootfolder = "MyComputer"
if($Ordnername.ShowDialog() -eq "OK")
{
$Ordner += $Ordnername.SelectedPath
}
return $Ordner
}
############################################## GetPermissions function
function GetPermissions{
#$Folder = get-folder
$Result = (Get-ACL $path).access | Format-Table IdentityReference,FileSystemRights,AccessControlType,IsInherited,InheritanceFlags | Out-string
$outputBox.Text = $Result
}
function Close{
$Form.Close()
}
###################### CREATING PS GUI TOOL #############################
#### Form settings #################################################################
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$Form = New-Object System.Windows.Forms.Form
$Form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle #modifies the window border
$Form.Text = "Folder Permissions"
$Form.Size = New-Object System.Drawing.Size(1120,330)
$Form.StartPosition = "CenterScreen" #loads the window in the center of the screen
$Form.BackgroundImageLayout = "Zoom"
$Form.MinimizeBox = $False
$Form.MaximizeBox = $False
$Form.WindowState = "Normal"
$Form.SizeGripStyle = "Hide"
$Icon = [system.drawing.icon]::ExtractAssociatedIcon($PSHOME + "\powershell.exe")
$Form.Icon = $Icon
#### Input window with "Folder Path" label ##########################################
#$InputBox = New-Object System.Windows.Forms.TextBox
#$InputBox.Location = New-Object System.Drawing.Size(10,50)
#$InputBox.Size = New-Object System.Drawing.Size(180,20)
#$Form.Controls.Add($InputBox)
#$Label2 = New-Object System.Windows.Forms.Label
#$Label2.Text = "Folder Path:"
#$Label2.AutoSize = $True
#$Label2.Location = New-Object System.Drawing.Size(15,30)
#$Form.Controls.Add($Label2)
#### Group boxes for buttons ########################################################
$groupBox = New-Object System.Windows.Forms.GroupBox
$groupBox.Location = New-Object System.Drawing.Size(10,95)
$groupBox.size = New-Object System.Drawing.Size(180,180)
$groupBox.text = "Controls:"
$Form.Controls.Add($groupBox)
###################### BUTTONS ##########################################################
#### Path ###################################################################
$Path = New-Object System.Windows.Forms.Button
$Path.Location = New-Object System.Drawing.Size(10,10)
$Path.Size = New-Object System.Drawing.Size(150,60)
$Path.Text = "Path"
$Path.Add_Click({Get-folder})
$Path.Cursor = [System.Windows.Forms.Cursors]::Hand
$Form.Controls.Add($Path)
#### Permissions ###################################################################
$Permissions = New-Object System.Windows.Forms.Button
$Permissions.Location = New-Object System.Drawing.Size(15,25)
$Permissions.Size = New-Object System.Drawing.Size(150,60)
$Permissions.Text = "Permissions"
$Permissions.Add_Click({GetPermissions})
$Permissions.Cursor = [System.Windows.Forms.Cursors]::Hand
$groupBox.Controls.Add($Permissions)
#### Close ###################################################################
$Close = New-Object System.Windows.Forms.Button
$Close.Location = New-Object System.Drawing.Size(15,100)
$Close.Size = New-Object System.Drawing.Size(150,60)
$Close.Text = "Close"
$Close.Add_Click({Close})
$Close.Cursor = [System.Windows.Forms.Cursors]::Hand
$groupBox.Controls.Add($Close)
###################### END BUTTONS ######################################################
#### Output Box Field ###############################################################
$outputBox = New-Object System.Windows.Forms.RichTextBox
$outputBox.Location = New-Object System.Drawing.Size(200,20)
$outputBox.Size = New-Object System.Drawing.Size(900,265)
$outputBox.Font = New-Object System.Drawing.Font("Consolas", 8 ,[System.Drawing.FontStyle]::Regular)
$outputBox.MultiLine = $True
$outputBox.ScrollBars = "Vertical"
$Form.Controls.Add($outputBox)
##############################################
$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()
The reason for that mainly has to do with scoping, plus you should not write the function as $path = Function ... making it a call to the function.
Also, $Ordner += $Ordnername.SelectedPath is wrong, because you have never defined what $Ordner is in the function.
Below a rewrite of your code where I took the liberty to change some variable names to make them more self-explanatory:
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
###################### CREATING PS GUI TOOL #############################
# define your selected path variable and initialize to nothing
$SelectedPath = $null
#################################################### Functions #######################################################
function Get-Folder {
$Ordnername = New-Object System.Windows.Forms.FolderBrowserDialog
$Ordnername.Description = "Ordner auswählen"
$Ordnername.rootfolder = "MyComputer"
if($Ordnername.ShowDialog() -eq "OK") {
$script:SelectedPath = $Ordnername.SelectedPath # use script scoping here
$outputBox.Text = $script:SelectedPath
}
}
############################################## GetPermissions function
function Get-Permissions {
$outputBox.Text = '' # clear the textbox
if (-not [string]::IsNullOrWhiteSpace($script:SelectedPath)) {
$Result = (Get-ACL $script:SelectedPath).Access | # use script scoping here
Format-Table IdentityReference,FileSystemRights,AccessControlType,IsInherited,InheritanceFlags | Out-string
$outputBox.Text = $script:SelectedPath + "`r`n`r`n" + $Result
}
}
#### Form settings #################################################################
$Form = New-Object System.Windows.Forms.Form
$Form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle #modifies the window border
$Form.Text = "Folder Permissions"
$Form.Size = New-Object System.Drawing.Size(1120,330)
$Form.StartPosition = "CenterScreen" #loads the window in the center of the screen
$Form.BackgroundImageLayout = "Zoom"
$Form.MinimizeBox = $False
$Form.MaximizeBox = $False
$Form.WindowState = "Normal"
$Form.SizeGripStyle = "Hide"
$Icon = [System.Drawing.Icon]::ExtractAssociatedIcon((Join-Path -Path $PSHOME -ChildPath 'powershell.exe'))
$Form.Icon = $Icon
#### Group boxes for buttons ########################################################
$groupBox = New-Object System.Windows.Forms.GroupBox
$groupBox.Location = New-Object System.Drawing.Size(10,95)
$groupBox.size = New-Object System.Drawing.Size(180,180)
$groupBox.text = "Controls:"
$Form.Controls.Add($groupBox)
###################### BUTTONS ##########################################################
#### Path button ###################################################################
$PathButton = New-Object System.Windows.Forms.Button
$PathButton.Location = New-Object System.Drawing.Size(10,10)
$PathButton.Size = New-Object System.Drawing.Size(150,60)
$PathButton.Text = "Path"
$PathButton.Cursor = [System.Windows.Forms.Cursors]::Hand
$PathButton.Add_Click({Get-Folder})
$Form.Controls.Add($PathButton)
#### Permissions button ###################################################################
$Permissions = New-Object System.Windows.Forms.Button
$Permissions.Location = New-Object System.Drawing.Size(15,25)
$Permissions.Size = New-Object System.Drawing.Size(150,60)
$Permissions.Text = "Permissions"
$Permissions.Cursor = [System.Windows.Forms.Cursors]::Hand
$Permissions.Add_Click({Get-Permissions})
$groupBox.Controls.Add($Permissions)
#### Close ###################################################################
$Close = New-Object System.Windows.Forms.Button
$Close.Location = New-Object System.Drawing.Size(15,100)
$Close.Size = New-Object System.Drawing.Size(150,60)
$Close.Text = "Close"
$Close.Cursor = [System.Windows.Forms.Cursors]::Hand
$Close.Add_Click({$Form.Close()})
$groupBox.Controls.Add($Close)
###################### END BUTTONS ######################################################
#### Output Box Field ###############################################################
$outputBox = New-Object System.Windows.Forms.RichTextBox
$outputBox.Location = New-Object System.Drawing.Size(200,20)
$outputBox.Size = New-Object System.Drawing.Size(900,265)
$outputBox.Font = New-Object System.Drawing.Font("Consolas", 8 ,[System.Drawing.FontStyle]::Regular)
$outputBox.MultiLine = $True
$outputBox.ScrollBars = "Vertical"
$Form.Controls.Add($outputBox)
##############################################
$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()
# destroy the form from memory
$Form.Dispose()
Related
I want to monitor the date and time of a file. I wrote the code that do the job as I want but I can't reposition the gui window. I tried all I could find like "start-job" or create a new runspace but I don't get any results in richtextbox. Any suggestion is welcome.
$targetFile = "full path"
# Function - Add Text to RichTextBox
function Add-RichTextBox{
[CmdletBinding()]
param ($text)
#$richtextbox_output.Text += "`tCOMPUTERNAME: $ComputerName`n"
$richtext.Text += "$text"
$richtext.Text += "`n"
}
# Windows Form
$form = New-Object System.Windows.Forms.Form
$form.Text = "Monitor script"
$form.Size = New-Object System.Drawing.Size(400,300)
$form.StartPosition = 'CenterScreen'
$Font = New-Object System.Drawing.Font("Tahoma",11)
$Form.Font = $Font
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(20,40)
$label.Size = New-Object System.Drawing.Size(200,20)
$label.Text = (Get-Date).ToString()
$form.Controls.Add($label)
$StartButton = New-Object System.Windows.Forms.Button
$StartButton.Location = New-Object System.Drawing.Point(150,220)
$StartButton.Size = New-Object System.Drawing.Size(100,33)
$StartButton.Text = 'Start'
#$StartButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $StartButton
$form.Controls.Add($StartButton)
$StartButton.Add_click({
while($true){
$lastdate = Get-ChildItem $targetFile
$Date = $lastdate.LastAccessTime.ToString()
Add-RichTextBox "$date"
$richtext.SelectionStart = $richtext.TextLength
$richText.ScrollToCaret()
#$richtext.refresh()
#$form.refresh()
Start-sleep 30
}
})
## the Rich text box
$richtext = new-object System.Windows.Forms.RichTextBox
$richtext.Location = New-Object System.Drawing.Point(20,60)
$richtext.multiline = $true
$richtext.Name = "Results"
$richtext.text = "Results:`n"
$richtext.scrollbars = "Both"
$richtext.Height = 120
$richtext.width = 350
$richtext.font = new-object system.drawing.font "Lucida Console",10
$Form.controls.add($richtext)
$Form.Add_Shown({$Form.Activate()})
$form.ShowDialog()
Continuing from my comment to use a Timer on your form (if you absolutely do not want a FileSystemWatcher), here's how you can do that:
$targetFile = "D:\Test\blah.txt"
$lastAccessed = (Get-Date) # a variable to keep track of the last LastAccessTime of the file
# Windows Form
$form = New-Object System.Windows.Forms.Form
$form.Text = "Monitor script"
$form.Size = New-Object System.Drawing.Size(400,300)
$form.StartPosition = 'CenterScreen'
$Font = New-Object System.Drawing.Font("Tahoma",11)
$form.Font = $Font
# Label
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(20,40)
$label.Size = New-Object System.Drawing.Size(200,20)
$label.Text = (Get-Date).ToString()
$form.Controls.Add($label)
# Button
$StartButton = New-Object System.Windows.Forms.Button
$StartButton.Location = New-Object System.Drawing.Point(150,200)
$StartButton.Size = New-Object System.Drawing.Size(100,33)
$StartButton.Text = 'Start'
$form.Controls.Add($StartButton)
$StartButton.Add_click({
$timer.Enabled = $true
$timer.Start()
})
# RichTextBox
$richtext = New-Object System.Windows.Forms.RichTextBox
$richtext.Location = New-Object System.Drawing.Point(20,60)
$richtext.Multiline = $true
$richtext.Name = "Results"
$richtext.Text = "Results:`r`n"
$richtext.ScrollBars = "Both"
$richtext.Height = 120
$richtext.Width = 350
$richtext.Font = New-Object System.Drawing.Font "Lucida Console",10
$form.Controls.Add($richtext)
# Timer
$timer = New-Object System.Windows.Forms.Timer
$timer.Interval = 30000 # 30 seconds
$timer.Enabled = $false # disabled at first
$timer.Add_Tick({
$file = Get-Item -Path $targetFile -ErrorAction SilentlyContinue
# if we can find the file and its last AccessedTime is not
# the same as we already stored in variable $lastAccessed
# use script-scoping here, so $script:lastAccessed instead of $lastAccessed
if ($file -and $file.LastAccessTime -gt $script:lastAccessed) {
$richtext.AppendText("$($file.LastAccessTime.ToString())`r`n")
$script:lastAccessed = $file.LastAccessTime # remember this new datetime
}
})
$form.ShowDialog()
# Important: Clean up
$timer.Stop()
$timer.Dispose()
$richtext.Dispose()
$form.Dispose()
I would like to ask how to refresh and changed the jpg below GUI from "Offline" to "Online" based on the live monitoring of specific folder and filenames. For example, I am monitoring a folder for any changes, if in the folder a filename exists with specific string, then it will shows in GUI as "Online" and when the folder doesn't contain the file with specific string, then it will shows as "Offline" in the GUI.
Below is the code that I have.
####################################### Form settings ##############################################
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$Form = New-Object System.Windows.Forms.Form
$Form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedDialog
$Form.Anchor = "Top,Bottom,Left,Right"
$Form.Size = New-Object System.Drawing.Size(1920,1600)
$Form.AutoScale = $True
$Form.StartPosition = "CenterScreen" #loads the window in the center of the screen
$Form.BackgroundImageLayout = "Zoom"
$Form.MinimizeBox = $True
$Form.MaximizeBox = $False
$Form.WindowState = "Normal"
$Form.SizeGripStyle = "Auto"
$Form.AutoSizeMode = New-Object System.Windows.Forms.AutoSizeMode
$Form.SizeGripStyle = "Show"
$Form.BackColor = "LightGray"
$Form.Icon = $Icon
$TestStatus16 = Test-Path -Path C:\Desktop\Newfolder\P16*
$Online = [System.Drawing.Image]::FromFile("C:\Online.jpg")
$Offline = [System.Drawing.Image]::FromFile("C:\Offline.jpg")
$Photo16 = New-Object System.Windows.Forms.Button
$Photo16.Location = New-Object System.Drawing.Point(830,250)
$Photo16.Size = New-Object System.Drawing.Size(200,50)
$Photo16.Font = New-Object System.Drawing.Font ("HelveticaNeueLTStd-Roman",11,[System.Drawing.FontStyle]::Bold)
$Photo16.Text = "Photo # 16"
$Photo16.BackColor = "SandyBrown"
$Photo16.ForeColor = "Black"
$Form.Controls.Add($Photo16)
$StatusPhoto16 = New-Object System.Windows.Forms.PictureBox
$StatusPhoto16.Location = New-Object System.Drawing.Size(1050,252)
$StatusPhoto16.Size = New-Object System.Drawing.Size(150,45)
$StatusPhoto16.SizeMode = [System.Windows.Forms.PictureBoxSizeMode]::StretchImage
$StatusPhoto16.BackColor = "White"
$Form.Controls.Add($StatusPhoto16)
if($TestStatus16 -eq $True){$StatusPhoto16.Image=$Online}else{$StatusPhoto16.Image=$Offline}
$Timer16 = New-Object System.Windows.Forms.Timer
$Timer16.Interval = 3000
$Timer16.Add_Tick({Status16 $StatusPhoto16})
$Timer16.Enabled = $False
$Timer16.Start()
$Form.Add_Shown({$Form.Activate()})
$Form.ShowDialog()
Kindly help improving the above code.
You're missing is what I'm assuming it to be the function call for your Test-Path.
What's needed is for it to be defined, the object ($StatusPhoto16) be passed to it, and the update of the actual image based on your Test-Path.
####################################### Form settings ##############################################
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$Form = New-Object System.Windows.Forms.Form
$Form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedDialog
$Form.Anchor = "Top,Bottom,Left,Right"
$Form.Size = New-Object System.Drawing.Size(1920,1600)
$Form.AutoScale = $True
$Form.StartPosition = "CenterScreen" #loads the window in the center of the screen
$Form.BackgroundImageLayout = "Zoom"
$Form.MinimizeBox = $True
$Form.MaximizeBox = $False
$Form.WindowState = "Normal"
$Form.SizeGripStyle = "Auto"
$Form.AutoSizeMode = New-Object System.Windows.Forms.AutoSizeMode
$Form.SizeGripStyle = "Show"
$Form.BackColor = "LightGray"
$Form.Icon = $Icon
$Online = [System.Drawing.Image]::FromFile("C:\Online.jpg")
$Offline = [System.Drawing.Image]::FromFile("C:\Offline.jpg")
$Photo16 = New-Object System.Windows.Forms.Button
$Photo16.Location = New-Object System.Drawing.Point(830,250)
$Photo16.Size = New-Object System.Drawing.Size(200,50)
$Photo16.Font = New-Object System.Drawing.Font ("HelveticaNeueLTStd-Roman",11,[System.Drawing.FontStyle]::Bold)
$Photo16.Text = "Photo # 16"
$Photo16.BackColor = "SandyBrown"
$Photo16.ForeColor = "Black"
$Form.Controls.Add($Photo16)
function status16 ($StatusPhoto16) {
if (Test-Path -Path 'C:\Desktop\Newfolder\P16*')
{
$StatusPhoto16.Image = $Online
}
else
{
$StatusPhoto16.Image = $Offline
}
}
$StatusPhoto16 = New-Object System.Windows.Forms.PictureBox
$StatusPhoto16.Location = New-Object System.Drawing.Size(1050,252)
$StatusPhoto16.Size = New-Object System.Drawing.Size(150,45)
$StatusPhoto16.SizeMode = [System.Windows.Forms.PictureBoxSizeMode]::StretchImage
$StatusPhoto16.BackColor = "White"
$Form.Controls.Add($StatusPhoto16)
$Timer16 = New-Object System.Windows.Forms.Timer
$Timer16.Interval = 3000
$Timer16.Add_Tick({status16 $StatusPhoto16})
$Timer16.Enabled = $False
$Timer16.Start()
$Form.Add_Shown({$Form.Activate()})
$Form.ShowDialog()
i have written a gui in powershell for creating groups in active directory depending on my selections i have made in the gui.
#### Form settings ################################################################
$Form = New-Object System.Windows.Forms.Form
$Form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle #modifies the window border
$Form.Text = "AD GROUP CREATER"
$Form.Size = New-Object System.Drawing.Size(750,470)
$Form.StartPosition = "CenterScreen" #loads the window in the center of the screen
$Form.MinimizeBox = $False
$Form.MaximizeBox = $False
$Form.WindowState = "Normal"
$Form.SizeGripStyle = "Hide"
$Icon = [System.Drawing.Icon]::ExtractAssociatedIcon((Join-Path -Path $PSHOME -ChildPath 'powershell.exe'))
$Form.Icon = $Icon
#### Label settings #################################################################
$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.Text = "CREATE IN"
$Label.Font = "8,style=bold"
$Label.Forecolor = "black"
$Form.Controls.Add($Label)
#### Group box ########################################################
$groupBox = New-Object System.Windows.Forms.GroupBox
$groupBox.Location = New-Object System.Drawing.Size(10,30)
$groupBox.size = New-Object System.Drawing.Size(320,50)
$Form.Controls.Add($groupBox)
#### A button ###################################################################
$A= New-Object System.Windows.Forms.RadioButton
$A.Location = New-Object System.Drawing.Size(15,20)
$A.Size = New-Object System.Drawing.Size(100,20)
$A.Text = "A"
$A.Cursor = [System.Windows.Forms.Cursors]::Hand
$A.Add_Click({})
$groupBox.Controls.Add($A)
#### B button ###################################################################
$B= New-Object System.Windows.Forms.RadioButton
$B.Location = New-Object System.Drawing.Size(150,20)
$B.Size = New-Object System.Drawing.Size(150,20)
$B.Text = "B"
$B.Cursor = [System.Windows.Forms.Cursors]::Hand
$B.Add_Click({})
$groupBox.Controls.Add($B)
#### Label 2 settings #################################################################
$Label2 = New-Object System.Windows.Forms.Label
$Label2.Location = New-Object System.Drawing.Size(10,100)
$Label2.Size = New-Object System.Drawing.Size(280,20)
$Label2.Text = "GROUP NAME"
$label2.Font = "8, style=bold"
$Label2.Forecolor = "black"
$Form.Controls.Add($Label2)
#### textbox settings #################################################################
$Textbox = New-Object System.Windows.Forms.TextBox
$Textbox.Location = New-Object System.Drawing.Size(10,120)
$Textbox.Size = New-Object System.Drawing.Size(320,20)
$Form.Controls.Add($Textbox)
#### Label 3 settings #################################################################
$Label3 = New-Object System.Windows.Forms.Label
$Label3.Location = New-Object System.Drawing.Size(10,170)
$Label3.Size = New-Object System.Drawing.Size(310,20)
$Label3.Text = "GROUP SECTION"
$Label3.Forecolor = "black"
$label3.Font = "8, style=bold"
$Form.Controls.Add($Label3)
#### Group box 2 ########################################################
$groupBox2 = New-Object System.Windows.Forms.GroupBox
$groupBox2.Location = New-Object System.Drawing.Size(10,190)
$groupBox2.size = New-Object System.Drawing.Size(320,160)
$Form.Controls.Add($groupBox2)
#### local button ###################################################################
$lokal = New-Object System.Windows.Forms.RadioButton
$lokal.Location = New-Object System.Drawing.Size(15,20)
$lokal.Size = New-Object System.Drawing.Size(150,20)
$lokal.Text = "Local"
$lokal.Cursor = [System.Windows.Forms.Cursors]::Hand
$lokal.Add_Click({})
$groupBox2.Controls.Add($lokal)
#### global button ###################################################################
$global = New-Object System.Windows.Forms.RadioButton
$global.Location = New-Object System.Drawing.Size(15,70)
$global.Size = New-Object System.Drawing.Size(150,20)
$global.Text = "Global"
$global.Cursor = [System.Windows.Forms.Cursors]::Hand
$global.Add_Click({})
$groupBox2.Controls.Add($global)
#### universal button ###################################################################
$universal = New-Object System.Windows.Forms.RadioButton
$universal.Location = New-Object System.Drawing.Size(15,120)
$universal.Size = New-Object System.Drawing.Size(150,20)
$universal.Text = "Universal"
$universal.Cursor = [System.Windows.Forms.Cursors]::Hand
$universal.Add_Click({})
$groupBox2.Controls.Add($universal)
#### Label 4 settings #################################################################
$Label4 = New-Object System.Windows.Forms.Label
$Label4.Location = New-Object System.Drawing.Size(400,170)
$Label4.Size = New-Object System.Drawing.Size(320,20)
$Label4.Text = "GROUP TYPE"
$Label4.Forecolor = "black"
$label4.Font = "8, style=bold"
$Form.Controls.Add($Label4)
#### Group box 3 ########################################################
$groupBox3 = New-Object System.Windows.Forms.GroupBox
$groupBox3.Location = New-Object System.Drawing.Size(400,190)
$groupBox3.size = New-Object System.Drawing.Size(320,160)
$Form.Controls.Add($groupBox3)
#### security button ###################################################################
$security = New-Object System.Windows.Forms.RadioButton
$security.Location = New-Object System.Drawing.Size(15,20)
$security.Size = New-Object System.Drawing.Size(150,20)
$security.Text = "Security"
$security.Cursor = [System.Windows.Forms.Cursors]::Hand
$security.Add_Click({})
$groupBox3.Controls.Add($security)
#### Distribution button ###################################################################
$Distribution = New-Object System.Windows.Forms.RadioButton
$Distribution.Location = New-Object System.Drawing.Size(15,70)
$Distribution.Size = New-Object System.Drawing.Size(150,20)
$Distribution.Text = "Distribution"
$Distribution.Cursor = [System.Windows.Forms.Cursors]::Hand
$Distribution.Add_Click({})
$groupBox3.Controls.Add($Distribution)
#### Create button ###################################################################
$Create = New-Object System.Windows.Forms.Button
$Create.Location = New-Object System.Drawing.Size(10,370)
$Create.Text = "Create"
$Create.Cursor = [System.Windows.Forms.Cursors]::Hand
$Create.Width = 710
$Create.Height = 50
$Create.Font = "12,style=bold"
$Create.Add_Click({pinginfo})
$Form.Controls.Add($Create)
$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()
# destroy the form from memory
$Form.Dispose()
The gui is finished but i have problems completing my code. what do i need to add to my code that the create button creates the groups? furthermore i think that an assignment to the selection is missing
#Theo please have a look. I still need to input a GroupScope. After thet, the script says it is a wrong name but my name looks like test-group
#### Form settings ################################################################
$Form = New-Object System.Windows.Forms.Form
$Form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle #modifies the window border
$Form.Text = "AD GROUP CREATER"
$Form.Size = New-Object System.Drawing.Size(750,470)
$Form.StartPosition = "CenterScreen" #loads the window in the center of the screen
$Form.MinimizeBox = $False
$Form.MaximizeBox = $False
$Form.WindowState = "Normal"
$Form.SizeGripStyle = "Hide"
$Icon = [System.Drawing.Icon]::ExtractAssociatedIcon((Join-Path -Path $PSHOME -ChildPath 'powershell.exe'))
$Form.Icon = $Icon
#### Label settings #################################################################
$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.Text = "CREATE IN"
$Label.Font = "8,style=bold"
$Label.Forecolor = "black"
$Form.Controls.Add($Label)
#### Group box ########################################################
$groupBox = New-Object System.Windows.Forms.GroupBox
$groupBox.Location = New-Object System.Drawing.Size(10,30)
$groupBox.size = New-Object System.Drawing.Size(320,50)
$Form.Controls.Add($groupBox)
#### A button ###################################################################
$A= New-Object System.Windows.Forms.RadioButton
$A.Location = New-Object System.Drawing.Size(15,20)
$A.Size = New-Object System.Drawing.Size(100,20)
$A.Text = "A"
$A.Cursor = [System.Windows.Forms.Cursors]::Hand
$A.Add_Click({})
$groupBox.Controls.Add($A)
#### B button ###################################################################
$B= New-Object System.Windows.Forms.RadioButton
$B.Location = New-Object System.Drawing.Size(150,20)
$B.Size = New-Object System.Drawing.Size(150,20)
$B.Text = "B"
$B.Cursor = [System.Windows.Forms.Cursors]::Hand
$B.Add_Click({})
$groupBox.Controls.Add($B)
#### Label 2 settings #################################################################
$Label2 = New-Object System.Windows.Forms.Label
$Label2.Location = New-Object System.Drawing.Size(10,100)
$Label2.Size = New-Object System.Drawing.Size(280,20)
$Label2.Text = "GROUP NAME"
$label2.Font = "8, style=bold"
$Label2.Forecolor = "black"
$Form.Controls.Add($Label2)
#### textbox settings #################################################################
$Textbox = New-Object System.Windows.Forms.TextBox
$Textbox.Location = New-Object System.Drawing.Size(10,120)
$Textbox.Size = New-Object System.Drawing.Size(320,20)
$Form.Controls.Add($Textbox)
#### Label 3 settings #################################################################
$Label3 = New-Object System.Windows.Forms.Label
$Label3.Location = New-Object System.Drawing.Size(10,170)
$Label3.Size = New-Object System.Drawing.Size(310,20)
$Label3.Text = "GROUP SECTION"
$Label3.Forecolor = "black"
$label3.Font = "8, style=bold"
$Form.Controls.Add($Label3)
#### Group box 2 ########################################################
$groupBox2 = New-Object System.Windows.Forms.GroupBox
$groupBox2.Location = New-Object System.Drawing.Size(10,190)
$groupBox2.size = New-Object System.Drawing.Size(320,160)
$Form.Controls.Add($groupBox2)
#### local button ###################################################################
$DomainLocal = New-Object System.Windows.Forms.RadioButton
$DomainLocal.Location = New-Object System.Drawing.Size(15,20)
$DomainLocal.Size = New-Object System.Drawing.Size(150,20)
$DomainLocal.Text = "Local"
$DomainLocal.Cursor = [System.Windows.Forms.Cursors]::Hand
$DomainLocal.Add_Click({})
$groupBox2.Controls.Add($DomainLocal)
#### global button ###################################################################
$global = New-Object System.Windows.Forms.RadioButton
$global.Location = New-Object System.Drawing.Size(15,70)
$global.Size = New-Object System.Drawing.Size(150,20)
$global.Text = "Global"
$global.Cursor = [System.Windows.Forms.Cursors]::Hand
$global.Add_Click({})
$groupBox2.Controls.Add($global)
#### universal button ###################################################################
$universal = New-Object System.Windows.Forms.RadioButton
$universal.Location = New-Object System.Drawing.Size(15,120)
$universal.Size = New-Object System.Drawing.Size(150,20)
$universal.Text = "Universal"
$universal.Cursor = [System.Windows.Forms.Cursors]::Hand
$universal.Add_Click({})
$groupBox2.Controls.Add($universal)
#### Label 4 settings #################################################################
$Label4 = New-Object System.Windows.Forms.Label
$Label4.Location = New-Object System.Drawing.Size(400,170)
$Label4.Size = New-Object System.Drawing.Size(320,20)
$Label4.Text = "GROUP TYPE"
$Label4.Forecolor = "black"
$label4.Font = "8, style=bold"
$Form.Controls.Add($Label4)
#### Group box 3 ########################################################
$groupBox3 = New-Object System.Windows.Forms.GroupBox
$groupBox3.Location = New-Object System.Drawing.Size(400,190)
$groupBox3.size = New-Object System.Drawing.Size(320,160)
$Form.Controls.Add($groupBox3)
#### security button ###################################################################
$security = New-Object System.Windows.Forms.RadioButton
$security.Location = New-Object System.Drawing.Size(15,20)
$security.Size = New-Object System.Drawing.Size(150,20)
$security.Text = "Security"
$security.Cursor = [System.Windows.Forms.Cursors]::Hand
$security.Add_Click({})
$groupBox3.Controls.Add($security)
#### Distribution button ###################################################################
$Distribution = New-Object System.Windows.Forms.RadioButton
$Distribution.Location = New-Object System.Drawing.Size(15,70)
$Distribution.Size = New-Object System.Drawing.Size(150,20)
$Distribution.Text = "Distribution"
$Distribution.Cursor = [System.Windows.Forms.Cursors]::Hand
$Distribution.Add_Click({})
$groupBox3.Controls.Add($Distribution)
#### Create button ###################################################################
$Create = New-Object System.Windows.Forms.Button
$Create.Location = New-Object System.Drawing.Size(10,370)
$Create.Text = "Create"
$Create.Cursor = [System.Windows.Forms.Cursors]::Hand
$Create.Width = 710
$Create.Height = 50
$Create.Font = "12,style=bold"
$Create.Add_Click({
# create an empty parameters object for New-ADGroup
$params = '' | Select-Object Name, GroupScope, GroupCategory, Path
$params.Name = $Textbox.Text.Trim()
# test if there is a name given and if so, test if that name does not already exist in AD
if ([string]::IsNullOrWhiteSpace($params.Name) -or (Get-ADGroup -Filter "Name -eq $($params.Name)")) {
# display an error about the name field
# I'll leave this as exercise for you
return
}
if ($DomainLocal.Checked) {$params.GroupScope = $DomainLocal.Text}
elseif ($global.Checked) {$params.GroupScope = $global.Text}
elseif ($universal.Checked) {$params.GroupScope = $universal.Text}
else {
# display an error that there MUST be a choice here
# I'll leave this as exercise for you
return
}
if ($security.Checked) {$params.GroupCategory = $security.Text}
elseif ($Distribution.Checked) {$params.GroupCategory = $Distribution.Text}
else {
# display an error that there MUST be a choice here
# I'll leave this as exercise for you
return
}
if ($A.Checked) { $params.Path = 'TheOU for option A' }
elseif( $B.Checked) { $params.Path = 'TheOU for option B' }
else {
# display an error that there MUST be a choice here
# I'll leave this as exercise for you
return
}
# next create the group
New-ADGroup #params
})
$Form.Controls.Add($Create)
$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()
# destroy the form from memory
$Form.Dispose()
Instead of the now undefined function pinginfo, you will need to check what radiobutton is checked in "Create IN" and from that define the -Path property for New-ADGroup.
Then test if a name is filled in, test if this name does not already exist and use that for -Name.
The checked radiobutton in "Group Section" decides the parameter -GroupScope and the one in "Group Type" is for -GroupCategory.
When you have collected those values and tested if they were filled in, create the group.
Something like this perhaps
Note: The order in which you test and fill in the Hashtable's properties is not important for splatting purposes.
It's up to you if you want to follow the forms TAB-order or use a different strategy.
$Create.Add_Click({
# create an empty parameters splatting Hashtable for New-ADGroup
$params = #{Path = $null; Name = $null; GroupScope = $null; GroupCategory = $null}
# test CREATE IN (OU path)
if ($A.Checked) { $params.Path = 'TheOU for option A' }
elseif ($B.Checked) { $params.Path = 'TheOU for option B' }
else {
# display an error that there MUST be a choice here
# I'll leave this as exercise for you
return
}
# test GROUP NAME
if ([string]::IsNullOrWhiteSpace($Textbox.Text)) {
# display an error about the name field being empty
# I'll leave this as exercise for you
return
}
$params.Name = $Textbox.Text.Trim()
# test GROUP SECTION (GroupScope)
if ($DomainLocal.Checked) {$params.GroupScope = 'DomainLocal'} # as opposed to 'Local' as in the radiobutton's Text
elseif ($global.Checked) {$params.GroupScope = $global.Text} # 'Global'
elseif ($universal.Checked) {$params.GroupScope = $universal.Text} # 'Universal'
else {
# display an error that there MUST be a choice here
# I'll leave this as exercise for you
return
}
# test GROUP TYPE (GroupCategory)
if ($security.Checked) {$params.GroupCategory = $security.Text} # 'Security'
elseif ($Distribution.Checked) {$params.GroupCategory = $Distribution.Text} # 'Distribution '
else {
# display an error that there MUST be a choice here
# I'll leave this as exercise for you
return
}
# finally check to test if the given groupname already exists
if (Get-ADGroup -Filter "Name -eq $($params.Name)") {
# display an error that there already is a group by that name
return
}
# next create the group
New-ADGroup #params
})
I wrote six small gui scripts that I'd like to place in a single file, for the sake of converting said file into rxecutable with ps2exe.
I've found a script ,here on stack that is perfect for what I want. Unfortunatelly I cann't find any info on script placement within tabs and MS documentation leads me to ISE tabs, which is not helpfull.
Say I'd like to place this
Add-Type -AssemblyName PresentationFramework
$button_click = {
$folder = $textBox1.Text;
$pytanie = [System.Windows.MessageBox]::Show('Czy chcesz usunac folder?', '', '4');
If($pytanie -eq 'Yes')
{Remove-Item –path $folder –recurse -Force};
$test = Test-Path $folder;
if ($test -eq $false){[System.Windows.MessageBox]::Show('Folder Usuniety', '', '0')}}
$label2 = New-Object System.Windows.Forms.Label
$label2.AutoSize = $True
$label2.Text = ("Scieżka")
$label2.Location = New-Object System.Drawing.Point (10,30)
$label2.Size = New-Object System.Drawing.Size (25,70)
$label2.Font = [System.Drawing.Font]::new("Arial", 10, [System.Drawing.FontStyle]::Bold)
$textBox1 = New-Object System.Windows.Forms.TextBox
$textBox1.Location = New-Object System.Drawing.Point(10,70) ### Location of the text box
$textBox1.Size = New-Object System.Drawing.Size(200,50) ### Size of the text box
$textBox1.Multiline = $false ### Allows multiple lines of data
$textBox1.Font = New-Object System.Drawing.Font("Consolas",10,[System.Drawing.FontStyle]::Regular)
$textBox1.ReadOnly=$false
$button = New-Object System.Windows.Forms.Button
$button.Location = New-Object System.Drawing.Point(10,120)
$button.Size = New-Object System.Drawing.Size (200,30)
$button.Text = "Usun Folder"
$button.Add_Click($button_click)
$form = New-Object System.Windows.Forms.Form
$form.Text = 'Mapowanie' ### Text to be displayed in the title
$form.Size = New-Object System.Drawing.Size(240,200) ### Size of the window
$form.StartPosition = 'Manual'
$form.Location = '10,10'
$form.Topmost = $true ### Optional - Opens on top of other windows
$form.Controls.AddRange(#($textBox1,$button, $label2))
$form.ShowDialog()
within a tab. How to do it?
I think that it the
$Tab1.Controls.Add($button)
You are looking for.
For example
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$ApplicationForm = New-Object System.Windows.Forms.Form
$ApplicationForm.StartPosition = "CenterScreen"
$ApplicationForm.Topmost = $false
$ApplicationForm.Size = "800,600"
$FormTabControl = New-object System.Windows.Forms.TabControl
$FormTabControl.Size = "755,475"
$FormTabControl.Location = "25,75"
$ApplicationForm.Controls.Add($FormTabControl)
$Tab1 = New-object System.Windows.Forms.Tabpage
$Tab1.DataBindings.DefaultDataSourceUpdateMode = 0
$Tab1.UseVisualStyleBackColor = $True
$Tab1.Name = "Tab1"
$Tab1.Text = "Tab1”
$FormTabControl.Controls.Add($Tab1)
$textBox1 = New-Object System.Windows.Forms.TextBox
$textBox1.Location = New-Object System.Drawing.Point(10,70) ### Location of the text box
$textBox1.Size = New-Object System.Drawing.Size(200,50) ### Size of the text box
$textBox1.Multiline = $false ### Allows multiple lines of data
$textBox1.Font = New-Object System.Drawing.Font("Consolas",10,[System.Drawing.FontStyle]::Regular)
$textBox1.ReadOnly=$false
$Tab1.Controls.Add($textBox1)
$Tab2 = New-object System.Windows.Forms.Tabpage
$Tab2.DataBindings.DefaultDataSourceUpdateMode = 0
$Tab2.UseVisualStyleBackColor = $True
$Tab2.Name = "Tab2"
$Tab2.Text = "Tab2”
$FormTabControl.Controls.Add($Tab2)
$button = New-Object System.Windows.Forms.Button
$button.Location = New-Object System.Drawing.Point(10,120)
$button.Size = New-Object System.Drawing.Size (200,30)
$button.Text = "Usun Folder"
$button.Add_Click($button_click)
$Tab2.Controls.Add($button)
# Initlize the form
$ApplicationForm.Add_Shown({$ApplicationForm.Activate()})
[void] $ApplicationForm.ShowDialog()
Trying to create a simple GUI Script using powershell to ping a TP ip address and display it with a simple Green/Red Image depending whether its on/off.
But for some reason, when hitting the Close button, I cant seem to remove the green/red dots.
The File contains:
Arial,172.0.0.0
Bodoni,172.0.0.0
Caslon,172.0.0.1
Script:
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$Form = New-Object System.Windows.Forms.Form
$Form.text = "Hardware Checks"
$Form.Size = New-Object System.Drawing.Size(600,600)
$arrayTPs = Get-Content -path "C:\Activity monitor\TPs.txt"
#Image Locations
$filered = (get-item 'C:\Activity monitor\red1.png')
$filegreen = (get-item 'C:\Activity monitor\green1.png')
$imgred = [System.Drawing.Image]::Fromfile($filered)
$imggreen = [System.Drawing.Image]::Fromfile($filegreen)
$outputBox = New-Object System.Windows.Forms.TextBox
$outputBox.Location = New-Object System.Drawing.Size(10,200)
$outputBox.Size = New-Object System.Drawing.Size(565,200)
$outputBox.MultiLine = $True
$outputBox.ScrollBars = "Vertical"
$Form.Controls.Add($outputBox)
$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(450,30)
$Button.Size = New-Object System.Drawing.Size(75,75)
$Button.Text = "Check"
$Button.Add_Click({
$Form.controls.Remove($outputBox)
$Form.controls.refresh()
foreach($room in $arrayTPs){
#Get Name of room from array
$roomAlone = $room -split ","
$roomAlone[0]
$pictureBoxRed = new-object Windows.Forms.PictureBox
$pictureBoxGreen = new-object Windows.Forms.PictureBox
#Add room name to textbox
if($roomAlone[1] -eq "172.0.0.0"){
$pictureBoxGreen.Width = 10
$pictureBoxGreen.Height = 10
$pictureBoxGreen.Image = $imggreen
$pictureBoxGreen.Location = New-Object System.Drawing.Point(90,$counterpic)
$outputBox.controls.add($pictureBoxGreen)
$Form.Controls.Add($outputBox)}
else{
$pictureBoxRed.Width = 10
$pictureBoxRed.Height = 10
$pictureBoxRed.Image = $imgred
$pictureBoxRed.Location = New-Object System.Drawing.Point(90,$counterpic)
$outputBox.controls.add($pictureBoxRed)
$Form.Controls.Add($outputBox)
}
$counterpic = $Counterpic + 20
$counter = $Counter + 20
}})
$Form.Controls.Add($Button)
$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Size(450,520)
$cancelButton.Size = New-Object System.Drawing.Size(75,25)
$cancelButton.Text = "Close"
$cancelButton.Add_Click({
$outputBox.controls.Remove($pictureBoxRed)
$outputBox.controls.Remove($pictureBoxGreen)
$outputBox.Controls.Equals($null)
$outputBox.controls.update()
$Form.Refresh()
foreach($room in $arrayTPs){
#Get Name of room from array
$roomAlone = $room -split ","
$roomAlone[0]
$pictureBoxRed = new-object Windows.Forms.PictureBox
$pictureBoxGreen = new-object Windows.Forms.PictureBox
}})
$Form.Controls.Add($cancelButton)
$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()