Form Open Issue "PowerShell Script"

Muhammad Anwar 21 Reputation points
2021-09-29T11:10:42.357+00:00

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
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,362 questions
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 31,571 Reputation points
    2021-10-05T22:39:55.1+00:00

    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"
    

2 additional answers

Sort by: Most helpful
  1. MotoX80 31,571 Reputation points
    2021-10-06T22:29:44.71+00:00

    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
    
    0 comments No comments