none
Designing Simple GUI for a Powershell Script RRS feed

  • Question

  • Hello Everyone,

    I am glad that I got this opportunity to be a part of this supreme community and I truly realize the Wisdom of knowledge out here. With the help of many superiors out here I finally designed my Inventory Processing System Powershell Script

    #---------------------Clearing the Directory ----------------------------------
    $Folder=Get-ChildItem D:\Lzero\Input_1\*.csv -Recurse
    foreach($file in $Folder)
    {
    Remove-Item $file -Force -Recurse
    }
    $Folder2=Get-ChildItem D:\Lzero\Output\*.csv -Recurse
    foreach($file2 in $Folder2)
    {
    Remove-Item $file2 -Force -Recurse
    }
    #------------------------Input of Main Inventory * Start of basic Numeral Conversion------------------------------
    $MainInventory = Import-Csv -Path D:\Lzero\Input\Main.csv | sort Environment 
    $Main| foreach-object {
    $_.day= $_.day.replace("sunday","0")
    $_.day= $_.day.replace("monday","1")
    $_.day= $_.day.replace("tuesday","2")
    $_.day= $_.day.replace("wednesday","3")
    $_.day= $_.day.replace("thursday","4")
    $_.day= $_.day.replace("friday","5")
    $_.day= $_.day.replace("saturday","6")
    $_.day= $_.day.replace("Sunday","0")
    $_.day= $_.day.replace("Monday","1")
    $_.day= $_.day.replace("Tuesday","2")
    $_.day= $_.day.replace("Wednesday","3")
    $_.day= $_.day.replace("Thursday","4")
    $_.day= $_.day.replace("Friday","5")
    $_.day= $_.day.replace("Saturday","6")
    $_.Month=$_.Month.replace("January","1")
    $_.Month=$_.Month.replace("February","2")
    $_.Month=$_.Month.replace("March","3")
    $_.Month=$_.Month.replace("April","4")
    $_.Month=$_.Month.replace("May","5")
    $_.Month=$_.Month.replace("June","6")
    $_.Month=$_.Month.replace("July","7")
    $_.Month=$_.Month.replace("August","8")
    $_.Month=$_.Month.replace("September","9")
    $_.Month=$_.Month.replace("October","10")
    $_.Month=$_.Month.replace("November","11")
    $_.Month=$_.Month.replace("December","12")
    $_.Time =$_.Time.replace(":",".-")
    }
    $MainInventory| export-csv D:\Lzero\Input_1\inventory_pass1.csv -notypeinformation 
    #---------------------------------------End Of Numeral Conversion--------------------------------------------------
    #-----------------------------------------Date Generation Logic----------------------------------------------------
    Function Get-WeekDayInMonth () 
    {
    [CmdletBinding()]
    Param(
    [Parameter(Mandatory=$True,ValueFromPipelineByPropertyName=$true)]
    [int]$Month,
    [Parameter(Mandatory=$True,ValueFromPipelineByPropertyName=$true)]
    [int]$WeekNumber,
    [Parameter(Mandatory=$True,ValueFromPipelineByPropertyName=$true)]
    [int]$WeekDay,
    ......and so on it performs its operations on an input CSV file

    My Question is by any chance is it possible to design a GUI simple one to input my Main File like I am doing in the code ..

    $MainInventory = Import-Csv -Path D:\Lzero\Input\Main.csv

    and then click a Run Button that would do all the task behind.

    It might look like :

    A windows Form with Browse for the main csv file and a button to run and execute.

    I should be able to Build them all in an exe.

    Any help in this regard is highly appreciated ..Thanks is advance

    Saturday, November 8, 2014 8:05 PM

Answers

  • Add-Type -AssemblyName System.Drawing
    Add-Type -AssemblyName System.Windows.Forms
    
    $Script:FileName = $Null
    
    Function mnuOpen_Click
    {
    	$OpenFileDialog = New-Object Microsoft.Win32.OpenFileDialog
    	$OpenFileDialog.initialDirectory = "C:\"
    	$OpenFileDialog.filter = "CSV files (*.csv)| *.csv"
    	$OpenFileDialog.ShowDialog() | Out-Null
    	$Script:FileName = $SaveFileDialog.filename
    } 
    
    Function mnuExit_Click
    {
       $Form.Close()
    } 
    
    Function Button_Click
    {
        [System.Windows.Forms.MessageBox]::Show("Your script goes here" , "Run Your Script")
    } 
    
    Function Show-Form
    {
        $Form = New-Object System.Windows.Forms.Form
        $MainMenu = New-Object System.Windows.Forms.MainMenu
        $mnuFile = New-Object System.Windows.Forms.MenuItem
        $mnuOpen = New-Object System.Windows.Forms.MenuItem
        $mnuExit = New-Object System.Windows.Forms.MenuItem
        $Button = New-Object System.Windows.Forms.Button
    
        $Form.Name = 'Form'
        $Form.Text = 'Some Form'
        $Form.DataBindings.DefaultDataSourceUpdateMode = 0
        $Form.ClientSize = New-Object System.Drawing.Size(290,159)
        $Form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen
        $Form.BackColor = [System.Drawing.Color]::FromArgb(255,185,209,234)
    
        $MainMenu.Name = "MainMenu"
        $Form.Menu = $MainMenu
    
        $mnuFile.Name = 'mnuFile'
        $mnuFile.Text = '&File'
        $MainMenu.MenuItems.Add($mnuFile) | Out-Null
    
        $mnuOpen.Name = 'mnuOpen'
        $mnuOpen.Text = '&Open'
        $mnuOpen.Add_Click({mnuOpen_Click})
        $mnuFile.MenuItems.Add($mnuOpen) | Out-Null
    
        $mnuExit.Name = 'mnuExit'
        $mnuExit.Text = 'E&xit'
        $mnuExit.Add_Click({mnuExit_Click})
        $mnuFile.MenuItems.Add($mnuExit) | Out-Null
    
        $Button.Name = "Button"
        $Button.Location = New-Object System.Drawing.Size(75,75)
        $Button.Size = New-Object System.Drawing.Size(120,23)
        $Button.Text = "Run Your Script"
        $Form.Controls.Add($Button)
        $Button.Add_Click({Button_Click})    
    
        $Form.ShowDialog()| Out-Null
    }
    
    Show-Form
    

    Saturday, November 8, 2014 9:59 PM

All replies

  • Here is how to do that in Powershell:

    Import-Csv -Path D:\Lzero\Input\Main.csv | 
         sort Environment |
         ForEach-Object{
            $_.Month=([datetime]"$($_.Month) 1, 2000").Month
            $_.Time =$_.Time.replace(":",".-")
            $_.day=@{Monday='1';Tuesday='2';Wednesday='3';Thursday='4';Friday='5';Saturday='6';Sunday='7'}[$_.day]
        } |
        Export-Csv D:\Lzero\Input_1\inventory_pass1.csv -notypeinformation 
    

    Her is how we get the week of the year in PowerShell

    PS >$today=[datetime]::Today
    PS >[System.Globalization.DateTimeFormatInfo]::CurrentInfo.Calendar.GetWeekOfYear($today,'FirstDay',1)
    45

    That is all it takes. There are a number of standard week algorithms in the Net FrameWork and it is culture relative so you can calculate for any culture.

    As for a form just place the following in a form in the click event of the button.

    Import-Csv -Path D:\Lzero\Input\Main.csv | 
         sort Environment |
         ForEach-Object{
            $_.Month=([datetime]"$($_.Month) 1, 2000").Month
            $_.dayOfWeek=@{Monday='1';Tuesday='2';Wednesday='3';Thursday='4';Friday='5';Saturday='6';Sunday='7'}[$_.day]
            $dateStr='{0}-{1}-{2} {3}' -f $_.day,$_.day,$_.year,$_.time
            $date=[DateTime]$datestr
            $_.WeekOfYear=[System.Globalization.DateTimeFormatInfo]::CurrentInfo.Calendar.GetWeekOfYear($today,'FirstDay',1)
        } |
        Export-Csv D:\Lzero\Input_1\inventory_pass1.csv -notypeinformation 
    


    ¯\_(ツ)_/¯

    Saturday, November 8, 2014 8:58 PM
  • Oh - if you have a date then we get the week day number like this:

    PS >$date=[datetime]'3-25-1998 12:32PM'
    PS >$date.DayOfWeek
    Saturday
    PS > [int]$date.DayOfWeek
    6

    Notice how the computer makes thinking and writing code unnecessary.  If you can think of it the chances are it is built into Windows already since computing is probably much older than you are. All of the questions you can think of that are useful have likely already been answered.


    ¯\_(ツ)_/¯



    • Edited by jrv Saturday, November 8, 2014 9:05 PM
    Saturday, November 8, 2014 9:03 PM
  • @jrv,

    Thanks a lot for your valuable suggestions, I am ok with the script it performs simple operation of Splitting a CSV that contains list of servers and patching schedule. Initially I am importing the Main file i.e. the CSV with details that has to be segregated in the code like this

    #------------------------Input of Main Inventory * Start of basic Numeral Conversion------------------------------
    $MainInventory
    = Import-Csv -Path D:\Lzero\Input\Main.csv | sort Environment

    and then executing the script. By any chance is it possible to have a simple GUI where I can browse the Main.csv and click a run button that would execute my script.

    Thanks in advance

    Saturday, November 8, 2014 9:04 PM
  • I would start by learning how to use Powershell first.  Forms are very difficult if you do not understand the language you are using to build them.

    You can use Out-Gridview to display a GUI with a CSV and click on a line.  The gridview return the line values which you can use.

    http://technet.microsoft.com/library/70915a86-d753-464e-8349-cba02316154c(v=wps.630).aspx

    http://mcpmag.com/articles/2013/01/08/pshell-gridview.aspx


    ¯\_(ツ)_/¯

    Saturday, November 8, 2014 9:09 PM
  • @jrv,

    Thanks a lot for your suggestions on it, but I am just a beginner I would I say not even a L-Zero at Powershell. I will soon start learning this technology too.:)

    Saturday, November 8, 2014 9:14 PM
  • @jrv,

    Thanks a lot for your suggestions on it, but I am just a beginner I would I say not even a L-Zero at Powershell. I will soon start learning this technology too.:)

    Start simple. With PowerShell you will progress very fast if you learn the basics well.  Try not to invent code when the system already has a solution.  This will make you quicker and more flexible.

    I recognize you initial effort and it is pretty good. Whenever you write many lines that look alike it is a clue that you are missing some piece of information. The redundant lines are a hint as to what you need to look for.

    Dates items are almost as old as a computer.  All of this is built into the system and not accessible via PowerShell/Net.  Once you understand those things you will be able to do amazing things.

    I am glad you like PowerShell.  Have lots of fun with it.

    http://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx


    ¯\_(ツ)_/¯


    • Edited by jrv Saturday, November 8, 2014 9:21 PM
    Saturday, November 8, 2014 9:20 PM
  • @jrv,

    That's so true and I agree with you,

    Ok just on my curiosity is it possible to generate the Date if you have given with WeekNumber,WeekDay, Month and Year as input.

    Eg. 2nd Saturday of February 2015 = 02/14/2014 ?? Using powershell

    Saturday, November 8, 2014 9:24 PM
  • Yes. It is possible.


    ¯\_(ツ)_/¯

    Saturday, November 8, 2014 9:33 PM
  • Is it too difficult for you to code and post now that would be a great help :)

    Saturday, November 8, 2014 9:37 PM
  • Here is an incomplete example:

    PS >$week=45
    PS >([datetime]'1/1/2014').AddDays($week*7+[int]([datetime]'1/1/2014').Dayofweek)
    Saturday, November 15, 2014 12:00:00 AM


    ¯\_(ツ)_/¯

    Saturday, November 8, 2014 9:42 PM
  • @jrv,

    That's amazing, I must say that you are genius at this technology.

    Thanks alot

    Saturday, November 8, 2014 9:49 PM
  • Add-Type -AssemblyName System.Drawing
    Add-Type -AssemblyName System.Windows.Forms
    
    $Script:FileName = $Null
    
    Function mnuOpen_Click
    {
    	$OpenFileDialog = New-Object Microsoft.Win32.OpenFileDialog
    	$OpenFileDialog.initialDirectory = "C:\"
    	$OpenFileDialog.filter = "CSV files (*.csv)| *.csv"
    	$OpenFileDialog.ShowDialog() | Out-Null
    	$Script:FileName = $SaveFileDialog.filename
    } 
    
    Function mnuExit_Click
    {
       $Form.Close()
    } 
    
    Function Button_Click
    {
        [System.Windows.Forms.MessageBox]::Show("Your script goes here" , "Run Your Script")
    } 
    
    Function Show-Form
    {
        $Form = New-Object System.Windows.Forms.Form
        $MainMenu = New-Object System.Windows.Forms.MainMenu
        $mnuFile = New-Object System.Windows.Forms.MenuItem
        $mnuOpen = New-Object System.Windows.Forms.MenuItem
        $mnuExit = New-Object System.Windows.Forms.MenuItem
        $Button = New-Object System.Windows.Forms.Button
    
        $Form.Name = 'Form'
        $Form.Text = 'Some Form'
        $Form.DataBindings.DefaultDataSourceUpdateMode = 0
        $Form.ClientSize = New-Object System.Drawing.Size(290,159)
        $Form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen
        $Form.BackColor = [System.Drawing.Color]::FromArgb(255,185,209,234)
    
        $MainMenu.Name = "MainMenu"
        $Form.Menu = $MainMenu
    
        $mnuFile.Name = 'mnuFile'
        $mnuFile.Text = '&File'
        $MainMenu.MenuItems.Add($mnuFile) | Out-Null
    
        $mnuOpen.Name = 'mnuOpen'
        $mnuOpen.Text = '&Open'
        $mnuOpen.Add_Click({mnuOpen_Click})
        $mnuFile.MenuItems.Add($mnuOpen) | Out-Null
    
        $mnuExit.Name = 'mnuExit'
        $mnuExit.Text = 'E&xit'
        $mnuExit.Add_Click({mnuExit_Click})
        $mnuFile.MenuItems.Add($mnuExit) | Out-Null
    
        $Button.Name = "Button"
        $Button.Location = New-Object System.Drawing.Size(75,75)
        $Button.Size = New-Object System.Drawing.Size(120,23)
        $Button.Text = "Run Your Script"
        $Form.Controls.Add($Button)
        $Button.Add_Click({Button_Click})    
    
        $Form.ShowDialog()| Out-Null
    }
    
    Show-Form
    

    Saturday, November 8, 2014 9:59 PM
  • @Brian Nadjiwon,

    That's exactly what I am looking for thanks a lot,

    Please answer few quick questions like:

    * where do I paste all my script into your codes of functions?

    Saturday, November 8, 2014 10:05 PM
  • Within the Button Click event

    Function Button_Click
    {
        # Your script goes here
        # $Script:FileName contains the filename 
        IF ($Script:FileName -NE $Null)
        {
            #  Do Something
        }
        Else
        {
           [System.Windows.Forms.MessageBox]::Show("Select File by Clicking Menu File - Open" , "No File Selected")
        }
    } 

    Saturday, November 8, 2014 10:14 PM