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()
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 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()
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()
I got a problem with PowerShell and its IF-Cmdlet.
It can also be the TextBox where I want to input a variable to set a default text. In either way it doesn't work as intended. What it should do exactly is described im the Code^^
Thanks to all people helping me out^^
It is not for work or anything...its just a little project I try^^
Oh...and sorry for my bad English(maybe), I'm from Germany.
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
function windowInstall1()
{
$window = New-Object System.Windows.Forms.Form
$window.Width = 550
$window.Height = 600
$window.Text = "TimeStampProgram Installer"
#Adding a Label(Header)
$Label1 = New-Object System.Windows.Forms.Label
$Label1.Location = New-Object System.Drawing.Size(10,10)
$Label1.Text = "Welcome to the TimeStamp Installation Setup"
$Label1.Size = New-Object System.Drawing.Size(450,40)
$Label1.Font = New-Object System.Drawing.Font("Lucidia Console" , 15, [System.Drawing.FontStyle]::Regular)
$window.Controls.Add($Label1)
#Adding a Label
$Label2 = New-Object System.Windows.Forms.Label
$Label2.Location = New-Object System.Drawing.Size(10,50)
$Label2.Text = ("To Continue the Installation please press " + '"' + "Continue" + '"')
$Label2.Size = New-Object System.Drawing.Size(450,20)
$Label2.Font = New-Object System.Drawing.Font("Lucidia Console" , 10, [System.Drawing.FontStyle]::Regular)
$window.Controls.Add($Label2)
#Adding a Label
$Label3 = New-Object System.Windows.Forms.Label
$Label3.Location = New-Object System.Drawing.Size(10,71)
$Label3.Text = ("Else press " + '"' + "Exit" + '"' + " to cancel the installation")
$Label3.Size = New-Object System.Drawing.Size(450,20)
$Label3.Font = New-Object System.Drawing.Font("Lucidia Console" , 10, [System.Drawing.FontStyle]::Regular)
$window.Controls.Add($Label3)
#Adding a button
$windowButton1 = New-Object System.Windows.Forms.Button
$windowButton1.Location = New-Object System.Drawing.Size(100,500)
$windowButton1.Size = New-Object System.Drawing.Size(75,50)
$windowButton1.Text = "Continue"
$windowButton1.Add_Click({
$global:status1 = "true"
$window.Dispose()
windowInstall2
})
$window.Controls.Add($windowButton1)
#Adding a button
$windowButton2 = New-Object System.Windows.Forms.Button
$windowButton2.Location = New-Object System.Drawing.Size(300,500)
$windowButton2.Size = New-Object System.Drawing.Size(75,50)
$windowButton2.Text = "Exit"
$windowButton2.Add_Click({
$global:status1 = "false"
$window.Dispose()
})
$window.Controls.Add($windowButton2)
[void]$window.ShowDialog((New-Object System.Windows.Forms.Form -Property #{TopMost = $true }))
}
function windowInstall2()
{
$window = New-Object System.Windows.Forms.Form
$window.Width = 550
$window.Height = 600
$window.Text = "TimeStampProgram Installer"
#Adding a Label(Header)
$Label1 = New-Object System.Windows.Forms.Label
$Label1.Location = New-Object System.Drawing.Size(10,10)
$Label1.Text = "Choose your Program"
$Label1.Size = New-Object System.Drawing.Size(450,40)
$Label1.Font = New-Object System.Drawing.Font("Lucidia Console" , 15, [System.Drawing.FontStyle]::Regular)
$window.Controls.Add($Label1)
#Adding RadioButtons in a GroupBox
$radioButton1 = New-Object System.Windows.Forms.RadioButton
$radioButton2 = New-Object System.Windows.Forms.RadioButton
$groupBox1 = New-Object System.Windows.Forms.GroupBox
$groupBox1.Controls.AddRange(
#(
$radioButton1,
$radioButton2
))
$groupBox1.Location = New-Object System.Drawing.Point(10, 60)
$groupBox1.Name = 'groupBox'
$groupBox1.Size = New-Object System.Drawing.Size(160, 100)
$groupBox1.Text = 'Programms'
# radioButton1
$radioButton1.Location = New-Object System.Drawing.Point(8, 32)
$radioButton1.Name = 'Button1'
$radioButton1.Text = 'TimesStamp'
$radioButton1.Size = New-Object System.Drawing.Size(150, 20)
# radioButton2
$radioButton2.Location = New-Object System.Drawing.Point(8, 64)
$radioButton2.Name = 'Button2'
$radioButton2.Text = 'TimeStamp with Text'
$radioButton2.Size = New-Object System.Drawing.Size(150, 20)
$window.Controls.Add($groupBox1)
#Adding a Button
$windowButton1 = New-Object System.Windows.Forms.Button
$windowButton1.Location = New-Object System.Drawing.Size(100,500)
$windowButton1.Size = New-Object System.Drawing.Size(75,50)
$windowButton1.Text = "Continue"
$windowButton1.Add_Click({
$global:status2 = "true"
$window.Dispose()
windowInstall3
})
$window.Controls.Add($windowButton1)
#Adding a Button
$windowButton2 = New-Object System.Windows.Forms.Button
$windowButton2.Location = New-Object System.Drawing.Size(300,500)
$windowButton2.Size = New-Object System.Drawing.Size(75,50)
$windowButton2.Text = "Exit"
$windowButton2.Add_Click({
$global:status2 = "false"
$window.Dispose()
})
$window.Controls.Add($windowButton2)
[void]$window.ShowDialog((New-Object System.Windows.Forms.Form -Property #{TopMost = $true }))
$status2
}
function folderDialog()
{
#Choose a Folder
$ChooseFolder = New-Object System.Windows.Forms.FolderBrowserDialog
$ChooseFolder.Description = 'Select the Saving Location Folder'
$ChooseFolder.ShowDialog((New-Object System.Windows.Forms.Form -Property #{TopMost = $true }))
$global:tempDir = $ChooseFolder.SelectedPath
}
function windowInstall3()
{
$window = New-Object System.Windows.Forms.Form
$window.Width = 550
$window.Height = 600
$window.Text = "TimeStampProgram Installer"
#Another Label(Header)
$Label1 = New-Object System.Windows.Forms.Label
$Label1.Location = New-Object System.Drawing.Size(10,10)
$Label1.Text = "Setup Install Location"
$Label1.Size = New-Object System.Drawing.Size(450,40)
$Label1.Font = New-Object System.Drawing.Font("Lucidia Console" , 15, [System.Drawing.FontStyle]::Regular)
$window.Controls.Add($Label1)
#Another Label
$Label2 = New-Object System.Windows.Forms.Label
$Label2.Location = New-Object System.Drawing.Size(10,80)
$Label2.Text = "Choose the Folder where the Installation should take place"
$Label2.Size = New-Object System.Drawing.Size(450,40)
$Label2.Font = New-Object System.Drawing.Font("Lucidia Console" , 10, [System.Drawing.FontStyle]::Regular)
$window.Controls.Add($Label2)
#Here is where I have Problems
#If $tempDir is empty it should put a default Path
#If not it should use $tempDir
$windowTextBox1 = New-Object System.Windows.Forms.TextBox
$windowTextBox1.Location = New-Object System.Drawing.Size(10,130)
$windowTextBox1.Size = New-Object System.Drawing.Size(300,150)
if($tempDir = "")
{
$windowTextBox1.Text = "C:\Program Files (x86)"
}
else
{
$windowTextBox1.Text = $tempDir
}
$window.Controls.Add($windowTextBox1)
$windowButton1 = New-Object System.Windows.Forms.Button
$windowButton1.Location = New-Object System.Drawing.Size(100,500)
$windowButton1.Size = New-Object System.Drawing.Size(75,50)
$windowButton1.Text = "Continue"
$windowButton1.Add_Click({
$global:status3 = "true"
$window.Dispose()
})
$window.Controls.Add($windowButton1)
#Add another Button
$windowButton2 = New-Object System.Windows.Forms.Button
$windowButton2.Location = New-Object System.Drawing.Size(300,500)
$windowButton2.Size = New-Object System.Drawing.Size(75,50)
$windowButton2.Text = "Exit"
$windowButton2.Add_Click({
$global:status3 = "false"
$window.Dispose()
})
$window.Controls.Add($windowButton2)
[void]$window.ShowDialog((New-Object System.Windows.Forms.Form -Property #{TopMost = $true }))
$status3
}
windowInstall1
The equal sign here is incorrect.
= is used for variable assignment
You should be using the -eq operator instead.
Correct
if ($tempDir -eq "") {
$windowTextBox1.Text = "C:\Program Files (x86)"
}
else {
$windowTextBox1.Text = $tempDir
}
Incorrect
# This is not a valid IF condition
if ($tempDir = "") {
$windowTextBox1.Text = "C:\Program Files (x86)"
}
else {
$windowTextBox1.Text = $tempDir
}
Additionally, if your $tempdir variable is $null instead of an empty script, this will be seen as if $tempdir is correctly populated. To cover both an empty string and a $null value, you can use [String]::IsNullOrEmpty($tempdir) in your condition statement.
I have some ps script which is runs OK via PowerShell ISE env.
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Data Entry Form"
$objForm.Size = New-Object System.Drawing.Size(600,400)
$objForm.StartPosition = "CenterScreen"
$objTextBox = New-Object System.Windows.Forms.TextBox
$objTextBox.Location = New-Object System.Drawing.Size(50,50)
$objTextBox.Size = New-Object System.Drawing.Size(150,20)
$findLabel = New-Object System.Windows.Forms.Label
$findLabel.Location = New-Object System.Drawing.Size(50,100)
$findLabel.Size = New-Object System.Drawing.Size(300,20)
$findLabel.Text = "Nađi:"
$findBox = New-Object System.Windows.Forms.TextBox
$findBox.Location = New-Object System.Drawing.Size(50,120)
$findBox.Size = New-Object System.Drawing.Size(260,20)
$replaceLabel = New-Object System.Windows.Forms.Label
$replaceLabel.Location = New-Object System.Drawing.Size(50,180)
$replaceLabel.Size = New-Object System.Drawing.Size(300,20)
$replaceLabel.Text = "Zameni sa:"
$replaceBox = New-Object System.Windows.Forms.TextBox
$replaceBox.Location = New-Object System.Drawing.Size(50,200)
$replaceBox.Size = New-Object System.Drawing.Size(260,20)
$objForm.Controls.Add($startButton)
$objForm.Controls.Add($findBox)
$objForm.Controls.Add($replaceBox)
$objForm.Controls.Add($objTextBox)
$objForm.Controls.Add($beginScriptButton)
$objForm.Controls.Add($findLabel)
$objForm.Controls.Add($replaceLabel)
$objForm.Topmost = $True
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
$startButton = New-Object System.Windows.Forms.Button
$startButton.Location = New-Object System.Drawing.Size(220,50)
$startButton.Size = New-Object System.Drawing.Size(75,23)
$startButton.Text = "Browse!"
$startButton.Add_Click({
$browser = New-Object System.Windows.Forms.FolderBrowserDialog
$browser.Location = New-Object System.Drawing.Size(60,60)
$null = $browser.ShowDialog()
$path = $browser.SelectedPath
$objTextBox.Text = $path
})
$beginScriptButton = New-Object System.Windows.Forms.Button
$beginScriptButton.Location = New-Object System.Drawing.Size(350,130)
$beginScriptButton.Size = New-Object System.Drawing.Size(350,180)
$beginScriptButton.Text = "Begin"
$beginScriptButton.Add_Click({
$a = $objTextBox.Text
if(($a) -and ($findBox.Text) -and ($replaceBox.Text)){
$objWord = New-Object -comobject Word.Application
$objWord.Visible = $false
$list = Get-ChildItem "c:\users\stefan\test\*.*" -Include *.doc*
foreach($item in $list){
$objDoc = $objWord.Documents.Open($item.FullName,$true)
$objSelection = $objWord.Selection
$wdFindContinue = 1
$FindText = $findBox.Text
$MatchCase = $false
$MatchWholeWord = $true
$MatchWildcards = $False
$MatchSoundsLike = $False
$MatchAllWordForms = $False
$Forward = $True
$Wrap = $wdFindContinue
$Format = $False
$wdReplaceNone = 0
$ReplaceWith = $replaceBox.Text
$wdFindContinue = 1
$ReplaceAll = 2
$a = $objSelection.Find.Execute($FindText,$MatchCase,$MatchWholeWord, `
$MatchWildcards,$MatchSoundsLike,$MatchAllWordForms,$Forward,`
$Wrap,$Format,$ReplaceWith,$ReplaceAll)
$objDoc.Save()
$objDoc.Close()
}
$objWord.Quit()
[System.Windows.Forms.MessageBox]::Show("Uspešno ste izvršili izmenu!")
}
else{
[System.Windows.Forms.MessageBox]::Show("Please fill in all fields.")
}
})
Then, I compile it via PowerGUI as .exe
And I run it, first, it opens a cmd and after a second it disappear, I can't even see my powershell form that I've created through the code.
I've tried different options through PowerGui (I want to not be displayed a cmd window, just my form). Do you know some way how to compile it to .exe and to see only form and logic behind it?
Thanks!
Compiling the script as an exe in Powershell runs it in a new instance, where you'll need to load the form components. Try adding this to the start of your script:
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")