question

MuhammadAnwar-6404 avatar image
0 Votes"
MuhammadAnwar-6404 asked MotoX80 published

Form Open Issue "PowerShell Script"

Hi,
i am work on an script where 02 buttons whom select the folder, but once i run the script the folder select form open first, how ever the main form should be open first, the script is as follow. please review it and share your advise.


Add Assembly


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

Add Form

$Form = New-Object System.Windows.Forms.Form
$Form.Text = "Data Backup - Incrimental"
$Form.Size = New-Object System.Drawing.Size(400,400)

Add Function for Source Button

Function Get-Folder($initialDirectory="")
{
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")|Out-Null
$foldername = New-Object System.Windows.Forms.FolderBrowserDialog
$foldername.Description = "Select Source Folder"
$foldername.rootfolder = "MyComputer"
$foldername.SelectedPath = $initialDirectory
if($foldername.ShowDialog() -eq "OK")
{
$folder += $foldername.SelectedPath
}
return $folder
}

$SourceFolder = Get-Folder

Add Source Button

$SFB = New-Object System.Windows.Forms.Button
$SFB.Size = New-Object System.Drawing.Size(150,30)
$SFB.Location = New-Object System.Drawing.Size(20,20)
$SFB.Text = "Select Source Folder"
$SFB.Add_Click({$foldername.ShowDialog()})

$Form.Controls.Add($SFB)

Add Label of Source Location

$LSF = New-Object System.Windows.Forms.Label
$LSF.Location = New-Object System.Drawing.Size(180,30)
$LSF.BackColor = "Transparent"
$LSF.AutoSize = $true
$LSF.Text = "$SourceFolder"

$Form.Controls.Add($LSF)

Add Function for Destination Button

[void][reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
$browse1 = new-object system.windows.Forms.FolderBrowserDialog
$browse1.RootFolder = 'MyComputer'
$browse1.ShowNewFolderButton = $false
$browse1.Description = "Select Destination Folder"

Add Destination Button

$SDB = New-Object System.Windows.Forms.Button
$SDB.Size = New-Object System.Drawing.Size(150,30)
$SDB.Location = New-Object System.Drawing.Size(20,70)
$SDB.Text = "Select Destination Folder"
$SDB.Add_Click({$browse1.ShowDialog()})

$Form.Controls.Add($SDB)


Add Lable of Dedtination Location

$LDF = New-Object System.Windows.Forms.Label
$LDF.Location = New-Object System.Drawing.Size(180,80)
$LDF.BackColor = "Transparent"
$LDF.AutoSize = $true
$LDF.Text = "Muhammad Anwar"

$Form.Controls.Add($LDF)

Add Output Screen

$Output = New-Object System.Windows.Forms.TextBox
$Output.Location = New-Object System.Drawing.Size(20,140)
$Output.Size = New-Object System.Drawing.Size(350,200)
$Output.Multiline = $true
$Output.ScrollBars = "Vertical"

$Form.Controls.Add($Output)

$Form.ShowDialog()

windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MotoX80 avatar image
0 Votes"
MotoX80 answered MuhammadAnwar-6404 commented

Here's something to get you going.

 #Add Assembly
    
 Add-Type -AssemblyName System.Windows.Forms
 Add-Type -AssemblyName System.Drawing
    
 #Add Form
 $Form = New-Object System.Windows.Forms.Form
 $Form.Text = "Data Backup - Incrimental"
 $Form.Size = New-Object System.Drawing.Size(400,400)
 $initialDirectory = ""
 $Sourcefolder = ""
 $Destfolder = ""
    
 #Add Function for Source Button
 Function Get-Folder($initialDirectory="")
 {
     [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")|Out-Null
     $foldername = New-Object System.Windows.Forms.FolderBrowserDialog
     $foldername.Description = "Select Source Folder"
     $foldername.rootfolder = "MyComputer"
     $foldername.SelectedPath = $initialDirectory
        
     if($foldername.ShowDialog() -eq "OK")
     {
         $folder += $foldername.SelectedPath
     }
     return $folder
 }
    
 #$SourceFolder = Get-Folder           # this was the statement that was showing the "wrong" dialog 
    
 #Add Source Button
 $SFB = New-Object System.Windows.Forms.Button
 $SFB.Size = New-Object System.Drawing.Size(150,30)
 $SFB.Location = New-Object System.Drawing.Size(20,20)
 $SFB.Text = "Select Source Folder"
 $SFB.Add_Click({
     $script:SourceFolder = Get-Folder  
     $LSF.Text = "$SourceFolder"
     $count = $output.items.add("Source folder set to: $Sourcefolder")
  })
    
 $Form.Controls.Add($SFB)
    
 #Add Label of Source Location
 $LSF = New-Object System.Windows.Forms.Label
 $LSF.Location = New-Object System.Drawing.Size(180,30)
 $LSF.BackColor = "Transparent"
 $LSF.AutoSize = $true
 $LSF.Text = "$SourceFolder"
    
 $Form.Controls.Add($LSF)
    
 #Add Function for Destination Button
 [void][reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
 $browse1 = new-object system.windows.Forms.FolderBrowserDialog
 $browse1.RootFolder = 'MyComputer'
 $browse1.ShowNewFolderButton = $false
 $browse1.Description = "Select Destination Folder"
    
 #Add Destination Button
 $SDB = New-Object System.Windows.Forms.Button
 $SDB.Size = New-Object System.Drawing.Size(150,30)
 $SDB.Location = New-Object System.Drawing.Size(20,70)
 $SDB.Text = "Select Destination Folder"
 $SDB.Add_Click({
    
     $script:DestFolder = Get-Folder($initialdirectory="c:\temp")  
     $LDF.Text  = "$DestFolder"
     $output.items.add("Dest folder set to: $destfolder")
  })
    
    
    
 $Form.Controls.Add($SDB)
    
    
 #Add Lable of Dedtination Location
 $LDF = New-Object System.Windows.Forms.Label
 $LDF.Location = New-Object System.Drawing.Size(180,80)
 $LDF.BackColor = "Transparent"
 $LDF.AutoSize = $true
 $LDF.Text = "Muhammad Anwar"
    
 $Form.Controls.Add($LDF)
    
 #Add Output Screen
 $Output = New-Object System.Windows.Forms.Listbox
 $Output.Location = New-Object System.Drawing.Size(20,140)
 $Output.Size = New-Object System.Drawing.Size(350,200)
 #$Output.Multiline = $true
 #$Output.ScrollBars = "Vertical"
 $count = $Output.items.add("Please select both source and destination folders.")
 $Form.Controls.Add($Output)
    
 $Form.ShowDialog()
    
 "Now go process these folders!"
    
 "Source folder: $sourcefolder"
    
 "Dest folder  : $destfolder"
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi MotoX80,

Thanks for your guidance, its working now, can you please further help to complete this project.

I also need a output box where all the action view in live as shows the command output while using PowerShell or CMD.

in this case the result shows after process complete.


0 Votes 0 ·
LimitlessTechnology-2700 avatar image
0 Votes"
LimitlessTechnology-2700 answered
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MotoX80 avatar image
0 Votes"
MotoX80 answered MotoX80 published

How about this.

 #Add Assembly
    
 Add-Type -AssemblyName System.Windows.Forms
 Add-Type -AssemblyName System.Drawing
    
 #Add Form
 $Form = New-Object System.Windows.Forms.Form
 $Form.Text = "Data Backup - Incrimental"
 $Form.Size = New-Object System.Drawing.Size(400,400)
 $initialDirectory = ""
 $Sourcefolder = ""
 $Destfolder = ""
    
 #Add Function for Source Button
 Function Get-Folder($initialDirectory="")
 {
     [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")|Out-Null
     $foldername = New-Object System.Windows.Forms.FolderBrowserDialog
     $foldername.Description = "Select Source Folder"
     $foldername.rootfolder = "MyComputer"
     $foldername.SelectedPath = $initialDirectory
        
     if($foldername.ShowDialog() -eq "OK")
     {
         $folder += $foldername.SelectedPath
     }
     return $folder
 }
    
 #$SourceFolder = Get-Folder           # this was the statement that was showing the "wrong" dialog 
    
 #Add Source Button
 $SFB = New-Object System.Windows.Forms.Button
 $SFB.Size = New-Object System.Drawing.Size(150,30)
 $SFB.Location = New-Object System.Drawing.Size(20,15)
 $SFB.Text = "Select Source Folder"
 $SFB.Add_Click({
     $script:SourceFolder = Get-Folder  
     $LSF.Text = "$SourceFolder"
     $count = $output.items.add("Source folder set to: $Sourcefolder")
  })
    
 $Form.Controls.Add($SFB)
    
 #Add Label of Source Location
 $LSF = New-Object System.Windows.Forms.Label
 $LSF.Location = New-Object System.Drawing.Size(180,20)
 $LSF.BackColor = "Transparent"
 $LSF.AutoSize = $true
 $LSF.Text = "$SourceFolder"
    
 $Form.Controls.Add($LSF)
    
 #Add Function for Destination Button
 [void][reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
 $browse1 = new-object system.windows.Forms.FolderBrowserDialog
 $browse1.RootFolder = 'MyComputer'
 $browse1.ShowNewFolderButton = $false
 $browse1.Description = "Select Destination Folder"
    
 #Add Destination Button
 $SDB = New-Object System.Windows.Forms.Button
 $SDB.Size = New-Object System.Drawing.Size(150,30)
 $SDB.Location = New-Object System.Drawing.Size(20,60)
 $SDB.Text = "Select Destination Folder"
 $SDB.Add_Click({
    
     $script:DestFolder = Get-Folder($initialdirectory="c:\temp")  
     $LDF.Text  = "$DestFolder"
     $output.items.add("Dest folder set to: $destfolder")
  })
 $Form.Controls.Add($SDB)
    
 #Add Run Button
 $Run = New-Object System.Windows.Forms.Button
 $Run.Size = New-Object System.Drawing.Size(150,30)
 $Run.Location = New-Object System.Drawing.Size(20,100)
 $Run.Text = "Run"
 $Run.Add_Click({
     if ($Sourcefolder -eq "") {
         $count = $Output.items.add("Please select a source folder.")
         return
     }
     if ($destfolder -eq "") {
         $count = $Output.items.add("Please select a destination folder.")
         return
     }
     
     # Build our script 
     $ScriptName = "$env:TEMP\temp.ps1"
     'write-host "Source is {0}"'  -f $sourcefolder  | out-file $ScriptName 
     'Get-Childitem "{0}"'  -f $sourcefolder  | out-file $ScriptName -Append 
     'write-host "Dest is {0}"' -f $destfolder  | out-file $ScriptName -Append
     'Get-Childitem "{0}"'  -f $destfolder  | out-file $ScriptName -Append 
     
    
     # Setup the Process startup info 
     $pinfo = New-Object System.Diagnostics.ProcessStartInfo 
     $pinfo.FileName = "powershell.exe" 
     $pinfo.Arguments = $ScriptName 
     $count =$output.items.add("Script name is $ScriptName")  
     $pinfo.UseShellExecute = $false 
     $pinfo.CreateNoWindow = $true 
     $pinfo.RedirectStandardOutput = $true 
     $pinfo.RedirectStandardError = $true
     # Create a process object using the startup info
     $process= New-Object System.Diagnostics.Process 
     $process.StartInfo = $pinfo
     # Start the process 
     $count =$output.items.add("Running....$ScriptName")
     
     $process.Start() | Out-Null 
     # Wait a while for the process to fire up and do something 
     sleep -Seconds 3 
     while (!$process.HasExited) {
         if ($process.StandardOutput.EndOfStream -eq $false) {
             $count = $output.items.add($process.StandardOutput.ReadLine())
         }
     }
     $count =$output.items.add($process.StandardError.ReadToEnd())
    
  })
    
 $Form.Controls.Add($Run)
    
    
 #Add Lable of Dedtination Location
 $LDF = New-Object System.Windows.Forms.Label
 $LDF.Location = New-Object System.Drawing.Size(180,70)
 $LDF.BackColor = "Transparent"
 $LDF.AutoSize = $true
 $LDF.Text = ""
    
 $Form.Controls.Add($LDF)
    
 #Add Output Screen
 $Output = New-Object System.Windows.Forms.Listbox
 $Output.Location = New-Object System.Drawing.Size(20,140)
 $Output.Size = New-Object System.Drawing.Size(350,200)
 #$Output.Multiline = $true
 #$Output.ScrollBars = "Vertical"
 $count = $Output.items.add("Please select both source and destination folders.")
 $Form.Controls.Add($Output)
    
 $result = $Form.ShowDialog()
    
 "Here is the script that we ran...." 
 Get-Content $ScriptName
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.