locked
Creating a Powershell Tool using the GUI RRS feed

  • Question

  • I've worked on this power shell script to remove the files from the target folder, I've completed the script and everything was going fine until one of my colleagues came up to me and asked to create the same script enhancing with the GUI :

    #------Script-----#

    Param([string] $Destination_Folder, [int] $Number_of_Days, [DateTime] $Today)

    # Declaring the user interaction variables as global variable

    $global:textbox = $textbox
    $global:textbox1 = $textbox1
    $global:textbox2 = $textbox2
    $global:Last_Write_time =$Last_Write_time

    $Today = Get-Date 
    #Takes Number of Days
    $Number_of_days="$textBox1"
    #Destination folder to get the files
    $Destination_Folder ="$textBox"
    #Extention of the file which we want to delete
    $Extension ="$textBox2"
    #Checking the days using Last_Write_Time parameter based on number of days
    $Last_Write_time = $Today.AddDays(-$textbox1.Text) 
    #Scanning the files using the last_write_time and the destination_folder

    # GUI using the System.windows.Forms

    Add-Type -AssemblyName System.Windows.Forms
    Add-Type -AssemblyName System.Drawing

    $form = New-Object System.Windows.Forms.Form
    $form.Text = 'Deleting the files according to the number of days given'
    $form.Size = New-Object System.Drawing.Size(475,325)
    $form.StartPosition = 'CenterScreen'

    $OKButton = New-Object System.Windows.Forms.Button
    $OKButton.Location = New-Object System.Drawing.Point(75,220)
    $OKButton.Size = New-Object System.Drawing.Size(75,23)
    $OKButton.Text = 'OK'
    $OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
    $form.AcceptButton = $OKButton
    $form.Controls.Add($OKButton)

    $CancelButton = New-Object System.Windows.Forms.Button
    $CancelButton.Location = New-Object System.Drawing.Point(175,220)
    $CancelButton.Size = New-Object System.Drawing.Size(75,23)
    $CancelButton.Text = 'Cancel'
    $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
    $form.CancelButton = $CancelButton
    $form.Controls.Add($CancelButton)

    $label = New-Object System.Windows.Forms.Label
    $label.Location = New-Object System.Drawing.Point(20,20)
    $label.Size = New-Object System.Drawing.Size(280,20)
    $label.Text = 'Please enter the Target path below:'
    $form.Controls.Add($label)

    $textBox = New-Object System.Windows.Forms.TextBox
    $textBox.Location = New-Object System.Drawing.Point(10,40)
    $textBox.Size = New-Object System.Drawing.Size(260,20)
    $form.Controls.Add($textBox)

    $label1 = New-Object System.Windows.Forms.Label
    $label1.Location = New-Object System.Drawing.Point(10,80)
    $label1.Size = New-Object System.Drawing.Size(280,20)
    $label1.Text = 'Please enter number of days:'
    $form.Controls.Add($label1)

    $textBox1 = New-Object System.Windows.Forms.TextBox
    $textBox1.Location = New-Object System.Drawing.Point(10,100)
    $textBox1.Size = New-Object System.Drawing.Size(260,20)
    $form.Controls.Add($textBox1)

    $label2 = New-Object System.Windows.Forms.Label
    $label2.Location = New-Object System.Drawing.Point(10,140)
    $label2.Size = New-Object System.Drawing.Size(280,40)
    $label2.Text = 'Please enter the type of extension that you want to delete:'
    $form.Controls.Add($label2)

    $textBox2 = New-Object System.Windows.Forms.TextBox
    $textBox2.Location = New-Object System.Drawing.Point(10,180)
    $textBox2.Size = New-Object System.Drawing.Size(260,20)
    $form.Controls.Add($textBox2)


    $form.Topmost = $true

    $form.Add_Shown({$textBox.Select()})
    $form.Add_Shown({$textBox1.Select()})
    $form.Add_Shown({$textBox2.Select()})
    $result = $form.ShowDialog()
      
    $OKButton_OnClick = 

    function CalculatingtheDays
    {

        $Files = Get-Childitem $textbox.Text -Include $textbox2.Text -Recurse | Where {$_.LastWriteTime -le "$Last_Write_time"} -ErrorAction Stop 
    try
    {
        if ($textbox1.Text -gt $Last_Write_time)
        {
            write-host "There are no files Present <g class="gr_ gr_67 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="67" id="67">refering</g> to the days you have entered" -ForegroundColor "Green" #It will check for the files which are created greater than the number of days
    }
                else
              {
                Write-host "Files are being Deleted!!" -ForegroundColor Yellow
          }
      }
      Catch{

    foreach ($File in $Files)     {
              if ($File -ne $NULL)
                  {
                  write-host "Removing Files $File" -ForegroundColor "Red"
                  Remove-Item $File.FullName | out-null
                
                  }
              else
                  {
                  Write-host "cleanup has complete. There are no more files to delete!" -Foregroundcolor White
                  }
              }


    }

    When I enter the number of days, destination folder and the extension, nothing is happening. Wondering where exactly I am doing wrong.

    <g class="gr_ gr_80 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="80" id="80">Apreciate</g> your response.

    Regards,

    Raghu


    • Edited by Ragotham Tuesday, August 28, 2018 5:46 AM
    Tuesday, August 28, 2018 5:45 AM

All replies

  • Please post your code correctly.  Use the tool provided on the edit bar.

    You cannot assign a function to a variable.  It serves no purpose.

    Where did you find this code?  We cannot debug and fix large amounts of code.  You need to ask a specific question.


    \_(ツ)_/

    Tuesday, August 28, 2018 6:10 AM
  • What is it you need to do?  What is not happening.   You have code that can never be executed.  Why?

    Here is a good place to begin learning how to use forms: https://info.sapien.com/index.php/guis

    I asked you to fix your original post.  Please do that first.


    \_(ツ)_/


    • Edited by jrv Tuesday, August 28, 2018 7:02 AM
    Tuesday, August 28, 2018 7:01 AM
  • Hello,

    In addition, you haven't defined at your function any file by extension... so in case your function work, it will delete all files allocated at specified folder.

    regards,

    Tuesday, August 28, 2018 11:09 AM