currently i am working on an little PowerShell GUI Porject.
I have a Form within this Form there's a Button to call another Form that provides an Listbox with some items in it.
So far so good - i'm getting the results as expected.
Now i mark one of the items - click on OK - then it should do something.
And here is my Problem, it does nothing :D No Error, no warning, just nothing.
If i test with write-host "$($listBox.SelectedItem)" - i get the selected Item as i expect it.
But if you want to use it with get-adfsrelyingpartytrust -Name $listBox.SelectedItem - nothing happens.
If i execute the function without the rest (so that only the listbox appears) everything works as intended O_o.
Code:
function single_rpt{ $RPTForm = New-Object System.Windows.Forms.Form
$RPTForm.ClientSize = '400,200'
$RPTForm.Text = "Choose"
$RPTForm.BackColor = "#b2b2b2"
$RPTForm.AutoSize = $false
$RPTForm.StartPosition = 'CenterScreen'
$RPTForm.ControlBox = $true
$rptButton = New-Object System.Windows.Forms.Button
$rptButton.BackColor = "#ffffff"
$rptButton.Text = "OK"
$rptButton.Width = 90
$rptButton.Height = 30
$rptButton.Location = New-Object System.Drawing.Point(20,125)
$rptButton.Font = 'Microsoft Sans Serif,10'
$rptButton.ForeColor = "#000000"
$rptButton.UseVisualStyleBackColor = $true
$rptButton.DialogResult = 1
# Test List Box
$listBox = New-Object System.Windows.Forms.ListBox
$listBox.Location = New-Object System.Drawing.Point(20,50)
$listBox.Size = New-Object System.Drawing.Size(350,200)
$listBox.Height = 80
$RPTForm.Controls.AddRange(#($listBox,$rptButton))
$listboxArray = #()
$ADFS_RPTS = Get-AdfsRelyingPartyTrust
Foreach($rpt in $ADFS_RPTS)
{
#$Object = Add-Member -TypeName NoteProperty -Name Service -Value $service.Name
$listboxArray += $rpt.Name
}
$listBox.Items.AddRange($listboxArray)
[void]$RPTForm.ShowDialog()
if($RPTForm.DialogResult -eq "OK")
{
Get-AdfsReylingPartyTrus -Name $listBox.SelectedItem
}
}
# Init PowerShell Gui
Add-Type -AssemblyName System.Windows.Forms
# Create Form with size, title and background color, etc.
$TESTForm = New-Object System.Windows.Forms.Form
$TESTForm.ClientSize = '250,300'
$TESTForm.Text = "TEST"
$TESTForm.BackColor = "#ffffff"
$TESTForm.AutoSize = $false
$TESTForm.StartPosition = 'CenterScreen'
$TESTForm.ControlBox = $true
$RPTSingle = New-Object System.Windows.Forms.Button
$RPTSingle.BackColor = "#ffffff"
$RPTSingle.Text = "RPTSingle"
$RPTSingle.Width = 90
$RPTSingle.Height = 30
$RPTSingle.Location = New-Object System.Drawing.Point(75,125)
$RPTSingle.Font = 'Microsoft Sans Serif,10'
$RPTSingle.ForeColor = "#000000"
$RPTSingle.UseVisualStyleBackColor = $true
$RPTSingle.Add_Click({single_rpt})
# Add a Cancel Button
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.BackColor = "#ffffff"
$CancelButton.Text = "Cancel"
$CancelButton.Width = 90
$CancelButton.Height = 30
$CancelButton.Location = New-Object System.Drawing.Point(75,250)
$CancelButton.Font = 'Microsoft Sans Serif,10'
$CancelButton.ForeColor = "#000000"
$CancelButton.UseVisualStyleBackColor = $true
$CancelButton.DialogResult = 2
$TESTForm.Controls.AddRange(#($RPTSingle,$CancelButton))
# Display the form
[void]$TESTForm.ShowDialog()
#------------------------------------#
Related
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 a question about a Powershell Form script that I can't quite get to work yet.
The code below does give the output I expect, but I can't manage to display the Form as it was during the initialization of 'establishing the connection'. The point is that we only want to start a program when there is connectivity with the router and the end user should also be able to see it that way, not just the end result.
Can anyone point me in the right direction to make the form dynamically show what's happening at the time when the script is running?
Ow, and also the 'Retry' ([System.Windows.Forms.DialogResult]::Retry)function does not do anything as expected... :(
#---------------------------------------------------------[Initialisations]--------------------------------------------------------
# Init PowerShell Gui
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
#---------------------------------------------------------[Form]--------------------------------------------------------
[System.Windows.Forms.Application]::EnableVisualStyles()
$LocalRCForm = New-Object system.Windows.Forms.Form
$LocalRCForm.ClientSize = '480,300'
$LocalRCForm.text = "Status router connection"
$LocalRCForm.BackColor = "#F9F9F9"
$LocalRCForm.StartPosition = "CenterScreen"
$LocalRCForm.TopMost = $true
$LocalRCForm.SuspendLayout()
$Titel = New-Object system.Windows.Forms.Label
$Titel.text = "Status router connection"
$Titel.AutoSize = $true
$Titel.width = 25
$Titel.height = 10
$Titel.location = New-Object System.Drawing.Point(20,20)
$Titel.Font = 'Microsoft Sans Serif,14,style=Bold'
$Description = New-Object system.Windows.Forms.Label
$Description.text = "To start Security Desk, wait for an established connection"
$Description.AutoSize = $false
$Description.width = 450
$Description.height = 50
$Description.location = New-Object System.Drawing.Point(20,60)
$Description.Font = 'Microsoft Sans Serif,10'
$RCStatus = New-Object system.Windows.Forms.Label
$RCStatus.text = "Status:"
$RCStatus.AutoSize = $true
$RCStatus.width = 25
$RCStatus.height = 10
$RCStatus.location = New-Object System.Drawing.Point(20,115)
$RCStatus.Font = 'Microsoft Sans Serif,10,style=Bold'
$RCFound = New-Object system.Windows.Forms.Label
$RCFound.text = "Connecting to router..."
$RCFound.AutoSize = $true
$RCFound.width = 25
$RCFound.height = 10
$RCFound.location = New-Object System.Drawing.Point(100,115)
$RCFound.Font = 'Microsoft Sans Serif,10'
$RCDetails = New-Object system.Windows.Forms.Label
$RCDetails.text = "Router succesfully connected"
$RCDetails.AutoSize = $true
$RCDetails.width = 25
$RCDetails.height = 10
$RCDetails.location = New-Object System.Drawing.Point(20,150)
$RCDetails.Font = 'Microsoft Sans Serif,10,style=Bold'
$RCTestConnection = New-Object system.Windows.Forms.Label
$RCTestConnection.text = "Checking connection"
$RCTestConnection.AutoSize =
$RCTestConnection.width = 25
$RCTestConnection.height = 10
$RCTestConnection.location = New-Object System.Drawing.Point(100,115)
$RCTestConnection.Font = 'Microsoft Sans Serif,10'
$AddRCBtn = New-Object system.Windows.Forms.Button
$AddRCBtn.BackColor = "#ff7b00"
$AddRCBtn.text = "Ok"
$AddRCBtn.width = 150
$AddRCBtn.height = 30
$AddRCBtn.location = New-Object System.Drawing.Point(300,250)
$AddRCBtn.Font = 'Microsoft Sans Serif,10'
$AddRCBtn.ForeColor = "#000"
$AddRCBtn.DialogResult = [System.Windows.Forms.DialogResult]::Yes
$CloseBtn = New-Object system.Windows.Forms.Button
$CloseBtn.BackColor = "#ffffff"
$CloseBtn.text = "Close"
$CloseBtn.width = 90
$CloseBtn.height = 30
$CloseBtn.location = New-Object System.Drawing.Point(300,250)
$CloseBtn.Font = 'Microsoft Sans Serif,10'
$CloseBtn.ForeColor = "#000"
$CloseBtn.DialogResult = [System.Windows.Forms.DialogResult]::Retry
$LocalRCForm.controls.AddRange(#($Titel,$Description,$RCStatus,$RCFound,$RCType,$AddRCBtn,$CloseBtn,$RCDetails,$routerIp,$testConnection))
#-----------------------------------------------------------[Functions]------------------------------------------------------------
function AddRC {
$RCFound.ForeColor = "#000000"
$RCFound.Text = 'Connected to Router'
}
#---------------------------------------------------------[Script]--------------------------------------------------------
# Check if router is online
$routerIp = '1.1.1.1'
$testConnection = Test-Connection $routerIp
If ($testConnection) {
$RCFound.text = "Router Ready"
$RCFound.ForeColor = "#7ed321"
$AddRCBtn.text = "Run the program"
$CloseBtn.Visible = $false
$RCDetails.Visible = $true
}else{
$RCFound.text = "Router not found"
$RCFound.ForeColor = "#D0021B"
$CloseBtn.text = "Close"
$AddRCBtn.Visible = $false
$RCDetails.Visible = $false
}
$AddRCBtn.Add_Click({ AddRC })
$LocalRCForm.Add_Shown({$testConnection.Enabled = $true; $testConnection.Start()})
return $LocalRCForm.ShowDialog()
There is too much to say about your script so let's focus on your main issue:
Your script will only show the form once the connection is tested, so the form will never display the various steps.
To achieve your goal, you should put your main code in a function and run it once the form is displayed.
function funTestConnection {
$testConnection = Test-Connection $routerIp
If($testConnection) {
$RCFound.text = "Router Ready"
$RCFound.ForeColor = "#7ed321"
$AddRCBtn.text = "Run the program"
$CloseBtn.Visible = $false
$RCDetails.Visible = $true
} else {
$RCFound.text = "Router not found"
$RCFound.ForeColor = "#D0021B"
$CloseBtn.text = "Close"
$AddRCBtn.Visible = $false
$RCDetails.Visible = $false
}
}
Also, you are trying to add a control in your form that is not a control ($testConnection). I suppose you wanted to add $RCTestConnection instead (and don't forget to take care of it's position).
# Change this line:
$LocalRCForm.controls.AddRange(#($Titel,$Description,$RCStatus,$RCFound,$RCType,$AddRCBtn,$CloseBtn,$RCDetails,$routerIp,$testConnection))
# To this line:
$LocalRCForm.controls.AddRange(#($Titel,$Description,$RCStatus,$RCFound,$RCType,$AddRCBtn,$CloseBtn,$RCDetails,$routerIp,$RCTestConnection))
Again, you use the variable $testConnection but it neither have a property enabled or a method Start(). So this line makes no sense and after having put your main code in a function, it should be replaced as follow:
# Replace this line that has no meaning
$LocalRCForm.Add_Shown({$testConnection.Enabled = $true; $testConnection.Start()})
# By this line
$LocalRCForm.Add_Shown({funTestConnection})
About retrying if not connected, [System.Windows.Forms.DialogResult]::Retry is not a function, it's a constant value so you can't expect it to do anything.
For it to be taken into account, you should use it in a way or the other.
One example would be like this:
# This code
do { $returnDialog = $LocalRCForm.ShowDialog() }
while($returnDialog -eq [System.Windows.Forms.DialogResult]::Retry)
# instead of this line
return $LocalRCForm.ShowDialog()
Finally, your function AddRC has no real impact as the form is closed when it's called (just a remark).
With all that said, you will have a functional script but it needs quite some adjustments related to what messages and information are displayed during the process (button names, labels, etc.) but at least it should do what you're expecting.
I am trying to open the next page of my Winforms script and atm on first launch the error is get when clicking the next button is "you cannot call a method on a null-valued expression" [void] $Form_UserInformation.ShowDialog() being the issue code here. When I run it the second time it runs fine but this doesn't work for an EXE file as it closes it and doesn't remember the previous session.
# HomePage Form
$Form_HomePage = New-Object System.Windows.Forms.Form
$Form_HomePage.text = "New User Script"
$Form_HomePage.Size = New-Object System.Drawing.Size(400,400)
$Form_HomePage.FormBorderStyle = "FixedDialog"
$Form_HomePage.TopMost = $false
$Form_HomePage.MaximizeBox = $false
$Form_HomePage.MinimizeBox = $true
$Form_HomePage.ControlBox = "true"
$Form_HomePage.StartPosition = "CenterScreen"
$Form_HomePage.Font = "Segoe UI"
# Title label
$label_HomeTitle = New-Object System.Windows.Forms.Label
$label_HomeTitle.Location = New-Object System.Drawing.Size(47,8)
$label_HomeTitle.Size = New-Object System.Drawing.Size(300,42)
$label_HomeTitle.Font = New-Object System.Drawing.Font("Segoe UI",24,[System.Drawing.FontStyle]::Regular)
$label_HomeTitle.TextAlign = "MiddleCenter"
$label_HomeTitle.Text = "Kuehne && Nagel"
$Form_HomePage.Controls.Add($label_HomeTitle)
# Subheading label
$label_HomeSubTitle = New-Object System.Windows.Forms.Label
$label_HomeSubTitle.Location = New-Object System.Drawing.Size(50,60)
$label_HomeSubTitle.Size = New-Object System.Drawing.Size(300,42)
$label_HomeSubTitle.Font = New-Object System.Drawing.Font("Segoe UI",16,[System.Drawing.FontStyle]::Regular)
$label_HomeSubTitle.TextAlign = "MiddleCenter"
$label_HomeSubTitle.Text = "New User Script"
$Form_HomePage.Controls.Add($label_HomeSubTitle)
# Next button
$button_HomeStart = New-Object System.Windows.Forms.Button
$button_HomeStart.Location = New-Object System.Drawing.Size(75,200)
$button_HomeStart.Size = New-Object System.Drawing.Size(240,32)
$button_HomeStart.TextAlign = "MiddleCenter"
$button_HomeStart.Text = "Start"
$button_HomeStart.Add_Click({
[void] $Form_UserInformation.ShowDialog()
$Form_HomePage.Hide()
})
$Form_HomePage.Controls.Add($button_HomeStart)
# About button
$button_About = New-Object System.Windows.Forms.Button
$button_About.Location = New-Object System.Drawing.Size(75,250)
$button_About.Size = New-Object System.Drawing.Size(240,32)
$button_About.TextAlign = "MiddleCenter"
$button_About.Text = "About"
$button_About.Add_Click({
})
$Form_HomePage.Controls.Add($button_About)
# Exit button
$button_HomeExit = New-Object System.Windows.Forms.Button
$button_HomeExit.Location = New-Object System.Drawing.Size(75,300)
$button_HomeExit.Size = New-Object System.Drawing.Size(240,32)
$button_HomeExit.TextAlign = "MiddleCenter"
$button_HomeExit.Text = "Exit"
$button_HomeExit.Add_Click({
$Form_HomePage.Close()
})
$Form_HomePage.Controls.Add($button_HomeExit)
# Show Form
$Form_HomePage.Add_Shown({$Form_HomePage.Activate()})
[void] $Form_HomePage.ShowDialog()
# User Information Form
$Form_UserInformation = New-Object System.Windows.Forms.Form
$Form_UserInformation.text = "New User Script"
$Form_UserInformation.Size = New-Object System.Drawing.Size(400,400)
$Form_UserInformation.FormBorderStyle = "FixedDialog"
$Form_UserInformation.TopMost = $false
$Form_UserInformation.MaximizeBox = $false
$Form_UserInformation.MinimizeBox = $true
$Form_UserInformation.ControlBox = "true"
$Form_UserInformation.StartPosition = "CenterScreen"
$Form_UserInformation.Font = "Segoe UI"
I have been stuck in this for whole day. I am creating a UI using PowerShell forms. In that:
user select an option from 1st combobox. click button Go
Based on the selection, a panel will appear having another combobox.
if user select another option in 1st combobox then another panel appears with another combobox
After selecting options from panel comboboxes, user clicks on start button.
This leads to a function which stores the selected options to a variable.
Problem
Now when user selects the options from the comboboxes of panels, I am using $combobox.selecteditem.Tostring to get the values.
But it gives me NULL result.
Here is my code..
$global:Button1Clicked = 0;
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
[System.Windows.Forms.Application]::EnableVisualStyles()
[System.Windows.Forms.FormClosingEventHandler]
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '1500,800'
$Form.text = "NextUI "
$Form.BackColor = 'White'
$Form.TopMost = $true
$Form.add_closing(
{
if($global:Button2Clicked)
{
$global:Button2Clicked = 0;
$_.cancel = $true;
}
})
$panel1 = New-Object system.Windows.Forms.Panel
$panel1.AutoSize = $true
$panel1.Width = 1200
$panel1.Height = 200
$panel1.location = New-Object System.Drawing.Point(50,100)
$panel1.Visible = $false
$panel1.Controls.Add($label3)
$panel1.Controls.Add($comboBox2)
$panel1.BorderStyle = 1
$panel2 = New-Object system.Windows.Forms.Panel
$panel2.AutoSize = $true
$panel2.Width = 1200
$panel2.Height =200
$panel2.location = New-Object system.Drawing.Point(50,100)
$panel2.Visible = $false
$panel2.Controls.Add($label4)
$panel2.Controls.Add($ComboBox3)
$panel2.BorderStyle = 1
$Label1 = New-Object system.Windows.Forms.Label
$Label1.text = "Select Module"
$Label1.AutoSize = $true
$Label1.location = New-Object System.Drawing.Point(35,50)
$Label1.Font = 'segoe ui,9.5'
$Label2 = New-Object system.Windows.Forms.Label
$Label2.text = "SharePoint Settings"
$Label2.AutoSize = $true
$Label2.location = New-Object System.Drawing.Point(35,15)
$Label2.Font = 'Segoe UI Semibold,9.5'
$Label3 = New-Object system.Windows.Forms.Label
$Label3.text = "Choose file and folder permission option"
$Label3.AutoSize = $true
$Label3.location = New-Object System.Drawing.Point(100,200)
$Label3.Font = 'segoe ui,9.5'
$Label4 = New-Object system.Windows.Forms.Label
$Label4.AutoSize = $True
$Label4.text = "File Permission"
$Label4.location = New-Object System.Drawing.Point(100,200)
$Label4.Font = 'Segoe UI ,9.5'
################ Module combo box ################
$ComboBox1 = New-Object system.Windows.Forms.ComboBox
$ComboBox1.text = "Select Module"
$ComboBox1.width = 200
$ComboBox1.height = 20
$ComboBox1.location = New-Object System.Drawing.Point(310, 45)
$ComboBox1.Font = 'Microsoft Sans Serif,10'
$combobox1.items.Add("ControlSettings")
$combobox1.items.Add("NextSettings")
######## file and folder permission #############
$ComboBox2 = New-Object system.Windows.Forms.ComboBox
$ComboBox2.text = "select an option"
$ComboBox2.width = 200
$ComboBox2.height = 20
$ComboBox2.location = New-Object System.Drawing.Point(450, 200)
$ComboBox2.Font = 'Microsoft Sans Serif,10'
$ComboBox2.items.Add("View")
$ComboBox2.items.Add("Edit")
$ComboBox3 = New-Object system.Windows.Forms.ComboBox
$ComboBox3.text = "select an option"
$ComboBox3.width = 200
$ComboBox3.height = 20
$ComboBox3.location = New-Object System.Drawing.Point(450, 200)
$ComboBox3.Font = 'Microsoft Sans Serif,10'
$ComboBox3.items.Add("View")
$ComboBox3.items.Add("Edit")
Function Button2_Click()
{
if ($ComboBox1.SelectedIndex -eq 0)
{
$panel2.Visible = $true
$panel1.Visible = $false
}
if ($ComboBox1.SelectedIndex -eq 1)
{
$panel1.Visible = $true
$panel2.Visible = $false
}
}
$Button1 = New-Object system.Windows.Forms.Button
$Button1.text = "Start"
$Button1.width = 150
$Button1.height = 30
$Button1.BackColor = '#F6CEE3'
$Button1.DialogResult = [System.Windows.Forms.DialogResult]::OK
$Button1.location = New-Object System.Drawing.Point(500, 700)
$Button1.Font = 'segoe ui,10'
$Button1.Add_Click({
Button1_Click;
$global:Button1Clicked = 1;
})
############# Button 'Go' #############
$Button2 = New-Object system.Windows.Forms.Button
$Button2.text = "Go"
$Button2.width = 100
$Button2.height = 30
$Button2.BackColor = '#F6CEE3'
$Button2.DialogResult = [System.Windows.Forms.DialogResult]::OK
$Button2.location = New-Object System.Drawing.Point(680, 43)
$Button2.Font = 'segoe ui,10'
$Button2.Add_Click({
Button2_Click;
$global:Button2Clicked = 1;
})
######### code Starts ###########
function Button1_Click()
{
$link = $comboBox2.SelectedItem.ToString();
$linktype = $comboBox3.SelectedItem.ToString();
}
####### end of code ######
$form.Controls.Add($Panel1)
$form.Controls.Add($Panel2)
$form.Controls.Add($button1)
$form.Controls.Add($comboBox1)
$form.Controls.Add($button2)
$form.Controls.Add($pictureBox1)
$form.Controls.Add($label1)
$form.Controls.Add($label2)
[void]$Form.Add_Shown({ $Form.Activate() })
[void]$Form.ShowDialog()
The issue is that your variables are being set in a function, so they are scoped to that function. There's really no reason to put that in a function, put it directly in the .add_click() instead. Or if you feel that you need to keep it in the function you can scope the variable like you did with $global:ButtonClicked and set them to $global:Link and $global:LinkType.
Edit: Order does make a difference in PowerShell, so I did move some stuff around in your script to make it work right, but I was able to get values to reflect fine when I put them in the global scope.
$global:Button1Clicked = 0;
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
[System.Windows.Forms.Application]::EnableVisualStyles()
[System.Windows.Forms.FormClosingEventHandler]
######### code Starts ###########
function Button1_Click()
{
write-host "Link = $global:link"
write-host "Link Type = $global:linktype"
}
Function Button2_Click()
{
if ($ComboBox1.SelectedIndex -eq 0)
{
$panel2.Visible = $true
$panel1.Visible = $false
}
if ($ComboBox1.SelectedIndex -eq 1)
{
$panel1.Visible = $true
$panel2.Visible = $false
}
}
####### end of code ######
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '1500,800'
$Form.text = "NextUI "
$Form.BackColor = 'White'
$Form.TopMost = $true
$Form.add_closing(
{
if($global:Button2Clicked)
{
$global:Button2Clicked = 0;
$_.cancel = $true;
}
})
################ Module combo box ################
$ComboBox1 = New-Object system.Windows.Forms.ComboBox
$ComboBox1.text = "Select Module"
$ComboBox1.width = 200
$ComboBox1.height = 20
$ComboBox1.location = New-Object System.Drawing.Point(310, 45)
$ComboBox1.Font = 'Microsoft Sans Serif,10'
$combobox1.items.Add("ControlSettings")
$combobox1.items.Add("NextSettings")
######## file and folder permission #############
$ComboBox2 = New-Object system.Windows.Forms.ComboBox
$ComboBox2.text = "select an option"
$ComboBox2.width = 200
$ComboBox2.height = 20
$ComboBox2.location = New-Object System.Drawing.Point(450, 200)
$ComboBox2.Font = 'Microsoft Sans Serif,10'
$ComboBox2.items.AddRange(#("View","Edit") )
#$ComboBox2.items.Add("Edit")
$ComboBox3 = New-Object system.Windows.Forms.ComboBox
$ComboBox3.text = "select an option"
$ComboBox3.width = 200
$ComboBox3.height = 20
$ComboBox3.location = New-Object System.Drawing.Point(450, 200)
$ComboBox3.Font = 'Microsoft Sans Serif,10'
$ComboBox3.items.Add("View")
$ComboBox3.items.Add("Edit")
$panel1 = New-Object system.Windows.Forms.Panel
$panel1.AutoSize = $true
$panel1.Width = 1200
$panel1.Height = 200
$panel1.location = New-Object System.Drawing.Point(50,100)
$panel1.Visible = $false
$panel1.Controls.Add($label3)
$panel1.Controls.Add($comboBox2)
$panel1.BorderStyle = 1
$panel2 = New-Object system.Windows.Forms.Panel
$panel2.AutoSize = $true
$panel2.Width = 1200
$panel2.Height =200
$panel2.location = New-Object system.Drawing.Point(50,100)
$panel2.Visible = $false
$panel2.Controls.Add($label4)
$panel2.Controls.Add($ComboBox3)
$panel2.BorderStyle = 1
$Label1 = New-Object system.Windows.Forms.Label
$Label1.text = "Select Module"
$Label1.AutoSize = $true
$Label1.location = New-Object System.Drawing.Point(35,50)
$Label1.Font = 'segoe ui,9.5'
$Label2 = New-Object system.Windows.Forms.Label
$Label2.text = "SharePoint Settings"
$Label2.AutoSize = $true
$Label2.location = New-Object System.Drawing.Point(35,15)
$Label2.Font = 'Segoe UI Semibold,9.5'
$Label3 = New-Object system.Windows.Forms.Label
$Label3.text = "Choose file and folder permission option"
$Label3.AutoSize = $true
$Label3.location = New-Object System.Drawing.Point(100,200)
$Label3.Font = 'segoe ui,9.5'
$Label4 = New-Object system.Windows.Forms.Label
$Label4.AutoSize = $True
$Label4.text = "File Permission"
$Label4.location = New-Object System.Drawing.Point(100,200)
$Label4.Font = 'Segoe UI ,9.5'
$Button1 = New-Object system.Windows.Forms.Button
$Button1.text = "Start"
$Button1.width = 150
$Button1.height = 30
$Button1.BackColor = '#F6CEE3'
$Button1.DialogResult = [System.Windows.Forms.DialogResult]::OK
$Button1.location = New-Object System.Drawing.Point(500, 700)
$Button1.Font = 'segoe ui,10'
$Button1.Add_Click({
$global:link = $comboBox2.SelectedItem.ToString();
$global:linktype = $comboBox3.SelectedItem.ToString();
Button1_Click;
$global:Button1Clicked = 1;
})
############# Button 'Go' #############
$Button2 = New-Object system.Windows.Forms.Button
$Button2.text = "Go"
$Button2.width = 100
$Button2.height = 30
$Button2.BackColor = '#F6CEE3'
$Button2.DialogResult = [System.Windows.Forms.DialogResult]::OK
$Button2.location = New-Object System.Drawing.Point(680, 43)
$Button2.Font = 'segoe ui,10'
$Button2.Add_Click({
Button2_Click;
$global:Button2Clicked = 1;
})
$form.Controls.Add($Panel1)
$form.Controls.Add($Panel2)
$form.Controls.Add($button1)
$form.Controls.Add($comboBox1)
$form.Controls.Add($button2)
$form.Controls.Add($pictureBox1)
$form.Controls.Add($label1)
$form.Controls.Add($label2)
[void]$Form.Add_Shown({ $Form.Activate() })
[void]$Form.ShowDialog()
When I ran that, set the two combobox values, and clicked Start I got text in the ISE saying what I set the Link and LinkType variables to, and was able to echo $global:link and $global:linktype to see the values correctly assigned.
I have built a powershell script using the GUI .net framework that provides the user with a graphical interface to add alternate data streams (ADS) to files on a NTFS file system.
Below is the code I wrote for the powershell script:
<#
This script is a GUI featured way to add extended attributes to files
#>
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
#region begin GUI{
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '600,600'
$Form.text = "Add Extended Attributes"
$Form.TopMost = $false
# Add Extended Attributes Label
$mainLabel = New-Object system.Windows.Forms.Label
$mainLabel.text = "Add Extended Attributes"
$mainLabel.AutoSize = $true
$mainLabel.width = 25
$mainLabel.height = 10
$mainLabel.location = New-Object System.Drawing.Point(180,10)
$mainLabel.Font = 'Microsoft Sans Serif,18'
# text box for entering file path
$filePath = New-Object system.Windows.Forms.TextBox
$filePath.multiline = $false
$filePath.width = 300
$filePath.height = 20
$filePath.location = New-Object System.Drawing.Point(200,80)
$filePath.Font = 'Microsoft Sans Serif,10'
# label for the file path text box "File Path: "
$FilePathLabel = New-Object system.Windows.Forms.Label
$FilePathLabel.text = "File Path: "
$FilePathLabel.AutoSize = $true
$FilePathLabel.width = 25
$FilePathLabel.height = 10
$FilePathLabel.location = New-Object System.Drawing.Point(80,85)
$FilePathLabel.Font = 'Microsoft Sans Serif,10'
# Attributes Label
$idLabel = New-Object system.Windows.Forms.Label
$idLabel.text = "Attributes"
$idLabel.AutoSize = $true
$idLabel.width = 25
$idLabel.height = 10
$idLabel.location = New-Object System.Drawing.Point(80,150)
$idLabel.Font = 'Microsoft Sans Serif,12'
# Values Label
$valueLabel = New-Object system.Windows.Forms.Label
$valueLabel.text = "Value"
$valueLabel.AutoSize = $true
$valueLabel.width = 25
$valueLabel.height = 10
$valueLabel.location = New-Object System.Drawing.Point(300,150)
$valueLabel.Font = 'Microsoft Sans Serif,12'
# Checkbox for ID attribute
$fileId = New-Object System.Windows.Forms.CheckBox
$fileId.text = "Id"
$fileId.AutoSize = $true
$fileId.width = 104
$fileId.height = 20
$fileId.location = New-Object System.Drawing.Point(100,200)
$fileId.Font = 'Microsoft Sans Serif,10'
# Label for the ID checkbox
$idValue = New-Object system.Windows.Forms.TextBox
$idValue.multiline = $false
$idValue.width = 300
$idValue.height = 20
$idValue.location = New-Object System.Drawing.Point(202,200)
$idValue.Font = 'Microsoft Sans Serif,10'
# Checkbox for Description attribute
$description = New-Object System.Windows.Forms.CheckBox
$description.text = "Description"
$description.AutoSize = $true
$description.width = 104
$description.height = 20
$description.location = New-Object System.Drawing.Point(100,250)
$description.Font = 'Microsoft Sans Serif,10'
# Label for the Description checkbox
$descriptionValue = New-Object system.Windows.Forms.TextBox
$descriptionValue.multiline = $false
$descriptionValue.width = 300
$descriptionValue.height = 20
$descriptionValue.location = New-Object System.Drawing.Point(202,250)
$descriptionValue.Font = 'Microsoft Sans Serif,10'
# Checkbox for Type attribute
$type = New-Object System.Windows.Forms.CheckBox
$type.text = "Type"
$type.AutoSize = $true
$type.width = 104
$type.height = 20
$type.location = New-Object System.Drawing.Point(100,300)
$type.Font = 'Microsoft Sans Serif,10'
# Label for the type checkbox
$typeValue = New-Object system.Windows.Forms.TextBox
$typeValue.multiline = $false
$typeValue.width = 300
$typeValue.height = 20
$typeValue.location = New-Object System.Drawing.Point(202,300)
$typeValue.Font = 'Microsoft Sans Serif,10'
# Checkbox for silo attribute
$silo = New-Object System.Windows.Forms.CheckBox
$silo.text = "Silo"
$silo.AutoSize = $true
$silo.width = 104
$silo.height = 20
$silo.location = New-Object System.Drawing.Point(100,350)
$silo.Font = 'Microsoft Sans Serif,10'
# Label for the silo checkbox
$siloValue = New-Object system.Windows.Forms.TextBox
$siloValue.multiline = $false
$siloValue.width = 300
$siloValue.height = 20
$siloValue.location = New-Object System.Drawing.Point(202,350)
$siloValue.Font = 'Microsoft Sans Serif,10'
# submitt button
$button = New-Object System.Windows.Forms.Button
$button.text = "Submit"
$button.AutoSize = $true
$button.location = New-Object System.Drawing.Point(250,500)
$button.Font = 'Microsoft Sans Serif,10'
$Form.controls.AddRange(#($mainLabel, $fileId,$filePath,$idLabel,$valueLabel,$FilePathLabel,$idValue,$descriptionValue,$description, $type, $typeValue, $silo, $siloValue, $button))
#region gui events {
function SubmitForm(){
if($fileId.checked -eq $true){
sc -path $filePath.Text -stream $fileId.text -value $idValue.Text
}
if($description.checked -eq $true){
sc -path $filePath.Text -stream $description.text -value $descriptionValue.text
}
if($type.checked -eq $true){
sc -path $filePath.Text -stream $type.text -value $typeValue.text
}
if($silo.checked -eq $true){
sc -path $filePath.Text -stream $silo.text -value $siloValue.text
}
[System.Windows.Forms.MessageBox]::Show("Successfully Added Attributes")
}
#Add Button event
$Button.Add_Click({SubmitForm})
#endregion events }
#endregion GUI }
# logic here
[void]$Form.ShowDialog()
Currently the user would have to actually run the powershell script from the root folder and then add the file path to the text input the GUI, along with the rest of the extended attributes. An example of what I currently have is below:
I would like the user to be able to right click on any file and have the form come up with the path of the file that was right clicked in windows explorer, instead of the path being manually entered by the individual making the updates to the alternate data streams. Something similar as how you would extract a file using zip7 (example below).
Can someone tell me if this is even possible? Should I be trying to tackle this problem in another language than using powershell?
You can do all this with Powershell.
First you want to create a script from your code and make input parameter for chosen folder. Like so:
param($FileName)
<#
This script is a GUI featured way to add extended attributes to files
#>
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
#region begin GUI{
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '600,600'
$Form.text = "Add Extended Attributes"
$Form.TopMost = $false
# Add Extended Attributes Label
$mainLabel = New-Object system.Windows.Forms.Label
$mainLabel.text = "Add Extended Attributes"
$mainLabel.AutoSize = $true
$mainLabel.width = 25
$mainLabel.height = 10
$mainLabel.location = New-Object System.Drawing.Point(180,10)
$mainLabel.Font = 'Microsoft Sans Serif,18'
# text box for entering file path
$filePath = New-Object system.Windows.Forms.TextBox
$filePath.multiline = $false
$filePath.width = 300
$filePath.height = 20
$filePath.location = New-Object System.Drawing.Point(200,80)
$filePath.Font = 'Microsoft Sans Serif,10'
$filePath.Text = $FileName
# label for the file path text box "File Path: "
$FilePathLabel = New-Object system.Windows.Forms.Label
$FilePathLabel.text = "File Path: "
$FilePathLabel.AutoSize = $true
$FilePathLabel.width = 25
$FilePathLabel.height = 10
$FilePathLabel.location = New-Object System.Drawing.Point(80,85)
$FilePathLabel.Font = 'Microsoft Sans Serif,10'
# Attributes Label
$idLabel = New-Object system.Windows.Forms.Label
$idLabel.text = "Attributes"
$idLabel.AutoSize = $true
$idLabel.width = 25
$idLabel.height = 10
$idLabel.location = New-Object System.Drawing.Point(80,150)
$idLabel.Font = 'Microsoft Sans Serif,12'
# Values Label
$valueLabel = New-Object system.Windows.Forms.Label
$valueLabel.text = "Value"
$valueLabel.AutoSize = $true
$valueLabel.width = 25
$valueLabel.height = 10
$valueLabel.location = New-Object System.Drawing.Point(300,150)
$valueLabel.Font = 'Microsoft Sans Serif,12'
# Checkbox for ID attribute
$fileId = New-Object System.Windows.Forms.CheckBox
$fileId.text = "Id"
$fileId.AutoSize = $true
$fileId.width = 104
$fileId.height = 20
$fileId.location = New-Object System.Drawing.Point(100,200)
$fileId.Font = 'Microsoft Sans Serif,10'
# Label for the ID checkbox
$idValue = New-Object system.Windows.Forms.TextBox
$idValue.multiline = $false
$idValue.width = 300
$idValue.height = 20
$idValue.location = New-Object System.Drawing.Point(202,200)
$idValue.Font = 'Microsoft Sans Serif,10'
# Checkbox for Description attribute
$description = New-Object System.Windows.Forms.CheckBox
$description.text = "Description"
$description.AutoSize = $true
$description.width = 104
$description.height = 20
$description.location = New-Object System.Drawing.Point(100,250)
$description.Font = 'Microsoft Sans Serif,10'
# Label for the Description checkbox
$descriptionValue = New-Object system.Windows.Forms.TextBox
$descriptionValue.multiline = $false
$descriptionValue.width = 300
$descriptionValue.height = 20
$descriptionValue.location = New-Object System.Drawing.Point(202,250)
$descriptionValue.Font = 'Microsoft Sans Serif,10'
# Checkbox for Type attribute
$type = New-Object System.Windows.Forms.CheckBox
$type.text = "Type"
$type.AutoSize = $true
$type.width = 104
$type.height = 20
$type.location = New-Object System.Drawing.Point(100,300)
$type.Font = 'Microsoft Sans Serif,10'
# Label for the type checkbox
$typeValue = New-Object system.Windows.Forms.TextBox
$typeValue.multiline = $false
$typeValue.width = 300
$typeValue.height = 20
$typeValue.location = New-Object System.Drawing.Point(202,300)
$typeValue.Font = 'Microsoft Sans Serif,10'
# Checkbox for silo attribute
$silo = New-Object System.Windows.Forms.CheckBox
$silo.text = "Silo"
$silo.AutoSize = $true
$silo.width = 104
$silo.height = 20
$silo.location = New-Object System.Drawing.Point(100,350)
$silo.Font = 'Microsoft Sans Serif,10'
# Label for the silo checkbox
$siloValue = New-Object system.Windows.Forms.TextBox
$siloValue.multiline = $false
$siloValue.width = 300
$siloValue.height = 20
$siloValue.location = New-Object System.Drawing.Point(202,350)
$siloValue.Font = 'Microsoft Sans Serif,10'
# submitt button
$button = New-Object System.Windows.Forms.Button
$button.text = "Submit"
$button.AutoSize = $true
$button.location = New-Object System.Drawing.Point(250,500)
$button.Font = 'Microsoft Sans Serif,10'
$Form.controls.AddRange(#($mainLabel, $fileId,$filePath,$idLabel,$valueLabel,$FilePathLabel,$idValue,$descriptionValue,$description, $type, $typeValue, $silo, $siloValue, $button))
#region gui events {
function SubmitForm(){
if($fileId.checked -eq $true){
sc -path $filePath.Text -stream $fileId.text -value $idValue.Text
}
if($description.checked -eq $true){
sc -path $filePath.Text -stream $description.text -value $descriptionValue.text
}
if($type.checked -eq $true){
sc -path $filePath.Text -stream $type.text -value $typeValue.text
}
if($silo.checked -eq $true){
sc -path $filePath.Text -stream $silo.text -value $siloValue.text
}
[System.Windows.Forms.MessageBox]::Show("Successfully Added Attributes")
}
#Add Button event
$Button.Add_Click({SubmitForm})
#endregion events }
#endregion GUI }
# logic here
[void]$Form.ShowDialog()
Next you would need to create registry reference for context menu item and powershell script according to it. Like so:
New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT
New-Item HKCR:\directory\shell\PowerShellScript
New-Item HKCR:\directory\shell\PowerShellScript\command
Set-ItemProperty 'HKCR:\directory\shell\PowerShellScript\command' -Name '(default)' -Value 'Powershell -WindowStyle Hidden -ExecutionPolicy Bypass -NoExit -File "C:\Test.ps1" "%L"'
Context menu item:
Chosen directory passed to script's input parameter: