none
need help with Powershell GUI

    Question

  • Hello Friends

    i need help with a GUI which will become part of my existing staging script, the GUI should get the information of packages from Dev ( i think i can Handel this part )

    should be able to select more than one software package

    select if staging to Field Testing / Production (  need this Dropdown in the existing form )  screenshot attached

    write the information out

    Input : List of applications from DEV Environment

    Out put should be like below :

    0210 Toshiba_Intel_WLAN_15.14.0.2,DCA002E3,Field Test
    0800 Marvel_WLAN_15.68.3073.151,DCA0041D,Production
    0800 MS_Intel_Video_20.19.15.4331,DCA009E2,Field Test

    also how to add a logo, in case if i wish to add company logo

    so far i could reach till here ( script below )

    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
    [void][System.Reflection.Assembly]::LoadWithPartialName( “Microsoft.VisualBasic”)

    $objForm = New-Object System.Windows.Forms.Form
    $objForm.Text = "Software Staging Tool"
    $objForm.Size = New-Object System.Drawing.Size(900,700)
    $objForm.StartPosition = "CenterScreen"

    # Got rid of the block of code related to KeyPreview and KeyDown events.

    $OKButton = New-Object System.Windows.Forms.Button
    $OKButton.Location = New-Object System.Drawing.Size(70,530)
    $OKButton.Size = New-Object System.Drawing.Size(75,23)
    $OKButton.Text = "OK"

    # Got rid of the Click event for the OK button, and instead just assigned its DialogResult property to OK.
    $OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK

    $objForm.Controls.Add($OKButton)

    # Setting the form's AcceptButton property causes it to automatically intercept the Enter keystroke and
    # treat it as clicking the OK button (without having to write your own KeyDown events).
    $objForm.AcceptButton = $OKButton

    $CancelButton = New-Object System.Windows.Forms.Button
    $CancelButton.Location = New-Object System.Drawing.Size(150,530)
    $CancelButton.Size = New-Object System.Drawing.Size(75,23)
    $CancelButton.Text = "Cancel"

    # Got rid of the Click event for the Cancel button, and instead just assigned its DialogResult property to Cancel.
    $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel

    $objForm.Controls.Add($CancelButton)

    # Setting the form's CancelButton property causes it to automatically intercept the Escape keystroke and
    # treat it as clicking the OK button (without having to write your own KeyDown events).
    $objForm.CancelButton = $CancelButton

    $objLabel = New-Object System.Windows.Forms.Label
    $objLabel.Location = New-Object System.Drawing.Size(10,20)
    $objLabel.Size = New-Object System.Drawing.Size(280,20)
    $objLabel.Text = "Please select Package's to Stage"
    $objForm.Controls.Add($objLabel)

    $objListBox = New-Object System.Windows.Forms.ListBox
    $objListBox.Location = New-Object System.Drawing.Size(10,40)
    $objListBox.Size = New-Object System.Drawing.Size(700,400)
    $objListBox.Height = 300

    $objListBox.BackColor = "cyan"
    $objListBox.Forecolor = "blue"
    $objListBox.Font = "Arial Black"
    $objListbox.SelectionMode = "MultiSimple"



    Get-Content C:\temp\input.txt | ForEach-Object {[void] $objListBox.Items.Add($_)}


    $objForm.Controls.Add($objListBox)

    $objForm.Topmost = $True

    $result = $objForm.ShowDialog()
    if ($result -eq [System.Windows.Forms.DialogResult]::OK -and $objListBox.SelectedIndex -ge 0)
    {
        foreach ($objItem in $objListbox.SelectedItems)
                {$selection = $objItem
                $selection | Out-File "C:\temp\output.txt" -Append
                 }
            $objForm.Close()

        # Do something with $selection
    }
    Clear-Host

    Thank you in advance


    OSLM ENGINEER - SCCM 2007 & 2012

    Saturday, March 11, 2017 3:36 PM

Answers

  • Here is how to do the logo as well as many ways to improve the form by using standard design and naming methods.  Your version is very old converted VBScript which doesn't work well.

    You will also have sizing and display  issues if you don't set up visual styles.

    function Get-Packages{
    	Param(
    		[Parameter(Mandatory)]
    		$InputFile
    	)
    	$list = Get-Content $InputFile
    	$script:mode = 'Production'
    	Add-Type -AssemblyName System.Windows.Forms
    
    	$script:form = New-Object System.Windows.Forms.Form
    	$form.Text = 'Software Staging Tool'
    	$form.Size = '650, 600'
    	$form.StartPosition = 'CenterScreen'
    	$form.Topmost = $True
    	$form.BackgroundImage.('http://www.designedsystemsonline.com/iis.png')
    	
    	$picturebox1 = New-Object System.Windows.Forms.PictureBox
    	$form.Controls.Add($picturebox1)
    	$picturebox1.Location = '5,5'
    	$picturebox1.Margin = '0, 0, 0, 0'
    	$picturebox1.Name = 'picturebox1'
    	$picturebox1.Size = '50, 50'
    	$picturebox1.SizeMode = 'StretchImage'
    	$picturebox1.TabStop = $False
    	$picturebox1.Load('http://www.designedsystemsonline.com/abstract-logo-with-colorful-leaves_1025-695.jpg')
    	
    	$label1 = New-Object System.Windows.Forms.Label
    	$form.Controls.Add($label1)
    	$label1.Location = '60, 20'
    	$label1.AutoSize = $true
    	$label1.TextAlign = 'MiddleLeft'
    	$label1.Font = 'Engravers MT, 16.2pt, style=Bold, Italic'
    	$label1.Text = 'Software Staging Tool'
    	
    	$buttonOK = New-Object System.Windows.Forms.Button
    	$form.Controls.Add($buttonOK)
    	$form.AcceptButton = $buttonOK
    	$buttonOK.Location = '200, 450'
    	$buttonOK.Text = 'OK'
    	$buttonOK.DialogResult = 'OK'
    
    	$buttonCancel = New-Object System.Windows.Forms.Button
    	$form.Controls.Add($buttonCancel)
    	$form.CancelButton = $buttonCancel
    	$buttonCancel.Location = '280, 450'
    	$buttonCancel.Text = 'Cancel'
    	$buttonCancel.DialogResult = 'Cancel'
    
    	$label2 = New-Object System.Windows.Forms.Label
    	$form.Controls.Add($label2)
    	$label2.Location = '10, 70'
    	$label2.AutoSize = $true
    	$label2.Text = 'Please select Package''s to Stage to:'
    	
    	$radiobuttonProduction = New-Object System.Windows.Forms.RadioButton
    	$form.Controls.Add($radiobuttonProduction)
    	$radiobuttonProduction.AutoSize = $true
    	$radiobuttonProduction.TextAlign = 'MiddleLeft'
    	$radiobuttonProduction.Location = '245,67'
    	$radiobuttonProduction.Text = 'Production'
    	$radiobuttonProduction.Checked = $true
    	$checkChanged = {
    		$script:mode = if($radiobuttonProduction.Checked){'Production'}else{'Field Test'}
        }
    	$radiobuttonProduction.add_CheckedChanged($checkChanged)
    	
    	$radiobuttonFieldtest = New-Object System.Windows.Forms.RadioButton
    	$form.Controls.Add($radiobuttonFieldtest)
    	$radiobuttonFieldtest.AutoSize = $true
    	$radiobuttonFieldtest.TextAlign = 'MiddleLeft'
    	$radiobuttonFieldtest.Location = '340,67'
    	$radiobuttonFieldtest.Text ='Field Test'
    	
    	$lisboxList = New-Object System.Windows.Forms.ListBox
    	$form.Controls.Add($lisboxList)
    	$lisboxList.Location = '10, 90'
    	$lisboxList.Size = '1100, 350'
    	$lisboxList.BackColor = 'cyan'
    	$lisboxList.Forecolor = 'blue'
    	$lisboxList.Font = 'Arial Black'
    	$lisboxList.SelectionMode = 'MultiSimple'
    	$lisboxList.Items.AddRange($list)
    
    
    	if ('Ok' -eq $form.ShowDialog()) {
    		$i = 0
    		$lisboxList.SelectedItems |
    			ForEach-Object{
    				[pscustomobject]@{
    					Index = $i++
    					Mode = $mode
    					Package = $_
    	            }
    	    	}
    	}
    }
    
    # by default PowerShell now gives us three ways to ru this
    #Get-Packages
    Get-Packages c:\temp\input.txt
    #Get-Packages c:\temp\input.txt | Export-Csv c:\temp\outfile.csv -notype
    


    \_(ツ)_/

    Sunday, March 12, 2017 12:58 AM
  • Hi

    is there a way that i can prevent this freezing, like the form just triggers the scrpt 2 and the script 2 runs independently

    Kind regards

    Tanoj


    OSLM ENGINEER - SCCM 2007 & 2012


    You could use "Start-Job"

    \_(ツ)_/

    Friday, March 24, 2017 6:41 PM

All replies

  • You have not really asked a question.  What is it that is not working?

    The only clear question is how to add a logo.  Just add a "PictureBox" control.

    Here are two hints as to how to use a ListBox.

    Load from a  file:

    $objListBox.Items.AddRange((Get-Content C:\temp\input.txt))

    Save to a file:

    $objItem in $objListbox.SelectedItems | Out-File C:\temp\output.txt

    Here is a good resource for designing forms: http://info.sapien.com/index.php/guis

    One suggestion is to use a CV to load and save the data.

    What is the box outlined in red for?


    \_(ツ)_/


    • Edited by jrv Saturday, March 11, 2017 4:48 PM
    Saturday, March 11, 2017 4:46 PM
  • Dear Jvr

    Basically the code for the portion marked in red in the pic attached is missing and i do not know how to add this to the code

    basically the input i feed from a text file

    Text1

    Text2

    Text3

    if field test box is check marked the output should be

    Text2,field test

    if not check marked then output should be

    Text2,Production

    Thank you


    OSLM ENGINEER - SCCM 2007 & 2012

    Saturday, March 11, 2017 7:20 PM
  • if($checkbox.Checked){'field test'}else{'Production'}

    I do not understand the issue with the control in red.  What is it that it doesn't do?

    If yuo are just copying code from the Internet and do not know anything about scripting or forms then I recommend that you take the time to learn bot at some level.  I posted a link with good information.  Until you know the basics you will not even be able to ask a good question.

    Also - we do not re-write or fix code found on the Internet.  It is your responsibility to know enough about the technologies you are working with to be able to edit the code and to understand what it is doing.

    You will have to think your way through what you are doing and what you are trying to ask. As it is now your questions are very vague and incomplete.


    \_(ツ)_/

    Saturday, March 11, 2017 7:54 PM
  • I am almost very close to finish, but for some reason i am not getting the output

    if the check box is selected the output should be

    EXAPTpdo Client,Field Test

    if check box is not marked the output should be

    EXAPTpdo Client,Production

    script so far:

    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")

    Clear-History
    Clear-Host

    $objForm = New-Object System.Windows.Forms.Form
    $objForm.Text = " Software Staging Tool"
    $objForm.Size = New-Object System.Drawing.Size(900,700)
    $objForm.StartPosition = "CenterScreen"

    # Got rid of the block of code related to KeyPreview and KeyDown events.

    $OKButton = New-Object System.Windows.Forms.Button
    $OKButton.Location = New-Object System.Drawing.Size(70,330)
    $OKButton.Size = New-Object System.Drawing.Size(75,23)
    $OKButton.Text = "OK"

    # Got rid of the Click event for the OK button, and instead just assigned its DialogResult property to OK.
    $OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK

    $objForm.Controls.Add($OKButton)

    # Setting the form's AcceptButton property causes it to automatically intercept the Enter keystroke and
    # treat it as clicking the OK button (without having to write your own KeyDown events).
    $objForm.AcceptButton = $OKButton

    $CancelButton = New-Object System.Windows.Forms.Button
    $CancelButton.Location = New-Object System.Drawing.Size(150,330)
    $CancelButton.Size = New-Object System.Drawing.Size(75,23)
    $CancelButton.Text = "Cancel"

    # Got rid of the Click event for the Cancel button, and instead just assigned its DialogResult property to Cancel.
    $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel

    $objForm.Controls.Add($CancelButton)

    # Setting the form's CancelButton property causes it to automatically intercept the Escape keystroke and
    # treat it as clicking the OK button (without having to write your own KeyDown events).
    $objForm.CancelButton = $CancelButton

    $objLabel = New-Object System.Windows.Forms.Label
    $objLabel.Location = New-Object System.Drawing.Size(10,20)
    $objLabel.Size = New-Object System.Drawing.Size(280,20)
    $objLabel.Text = "Please select Package's to Stage to Production:"
    $objForm.Controls.Add($objLabel)

    $objListBox = New-Object System.Windows.Forms.ListBox
    $objListBox.Location = New-Object System.Drawing.Size(10,40)
    $objListBox.Size = New-Object System.Drawing.Size(600,1100)
    $objListBox.Height = 300

    $objListBox.BackColor = "cyan"
    $objListBox.Forecolor = "blue"
    $objListBox.Font = "Arial Black"
    $objListbox.SelectionMode = "MultiSimple"

    #This creates a checkbox called Field test
    $objTypeCheckbox = New-Object System.Windows.Forms.Checkbox
    $objTypeCheckbox.Location = New-Object System.Drawing.Size(650,120)
    $objTypeCheckbox.Size = New-Object System.Drawing.Size(150,120)
    $objTypeCheckbox.Text = "Field Test"
    $objTypeCheckbox.TabIndex = 8
    $objForm.Controls.Add($objTypeCheckbox)
    $objTypeCheckbox.Checked = $false
    #Add the new user to the default groups

    Get-Content C:\temp\input.txt | ForEach-Object {[void] $objListBox.Items.Add($_)}


    $objForm.Controls.Add($objListBox)

    $objForm.Topmost = $True


    $result = $objForm.ShowDialog()

    If ($objTypeCheckbox.Checked = $true)
      {
        $result = "Field Test"
      }
      else
      {
        $result = "Production"
      }
      $result
    if ($result -eq [System.Windows.Forms.DialogResult]::OK -and $objListBox.SelectedIndex -ge 0)
    {
    $i = 0
        foreach ($objItem in $objListbox.SelectedItems)
                {$selection = $objItem
                $final = "$selection + ", " + $result"
                $final | Out-File "C:\temp\output.txt" -Append
                $i++
                 }
            $objForm.Close()

        # Do something with $selection
    }


    Clear-Host


    OSLM ENGINEER - SCCM 2007 & 2012

    Saturday, March 11, 2017 9:21 PM
  • You are using $result in two places.  The second one overwrites the first use.  Use a different variable.


    \_(ツ)_/

    Saturday, March 11, 2017 9:23 PM
  • Here is how to do the logo as well as many ways to improve the form by using standard design and naming methods.  Your version is very old converted VBScript which doesn't work well.

    You will also have sizing and display  issues if you don't set up visual styles.

    function Get-Packages{
    	Param(
    		[Parameter(Mandatory)]
    		$InputFile
    	)
    	$list = Get-Content $InputFile
    	$script:mode = 'Production'
    	Add-Type -AssemblyName System.Windows.Forms
    
    	$script:form = New-Object System.Windows.Forms.Form
    	$form.Text = 'Software Staging Tool'
    	$form.Size = '650, 600'
    	$form.StartPosition = 'CenterScreen'
    	$form.Topmost = $True
    	$form.BackgroundImage.('http://www.designedsystemsonline.com/iis.png')
    	
    	$picturebox1 = New-Object System.Windows.Forms.PictureBox
    	$form.Controls.Add($picturebox1)
    	$picturebox1.Location = '5,5'
    	$picturebox1.Margin = '0, 0, 0, 0'
    	$picturebox1.Name = 'picturebox1'
    	$picturebox1.Size = '50, 50'
    	$picturebox1.SizeMode = 'StretchImage'
    	$picturebox1.TabStop = $False
    	$picturebox1.Load('http://www.designedsystemsonline.com/abstract-logo-with-colorful-leaves_1025-695.jpg')
    	
    	$label1 = New-Object System.Windows.Forms.Label
    	$form.Controls.Add($label1)
    	$label1.Location = '60, 20'
    	$label1.AutoSize = $true
    	$label1.TextAlign = 'MiddleLeft'
    	$label1.Font = 'Engravers MT, 16.2pt, style=Bold, Italic'
    	$label1.Text = 'Software Staging Tool'
    	
    	$buttonOK = New-Object System.Windows.Forms.Button
    	$form.Controls.Add($buttonOK)
    	$form.AcceptButton = $buttonOK
    	$buttonOK.Location = '200, 450'
    	$buttonOK.Text = 'OK'
    	$buttonOK.DialogResult = 'OK'
    
    	$buttonCancel = New-Object System.Windows.Forms.Button
    	$form.Controls.Add($buttonCancel)
    	$form.CancelButton = $buttonCancel
    	$buttonCancel.Location = '280, 450'
    	$buttonCancel.Text = 'Cancel'
    	$buttonCancel.DialogResult = 'Cancel'
    
    	$label2 = New-Object System.Windows.Forms.Label
    	$form.Controls.Add($label2)
    	$label2.Location = '10, 70'
    	$label2.AutoSize = $true
    	$label2.Text = 'Please select Package''s to Stage to:'
    	
    	$radiobuttonProduction = New-Object System.Windows.Forms.RadioButton
    	$form.Controls.Add($radiobuttonProduction)
    	$radiobuttonProduction.AutoSize = $true
    	$radiobuttonProduction.TextAlign = 'MiddleLeft'
    	$radiobuttonProduction.Location = '245,67'
    	$radiobuttonProduction.Text = 'Production'
    	$radiobuttonProduction.Checked = $true
    	$checkChanged = {
    		$script:mode = if($radiobuttonProduction.Checked){'Production'}else{'Field Test'}
        }
    	$radiobuttonProduction.add_CheckedChanged($checkChanged)
    	
    	$radiobuttonFieldtest = New-Object System.Windows.Forms.RadioButton
    	$form.Controls.Add($radiobuttonFieldtest)
    	$radiobuttonFieldtest.AutoSize = $true
    	$radiobuttonFieldtest.TextAlign = 'MiddleLeft'
    	$radiobuttonFieldtest.Location = '340,67'
    	$radiobuttonFieldtest.Text ='Field Test'
    	
    	$lisboxList = New-Object System.Windows.Forms.ListBox
    	$form.Controls.Add($lisboxList)
    	$lisboxList.Location = '10, 90'
    	$lisboxList.Size = '1100, 350'
    	$lisboxList.BackColor = 'cyan'
    	$lisboxList.Forecolor = 'blue'
    	$lisboxList.Font = 'Arial Black'
    	$lisboxList.SelectionMode = 'MultiSimple'
    	$lisboxList.Items.AddRange($list)
    
    
    	if ('Ok' -eq $form.ShowDialog()) {
    		$i = 0
    		$lisboxList.SelectedItems |
    			ForEach-Object{
    				[pscustomobject]@{
    					Index = $i++
    					Mode = $mode
    					Package = $_
    	            }
    	    	}
    	}
    }
    
    # by default PowerShell now gives us three ways to ru this
    #Get-Packages
    Get-Packages c:\temp\input.txt
    #Get-Packages c:\temp\input.txt | Export-Csv c:\temp\outfile.csv -notype
    


    \_(ツ)_/

    Sunday, March 12, 2017 12:58 AM
  • Thank you Jrv :)

    OSLM ENGINEER - SCCM 2007 & 2012

    Sunday, March 12, 2017 10:14 PM
  • Hello Jrv,

    My sript so far is below, and technically it works as expected, however i have an issue

    the moment i click OK Button the UI Gets closed and i want it to be open since after clicking OK, the script executes a function and does some task and should write log to a text box  which i created,

    How to prevent the UI of getting closed without hurting the functionality of OK Button or Cancel Button

    ==================================================================

    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
    [System.Windows.Forms.FormClosingEventHandler]

    Clear-History
    Clear-Host

    $currentdir = (Get-Location).Path
    $logpath = "C:\Development_Zone\Applications taging"

    # Erase existing input & output

    Remove-Item -Path "$currentdir\output.csv" -Force -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
    Remove-Item -Path "$currentdir\Applications.txt" -Force -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
    New-Item -Path "$currentdir\output.csv" -ItemType file -Force | out-null
    New-Item -Path "$currentdir\Applications.txt" -ItemType file -Force | out-null
    Remove-Item -Path "$currentdir\collections.csv" -Force -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
    New-Item -Path "$currentdir\collections.csv" -ItemType file -Force | out-null
    add-content -path "$currentdir\collections.csv" -Value "CollectionName,CollectionID" -Force -WarningAction SilentlyContinue -ErrorAction SilentlyContinue


    $SiteCode="DCA"
    $SCCMServer="XXX.com"
    # Get a list of collections and find the object to build the collection list.
    $Collections = (Get-WmiObject -ComputerName $SCCMServer  -Namespace "root\sms\site_$SiteCode" -Class 'SMS_Collection' | Select-Object Name,CollectionID)
    foreach($Collection in $Collections)
    {
    $cn = $Collection.Name;
    $CID = $Collection.CollectionID;
    $output = -join ($cn +","+$CID)
    Add-Content -Value $output -Path "$currentdir\collections.csv" -Force -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
    }

    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")

    Clear-History
    Clear-Host

    $backcolour = [System.Drawing.Image]::Fromfile((get-item 'C:\Development_Zone\Applications taging\Logo_Pic.png'));
    $objForm = New-Object System.Windows.Forms.Form
    $objForm.Text = "Software Staging Tool"
    $objForm.Size = New-Object System.Drawing.Size(1700,850)
    $objForm.BackColor = "White"
    $objForm.StartPosition = "CenterScreen"

    # Got rid of the block of code related to KeyPreview and KeyDown events.

    $OKButton = New-Object System.Windows.Forms.Button
    $OKButton.Location = New-Object System.Drawing.Size(310,500)
    $OKButton.Size = New-Object System.Drawing.Size(75,23)
    $OKButton.Text = "OK"

    # Got rid of the Click event for the OK button, and instead just assigned its DialogResult property to OK.
    $OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK

    $objForm.Controls.Add($OKButton)

    # Setting the form's AcceptButton property causes it to automatically intercept the Enter keystroke and
    # treat it as clicking the OK button (without having to write your own KeyDown events).
    $objForm.AcceptButton = $OKButton

    $CancelButton = New-Object System.Windows.Forms.Button
    $CancelButton.Location = New-Object System.Drawing.Size(390,500)
    $CancelButton.Size = New-Object System.Drawing.Size(75,23)
    $CancelButton.Text = "Cancel"

    # Got rid of the Click event for the Cancel button, and instead just assigned its DialogResult property to Cancel.
    $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel

    $objForm.Controls.Add($CancelButton)

    # Setting the form's CancelButton property causes it to automatically intercept the Escape keystroke and
    # treat it as clicking the OK button (without having to write your own KeyDown events).
    $objForm.CancelButton = $CancelButton

    $objLabel = New-Object System.Windows.Forms.Label
    $objLabel.Location = New-Object System.Drawing.Size(350,168)
    $objLabel.Size = New-Object System.Drawing.Size(280,25)
    $objLabel.Text = "Please select Collection:"
    $objForm.Controls.Add($objLabel)

    # to adjust the blue background

    $objListBox = New-Object System.Windows.Forms.ListBox
    $objListBox.Location = New-Object System.Drawing.Size(120,200)
    $objListBox.Size = New-Object System.Drawing.Size(600,1100)
    $objListBox.Height = 300

    $objListBox.BackColor = "cyan"
    $objListBox.Forecolor = "Blue"
    $objListBox.Font = "Arial Black"
    $objListbox.SelectionMode = "MultiSimple"


    ########### Info Box Field
    $outputBox = New-Object System.Windows.Forms.RichTextBox
    $outputBox.Location = New-Object System.Drawing.Size(55,535)
    $outputBox.Size = New-Object System.Drawing.Size(770,110)
    $outputBox.Font = New-Object System.Drawing.Font("Lucida Console",16)
    $outputBox.ForeColor = [Drawing.Color]::Green
    $outputBox.MultiLine = $True
    $outputBox.ReadOnly = $True
    $outputBox.ScrollBars = "Vertical"
    $outputBox.Text = " This tool will help you to Stage Applications, Hardware Drivers, Software Updates, Task Sequence from Development Environment to Production Environment."
    $objForm.Controls.Add($outputBox)
    ############################################## end text fields

    # To Display  Logo

    ########### PictureBox Label
    $PictureBoxLabel = New-Object System.Windows.Forms.Label
    $PictureBoxLabel.Text="Current Logon Screen"
    $PictureBoxLabel.Visible = $False
    $PictureBoxLabel.Location = New-Object System.Drawing.Size(20,10)
    $PictureBoxLabel.Size = New-Object System.Drawing.Size(30,50)
    $objForm.Controls.Add($PictureBoxLabel)
     
    ########### Input PictureBox
    $defbackground = [System.Drawing.Image]::Fromfile((get-item "$currentdir\Logo.png"));
    $PictureBox = New-Object System.Windows.Forms.PictureBox
    $PictureBox.Location = "680, 20"
    $pictureBox.Width = 158
    $pictureBox.Height = 90
    #$PictureBox.ClientSize = "480, 300"
    $PictureBox.Image = $defbackground
    #$PictureBox.BackColor = "Black"
    $PictureBox.SizeMode = "Zoom"
    $objForm.Controls.Add($PictureBox)

    #Drop Down to select Field Test / Production

    $objLabel2 = New-Object System.Windows.Forms.Label
    $objLabel2.Location = New-Object System.Drawing.Size(320,90)
    $objLabel2.Size = New-Object System.Drawing.Size(200,20)
    $objLabel2.Text = "Rollout Out to Field Test/ Production:"
    $objForm.Controls.Add($objLabel2)

    [array]$DropDownArray2 = "Field Test", "Production" , "10 % Rollout"

    $DropDown2 = new-object System.Windows.Forms.ComboBox
    $DropDown2.Location = new-object System.Drawing.Size(350,115)
    $DropDown2.Size = new-object System.Drawing.Size(130,30)

    ForEach ($Item2 in $DropDownArray2) {
        $DropDown2.Items.Add($Item2)
    }

    $objForm.Controls.Add($DropDown2)

    $DropDownLabel2 = new-object System.Windows.Forms.Label
    $DropDownLabel2.Location = new-object System.Drawing.Size(10,10)
    $DropDownLabel2.size = new-object System.Drawing.Size(100,20)
    $DropDownLabel2.Text = $args
    $args = $server
    $objForm.Controls.Add($DropDownLabel2)


    #Drop Down to select HW / SW

    $objLabel = New-Object System.Windows.Forms.Label
    $objLabel.Location = New-Object System.Drawing.Size(265,20)
    $objLabel.Size = New-Object System.Drawing.Size(480,20)
    $objLabel.Text = "Type of Package Hardware/Application/Task Sequence:"
    $objForm.Controls.Add($objLabel)

    [array]$DropDownArray = "Hardware", "Application", "Task Sequence", "Software Update"

    $DropDown = new-object System.Windows.Forms.ComboBox
    $DropDown.Location = new-object System.Drawing.Size(350,45)
    $DropDown.Size = new-object System.Drawing.Size(130,30)

    ForEach ($Item in $DropDownArray) {
        $DropDown.Items.Add($Item)
    }

    $objForm.Controls.Add($DropDown)

    $DropDownLabel = new-object System.Windows.Forms.Label
    $DropDownLabel.Location = new-object System.Drawing.Size(10,10)
    $DropDownLabel.size = new-object System.Drawing.Size(100,20)
    $DropDownLabel.Text = $args
    $args = $server
    $objForm.Controls.Add($DropDownLabel)


    #Add the new user to the default groups
    $csvim = import-csv -Path "$currentdir\collections.csv"
    $csvim.CollectionName | Sort-Object | ForEach-Object {[void] $objListBox.Items.Add($_)}  

    $objForm.Controls.Add($objListBox)

    $objForm.Topmost = $True


    $result = $objForm.ShowDialog()

    # Field test / Production result

    #$Form.Close()
    $result4 = $null
    if ($DropDown2.SelectedItem -eq $null)
    {
    $outputBox.text= " Please Select a Value from the Drop Box to Continue"
    pause
    }
    elseif ($DropDown2.SelectedItem -eq "Field Test")
    {
     $result4 =  "-FT $"+"true"
    }
    elseif ($DropDown2.SelectedItem -eq "Production")
    {
    $result4 =  "-RuleSet $"+"true"
    }
    elseif ($DropDown2.SelectedItem -eq "10 % Rollout")
    {
    $result4 =  "Ten"
    }


    # This Function Returns the Selected Value and their actions then Closes the Form

        
        #$Form.Close()
    $result3 = $null
    if($DropDown.SelectedItem -eq $null)
    {
    $outputBox.text = "Please Select a Value from the Drop Box to Continue"
    pause
    }
    if ($DropDown.SelectedItem -eq "Hardware")
    {
    $result3 =  "HW"
    }
    elseif ($DropDown.SelectedItem -eq "Application")
    {
    $result3 =  "SW"
    }
    elseif ($DropDown.SelectedItem -eq "Task Sequence")
    {
    $result3 = "TS"
    }        


    $i = 0

    if(($DropDown.SelectedItem -ne $null) -and ($DropDown2.SelectedItem -ne $null))
    {
        foreach ($objItem in $objListbox.SelectedItems)
                {$ok = $objItem
                $final = -join ("$ok"+","+"$result3"+","+"$result4")
                $final | Out-File "$currentdir\output.csv" -Encoding ascii -Append
                #CloneCollection -SourceCollectionName "$ok" -TargetStage "PCA:" -SourceStage "DCA:" -StageMode $result3 -RuleSet $true
                $i++
                 }
    }
    $output2 = import-csv -Path "$currentdir\output.csv"

    $outputBox.text= "final step"
    Start-Sleep 7000
            

        # Do something with $selection

    $objForm.Close()
    Read-Host -Prompt “Press Enter to exit”
    Clear-Host


    OSLM ENGINEER - SCCM 2007 & 2012

    Thursday, March 16, 2017 9:48 PM
  • Sorry, but this is not a "here's my code dump--please embark on a debugging session, find that elusive bug, and fix it for me" forum. Read this first:

    This forum is for scripting questions rather than script requests


    -- Bill Stewart [Bill_Stewart]

    Thursday, March 16, 2017 9:53 PM
    Moderator
  • Hello,

    My sript so far is below, and technically it works as expected, however i have an issue

    the moment i click OK Button the UI Gets closed and i want it to be open since after clicking OK, the script executes a function and does some task and should write log to a text box  which i created,

    How to prevent the UI of getting closed without hurting the functionality of OK Button or Cancel Button

    ==================================================================

    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
    [System.Windows.Forms.FormClosingEventHandler]

    Clear-History
    Clear-Host

    $currentdir = (Get-Location).Path
    $logpath = "C:\Development_Zone\Applications taging"

    # Erase existing input & output

    Remove-Item -Path "$currentdir\output.csv" -Force -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
    Remove-Item -Path "$currentdir\Applications.txt" -Force -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
    New-Item -Path "$currentdir\output.csv" -ItemType file -Force | out-null
    New-Item -Path "$currentdir\Applications.txt" -ItemType file -Force | out-null
    Remove-Item -Path "$currentdir\collections.csv" -Force -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
    New-Item -Path "$currentdir\collections.csv" -ItemType file -Force | out-null
    add-content -path "$currentdir\collections.csv" -Value "CollectionName,CollectionID" -Force -WarningAction SilentlyContinue -ErrorAction SilentlyContinue


    $SiteCode="DCA"
    $SCCMServer="XXX.com"
    # Get a list of collections and find the object to build the collection list.
    $Collections = (Get-WmiObject -ComputerName $SCCMServer  -Namespace "root\sms\site_$SiteCode" -Class 'SMS_Collection' | Select-Object Name,CollectionID)
    foreach($Collection in $Collections)
    {
    $cn = $Collection.Name;
    $CID = $Collection.CollectionID;
    $output = -join ($cn +","+$CID)
    Add-Content -Value $output -Path "$currentdir\collections.csv" -Force -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
    }

    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")

    Clear-History
    Clear-Host

    $backcolour = [System.Drawing.Image]::Fromfile((get-item 'C:\Development_Zone\Applications taging\Logo_Pic.png'));
    $objForm = New-Object System.Windows.Forms.Form
    $objForm.Text = "Software Staging Tool"
    $objForm.Size = New-Object System.Drawing.Size(1700,850)
    $objForm.BackColor = "White"
    $objForm.StartPosition = "CenterScreen"

    # Got rid of the block of code related to KeyPreview and KeyDown events.

    $OKButton = New-Object System.Windows.Forms.Button
    $OKButton.Location = New-Object System.Drawing.Size(310,500)
    $OKButton.Size = New-Object System.Drawing.Size(75,23)
    $OKButton.Text = "OK"

    # Got rid of the Click event for the OK button, and instead just assigned its DialogResult property to OK.
    $OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK

    $objForm.Controls.Add($OKButton)

    # Setting the form's AcceptButton property causes it to automatically intercept the Enter keystroke and
    # treat it as clicking the OK button (without having to write your own KeyDown events).
    $objForm.AcceptButton = $OKButton

    $CancelButton = New-Object System.Windows.Forms.Button
    $CancelButton.Location = New-Object System.Drawing.Size(390,500)
    $CancelButton.Size = New-Object System.Drawing.Size(75,23)
    $CancelButton.Text = "Cancel"

    # Got rid of the Click event for the Cancel button, and instead just assigned its DialogResult property to Cancel.
    $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel

    $objForm.Controls.Add($CancelButton)

    # Setting the form's CancelButton property causes it to automatically intercept the Escape keystroke and
    # treat it as clicking the OK button (without having to write your own KeyDown events).
    $objForm.CancelButton = $CancelButton

    $objLabel = New-Object System.Windows.Forms.Label
    $objLabel.Location = New-Object System.Drawing.Size(350,168)
    $objLabel.Size = New-Object System.Drawing.Size(280,25)
    $objLabel.Text = "Please select Collection:"
    $objForm.Controls.Add($objLabel)

    # to adjust the blue background

    $objListBox = New-Object System.Windows.Forms.ListBox
    $objListBox.Location = New-Object System.Drawing.Size(120,200)
    $objListBox.Size = New-Object System.Drawing.Size(600,1100)
    $objListBox.Height = 300

    $objListBox.BackColor = "cyan"
    $objListBox.Forecolor = "Blue"
    $objListBox.Font = "Arial Black"
    $objListbox.SelectionMode = "MultiSimple"


    ########### Info Box Field
    $outputBox = New-Object System.Windows.Forms.RichTextBox
    $outputBox.Location = New-Object System.Drawing.Size(55,535)
    $outputBox.Size = New-Object System.Drawing.Size(770,110)
    $outputBox.Font = New-Object System.Drawing.Font("Lucida Console",16)
    $outputBox.ForeColor = [Drawing.Color]::Green
    $outputBox.MultiLine = $True
    $outputBox.ReadOnly = $True
    $outputBox.ScrollBars = "Vertical"
    $outputBox.Text = " This tool will help you to Stage Applications, Hardware Drivers, Software Updates, Task Sequence from Development Environment to Production Environment."
    $objForm.Controls.Add($outputBox)
    ############################################## end text fields

    # To Display  Logo

    ########### PictureBox Label
    $PictureBoxLabel = New-Object System.Windows.Forms.Label
    $PictureBoxLabel.Text="Current Logon Screen"
    $PictureBoxLabel.Visible = $False
    $PictureBoxLabel.Location = New-Object System.Drawing.Size(20,10)
    $PictureBoxLabel.Size = New-Object System.Drawing.Size(30,50)
    $objForm.Controls.Add($PictureBoxLabel)
     
    ########### Input PictureBox
    $defbackground = [System.Drawing.Image]::Fromfile((get-item "$currentdir\Logo.png"));
    $PictureBox = New-Object System.Windows.Forms.PictureBox
    $PictureBox.Location = "680, 20"
    $pictureBox.Width = 158
    $pictureBox.Height = 90
    #$PictureBox.ClientSize = "480, 300"
    $PictureBox.Image = $defbackground
    #$PictureBox.BackColor = "Black"
    $PictureBox.SizeMode = "Zoom"
    $objForm.Controls.Add($PictureBox)

    #Drop Down to select Field Test / Production

    $objLabel2 = New-Object System.Windows.Forms.Label
    $objLabel2.Location = New-Object System.Drawing.Size(320,90)
    $objLabel2.Size = New-Object System.Drawing.Size(200,20)
    $objLabel2.Text = "Rollout Out to Field Test/ Production:"
    $objForm.Controls.Add($objLabel2)

    [array]$DropDownArray2 = "Field Test", "Production" , "10 % Rollout"

    $DropDown2 = new-object System.Windows.Forms.ComboBox
    $DropDown2.Location = new-object System.Drawing.Size(350,115)
    $DropDown2.Size = new-object System.Drawing.Size(130,30)

    ForEach ($Item2 in $DropDownArray2) {
        $DropDown2.Items.Add($Item2)
    }

    $objForm.Controls.Add($DropDown2)

    $DropDownLabel2 = new-object System.Windows.Forms.Label
    $DropDownLabel2.Location = new-object System.Drawing.Size(10,10)
    $DropDownLabel2.size = new-object System.Drawing.Size(100,20)
    $DropDownLabel2.Text = $args
    $args = $server
    $objForm.Controls.Add($DropDownLabel2)


    #Drop Down to select HW / SW

    $objLabel = New-Object System.Windows.Forms.Label
    $objLabel.Location = New-Object System.Drawing.Size(265,20)
    $objLabel.Size = New-Object System.Drawing.Size(480,20)
    $objLabel.Text = "Type of Package Hardware/Application/Task Sequence:"
    $objForm.Controls.Add($objLabel)

    [array]$DropDownArray = "Hardware", "Application", "Task Sequence", "Software Update"

    $DropDown = new-object System.Windows.Forms.ComboBox
    $DropDown.Location = new-object System.Drawing.Size(350,45)
    $DropDown.Size = new-object System.Drawing.Size(130,30)

    ForEach ($Item in $DropDownArray) {
        $DropDown.Items.Add($Item)
    }

    $objForm.Controls.Add($DropDown)

    $DropDownLabel = new-object System.Windows.Forms.Label
    $DropDownLabel.Location = new-object System.Drawing.Size(10,10)
    $DropDownLabel.size = new-object System.Drawing.Size(100,20)
    $DropDownLabel.Text = $args
    $args = $server
    $objForm.Controls.Add($DropDownLabel)


    #Add the new user to the default groups
    $csvim = import-csv -Path "$currentdir\collections.csv"
    $csvim.CollectionName | Sort-Object | ForEach-Object {[void] $objListBox.Items.Add($_)}  

    $objForm.Controls.Add($objListBox)

    $objForm.Topmost = $True


    $result = $objForm.ShowDialog()

    # Field test / Production result

    #$Form.Close()
    $result4 = $null
    if ($DropDown2.SelectedItem -eq $null)
    {
    $outputBox.text= " Please Select a Value from the Drop Box to Continue"
    pause
    }
    elseif ($DropDown2.SelectedItem -eq "Field Test")
    {
     $result4 =  "-FT $"+"true"
    }
    elseif ($DropDown2.SelectedItem -eq "Production")
    {
    $result4 =  "-RuleSet $"+"true"
    }
    elseif ($DropDown2.SelectedItem -eq "10 % Rollout")
    {
    $result4 =  "Ten"
    }


    # This Function Returns the Selected Value and their actions then Closes the Form

        
        #$Form.Close()
    $result3 = $null
    if($DropDown.SelectedItem -eq $null)
    {
    $outputBox.text = "Please Select a Value from the Drop Box to Continue"
    pause
    }
    if ($DropDown.SelectedItem -eq "Hardware")
    {
    $result3 =  "HW"
    }
    elseif ($DropDown.SelectedItem -eq "Application")
    {
    $result3 =  "SW"
    }
    elseif ($DropDown.SelectedItem -eq "Task Sequence")
    {
    $result3 = "TS"
    }        


    $i = 0

    if(($DropDown.SelectedItem -ne $null) -and ($DropDown2.SelectedItem -ne $null))
    {
        foreach ($objItem in $objListbox.SelectedItems)
                {$ok = $objItem
                $final = -join ("$ok"+","+"$result3"+","+"$result4")
                $final | Out-File "$currentdir\output.csv" -Encoding ascii -Append
                #CloneCollection -SourceCollectionName "$ok" -TargetStage "PCA:" -SourceStage "DCA:" -StageMode $result3 -RuleSet $true
                $i++
                 }
    }
    $output2 = import-csv -Path "$currentdir\output.csv"

    $outputBox.text= "final step"
    Start-Sleep 7000
            

        # Do something with $selection

    $objForm.Close()
    Read-Host -Prompt “Press Enter to exit”
    Clear-Host

    OSLM ENGINEER - SCCM 2007 & 2012

    • Merged by jrv Thursday, March 16, 2017 11:23 PM DUPLICATE
    Thursday, March 16, 2017 10:20 PM
  • This is what happens when you copy code that you don't understand. 

    # Setting the form's AcceptButton property causes it to automatically intercept the Enter keystroke and
    # treat it as clicking the OK button (without having to write your own KeyDown events).
    $objForm.AcceptButton = $OKButton


    \_(ツ)_/

    Thursday, March 16, 2017 11:18 PM
  • Hi Jrv,

    i hashed it ($objForm.AcceptButton = $OKButton)   but still the UI closes

    I need it to be open, what should i do ?

    Kind regards

    Tanoj


    OSLM ENGINEER - SCCM 2007 & 2012

    Thursday, March 16, 2017 11:37 PM
  • Stop being so helpless.  If you want to be a tech you have to learn how to llok things up and understand what they are. THere are 10s of thousnads of these things.  You need to learn about computer and windows technology and how to do basic research.

    https://msdn.microsoft.com/en-us/library/system.windows.forms.form.acceptbutton(v=vs.110).aspx

    It was the first entry on a simple Google search.


    \_(ツ)_/

    Friday, March 17, 2017 12:19 AM
  • Hello Jrv,

    i replaced ok button with below butto, which calls a function 'LoadCollection'

    everything loads perfectly however when i press this button i get below error (screenshot attached)

    i can say the load button works, because if i has out the task which the function has to do and just write host inside the function to check if the function is working or not, i can see it works ( pic attached )

    ########### Load Collection  Button
    $OpenFile = New-Object System.Windows.Forms.Button
    $OpenFile.Location = New-Object System.Drawing.Size(45,280)
    $OpenFile.Size = New-Object System.Drawing.Size(150,60)
    $OpenFile.backcolor = [System.Drawing.Color]::CornflowerBlue
    $OpenFile.Text = "Load Collection"
    $OpenFile.Add_Click({LoadCollection})
    $OpenFile.Cursor = [System.Windows.Forms.Cursors]::Hand
    $objForm.Controls.Add($OpenFile)

    ************* Exception Text **************
    System.Management.Automation.BreakException: System error.
       at System.Management.Automation.Interpreter.ThrowInstruction.Run(InterpretedFrame frame)
       at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
       at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
       at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
       at System.Management.Automation.Interpreter.Interpreter.Run(InterpretedFrame frame)
       at System.Management.Automation.Interpreter.LightLambda.RunVoid1[T0](T0 arg0)
       at System.Management.Automation.DlrScriptCommandProcessor.RunClause(Action`1 clause, Object dollarUnderbar, Object inputToProcess)
       at System.Management.Automation.CommandProcessorBase.DoComplete()
       at System.Management.Automation.Internal.PipelineProcessor.DoCompleteCore(CommandProcessorBase commandRequestingUpstreamCommandsToStop)
       at System.Management.Automation.Internal.PipelineProcessor.SynchronousExecuteEnumerate(Object input, Hashtable errorResults, Boolean enumerate)
       at System.Management.Automation.PipelineOps.InvokePipeline(Object input, Boolean ignoreInput, CommandParameterInternal[][] pipeElements, CommandBaseAst[] pipeElementAsts, CommandRedirection[][] commandRedirections, FunctionContext funcContext)
       at System.Management.Automation.Interpreter.ActionCallInstruction`6.Run(InterpretedFrame frame)
       at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
       at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
       at System.Management.Automation.Interpreter.Interpreter.Run(InterpretedFrame frame)
       at System.Management.Automation.Interpreter.LightLambda.RunVoid1[T0](T0 arg0)
       at System.Management.Automation.ScriptBlock.InvokeWithPipeImpl(ScriptBlockClauseToInvoke clauseToInvoke, Boolean createLocalScope, Dictionary`2 functionsToDefine, List`1 variablesToDefine, ErrorHandlingBehavior errorHandlingBehavior, Object dollarUnder, Object input, Object scriptThis, Pipe outputPipe, InvocationInfo invocationInfo, Object[] args)
       at System.Management.Automation.ScriptBlock.<>c__DisplayClassa.<InvokeWithPipe>b__8()
       at System.Management.Automation.Runspaces.RunspaceBase.RunActionIfNoRunningPipelinesWithThreadCheck(Action action)
       at System.Management.Automation.ScriptBlock.InvokeWithPipe(Boolean useLocalScope, Dictionary`2 functionsToDefine, List`1 variablesToDefine, ErrorHandlingBehavior errorHandlingBehavior, Object dollarUnder, Object input, Object scriptThis, Pipe outputPipe, InvocationInfo invocationInfo, Object[] args)
       at System.Management.Automation.ScriptBlock.InvokeAsDelegateHelper(Object dollarUnder, Object dollarThis, Object[] args)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


    ************** Loaded Assemblies **************


    OSLM ENGINEER - SCCM 2007 & 2012

    Saturday, March 18, 2017 12:45 AM
  • Run and debug in PowerShell.  Don't use another system.  You are not getting the correct error results after the exception is passed to the tool.


    \_(ツ)_/

    Saturday, March 18, 2017 12:49 AM
  • Thank you Jrv,

    i managed to fix the error , i added try catch to catch the error and write out

    now i am stuck into an other issue,when i press load button , it gathers info of collections and store it to a csv file, from here the $objListbox  should fetch and display on the gui, this was working untill i replaced OK Button with Load button, now it shows the box but its empty and does not display the collections

    Could you please help

    =====================

    $currentdir = (Get-Location).Path
    $logpath = "C:\Development_Zone\Applications taging"

    # Erase existing input & output
    Remove-Item -Path "$currentdir\Tenpercent.csv" -Force -ErrorAction

    SilentlyContinue -WarningAction SilentlyContinue
    Remove-Item -Path "$currentdir\output.csv" -Force -ErrorAction SilentlyContinue

    -WarningAction SilentlyContinue
    Remove-Item -Path "$currentdir\Applications.txt" -Force -ErrorAction

    SilentlyContinue -WarningAction SilentlyContinue
    New-Item -Path "$currentdir\Tenpercent.csv" -ItemType file -Force | out-null
    New-Item -Path "$currentdir\output.csv" -ItemType file -Force | out-null
    New-Item -Path "$currentdir\Applications.txt" -ItemType file -Force | out-null
    Remove-Item -Path "$currentdir\collections.csv" -Force -ErrorAction

    SilentlyContinue -WarningAction SilentlyContinue
    New-Item -Path "$currentdir\collections.csv" -ItemType file -Force | out-null
    add-content -path "$currentdir\collections.csv" -Value

    "CollectionName,CollectionID" -Force -WarningAction SilentlyContinue -

    ErrorAction SilentlyContinue

    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
    [System.Windows.Forms.FormClosingEventHandler]


    $backcolour = [System.Drawing.Image]::Fromfile((get-item 'C:\Development_Zone

    \Applications taging\Logo.png'));
    $objForm = New-Object System.Windows.Forms.Form
    $objForm.Text = "Software Staging Tool"
    $objForm.Size = New-Object System.Drawing.Size(1700,850)
    $objForm.BackColor = "White"
    $objForm.StartPosition = "CenterScreen"


    ########### Info Box Field
    $outputBox = New-Object System.Windows.Forms.RichTextBox
    $outputBox.Location = New-Object System.Drawing.Size(755,70)
    $outputBox.Size = New-Object System.Drawing.Size(770,590)
    $outputBox.Font = New-Object System.Drawing.Font("Lucida Console",10)
    $outputBox.ForeColor = [Drawing.Color]::Green
    $outputBox.MultiLine = $True
    $outputBox.ReadOnly = $True
    $outputBox.ScrollBars = "Vertical"
    $outputBox.Text = " This tool will help you to Stage Applications, Hardware

    Drivers, Software Updates, Task Sequence from Development Environment to

    Production Environment."
    $objForm.Controls.Add($outputBox)
    ############################################## end text fields


    ########### OpenFile Function - LoadCollection
    Function LoadColl {
    if($result2 -eq $D2p)
    {
    Write-Host "D2P - Selected"
    Try
    {
        c:
        Remove-Item -Path "$currentdir\collections.csv" -Force -ErrorAction

    SilentlyContinue -WarningAction SilentlyContinue
        New-Item -Path "$currentdir\collections.csv" -ItemType file -Force | out-

    null
        add-content -path "$currentdir\collections.csv" -Value

    "CollectionName,CollectionID" -Force -WarningAction SilentlyContinue -

    ErrorAction SilentlyContinue
        $SiteCode="DCA"
        $SCCMServer="ABCD.com"
        # Get a list of collections and find the object to build the collection

    list.
        $Collections = (Get-WmiObject -ComputerName $SCCMServer  -Namespace "root

    \sms\site_$SiteCode" -Class 'SMS_Collection' | Select-Object Name,CollectionID)
        foreach($Collection in $Collections)
        {
        $cn = $Collection.Name;
        $CID = $Collection.CollectionID;
        $output = -join ($cn +","+$CID)
        Add-Content -Value $output -Path "$currentdir\collections.csv" -Force -

    ErrorAction SilentlyContinue -WarningAction SilentlyContinue
        }

    }
    Catch
    {
      $outputBox.Text = "Exception Found"
      $outputBox.Text =  $_.Exception.Message
    }
    }
    elseif($result2 -eq $D2p)
    {
    Write-Host "P2P - Selected"
    Try
    {
        c:
        Remove-Item -Path "$currentdir\collections.csv" -Force -ErrorAction

    SilentlyContinue -WarningAction SilentlyContinue
        New-Item -Path "$currentdir\collections.csv" -ItemType file -Force | out-

    null
        add-content -path "$currentdir\collections.csv" -Value

    "CollectionName,CollectionID" -Force -WarningAction SilentlyContinue -

    ErrorAction SilentlyContinue
        $SiteCode="PCA"
        $SCCMServer="ABCD.com"
        # Get a list of collections and find the object to build the collection

    list.
        $Collections = (Get-WmiObject -ComputerName $SCCMServer  -Namespace "root

    \sms\site_$SiteCode" -Class 'SMS_Collection' | Select-Object Name,CollectionID)
        foreach($Collection in $Collections)
        {
        $cn = $Collection.Name;
        $CID = $Collection.CollectionID;
        $output = -join ($cn +","+$CID)
        Add-Content -Value $output -Path "$currentdir\collections.csv" -Force -

    ErrorAction SilentlyContinue -WarningAction SilentlyContinue
        }

    }
    Catch
    {
      $outputBox.Text = "Exception Found"
      $outputBox.Text =  $_.Exception.Message
    }
    }
    else
    {
    Write-Host "No Results for Dropdown D2P / P2P Selected"
    }
    }



    #Drop Down to select Dev to Prod/ Prod to Prod

    $objLabel3 = New-Object System.Windows.Forms.Label
    $objLabel3.Location = New-Object System.Drawing.Size(30,80)
    $objLabel3.Size = New-Object System.Drawing.Size(200,20)
    $objLabel3.Text = "select Dev to Prod/ Prod to Prod"
    $objForm.Controls.Add($objLabel3)

    [array]$DropDownArray3 = "Dev 2 Prod", "Prod 2 Prod"

    $DropDown3 = new-object System.Windows.Forms.ComboBox
    $DropDown3.Location = new-object System.Drawing.Size(60,100)
    $DropDown3.Size = new-object System.Drawing.Size(130,40)

    ForEach ($Item3 in $DropDownArray3) {
        $DropDown3.Items.Add($Item3)
    }

    $objForm.Controls.Add($DropDown3)

    $DropDownLabel3 = new-object System.Windows.Forms.Label
    $DropDownLabel3.Location = new-object System.Drawing.Size(10,10)
    $DropDownLabel3.size = new-object System.Drawing.Size(100,20)
    $DropDownLabel3.Text = $args
    $args = $server
    $objForm.Controls.Add($DropDownLabel3)


    # D 2 P / P 2 P

    $result2 = $null
    if($DropDown3.SelectedItem -eq $null)
    {
    $outputBox.text = "Please Select a Value from the Drop Box to Continue Dev 2

    Prod / Prod 2 Prod"
    }
    elseif ($DropDown3.SelectedItem -eq "Dev 2 Prod")
    {
    $result2 = $D2p
    write-host "Dev 2 Prod"
    $outputBox.text = "Development 2 production Selected"
    }
    elseif ($DropDown3.SelectedItem -eq "Prod 2 Prod")
    {
    $result2 = $P2P
    write-host "Prod 2 Prod"
    $outputBox.text = "Production 2 production Selected"
    }


    #Drop Down to select Field Test / Production

    $objLabel2 = New-Object System.Windows.Forms.Label
    $objLabel2.Location = New-Object System.Drawing.Size(30,140)
    $objLabel2.Size = New-Object System.Drawing.Size(200,20)
    $objLabel2.Text = "Rollout Out to Field Test/ Production:"
    $objForm.Controls.Add($objLabel2)

    [array]$DropDownArray2 = "Field Test", "Production" , "10 % Rollout"

    $DropDown2 = new-object System.Windows.Forms.ComboBox
    $DropDown2.Location = new-object System.Drawing.Size(60,160)
    $DropDown2.Size = new-object System.Drawing.Size(130,40)

    ForEach ($Item2 in $DropDownArray2) {
        $DropDown2.Items.Add($Item2)
    }

    $objForm.Controls.Add($DropDown2)

    $DropDownLabel2 = new-object System.Windows.Forms.Label
    $DropDownLabel2.Location = new-object System.Drawing.Size(10,10)
    $DropDownLabel2.size = new-object System.Drawing.Size(100,20)
    $DropDownLabel2.Text = $args
    $args = $server
    $objForm.Controls.Add($DropDownLabel2)


    #Drop Down to select HW / SW

    $objLabel = New-Object System.Windows.Forms.Label
    $objLabel.Location = New-Object System.Drawing.Size(20,199)
    $objLabel.Size = New-Object System.Drawing.Size(200,20)
    $objLabel.Text = "Hardware/Application/Task Sequence:"
    $objForm.Controls.Add($objLabel)

    [array]$DropDownArray = "Hardware", "Application", "Task Sequence", "Software

    Update"

    $DropDown = new-object System.Windows.Forms.ComboBox
    $DropDown.Location = new-object System.Drawing.Size(60,225)
    $DropDown.Size = new-object System.Drawing.Size(130,30)

    ForEach ($Item in $DropDownArray) {
        $DropDown.Items.Add($Item)
    }

    $objForm.Controls.Add($DropDown)

    $DropDownLabel = new-object System.Windows.Forms.Label
    $DropDownLabel.Location = new-object System.Drawing.Size(10,10)
    $DropDownLabel.size = new-object System.Drawing.Size(100,20)
    $DropDownLabel.Text = $args
    $args = $server
    $objForm.Controls.Add($DropDownLabel)


    ########### Load Collection  Button
    $OpenFile = New-Object System.Windows.Forms.Button
    $OpenFile.Location = New-Object System.Drawing.Size(45,280)
    $OpenFile.Size = New-Object System.Drawing.Size(150,60)
    $OpenFile.backcolor = [System.Drawing.Color]::CornflowerBlue
    $OpenFile.Text = "Load Collection"
    $OpenFile.Add_Click({LoadCollection})
    $OpenFile.Cursor = [System.Windows.Forms.Cursors]::Hand
    $objForm.Controls.Add($OpenFile)

    Function LoadCollection
    {
    LoadColl
    }

    ########### Check Deployment  Button
    $OpenFile = New-Object System.Windows.Forms.Button
    $OpenFile.Location = New-Object System.Drawing.Size(45,380)
    $OpenFile.Size = New-Object System.Drawing.Size(150,60)
    $OpenFile.backcolor = [System.Drawing.Color]::Pink
    $OpenFile.Text = "Check Deployment"
    $OpenFile.Add_Click({CheckDeployment})
    $OpenFile.Cursor = [System.Windows.Forms.Cursors]::Hand
    $objForm.Controls.Add($OpenFile)

    ########### OpenFile Function
    Function CheckDeployment {
    Write-Host "Check Deployment Button Clicked"
    }

    # Start Staging Button
    $OpenFile = New-Object System.Windows.Forms.Button
    $OpenFile.Location = New-Object System.Drawing.Size(45,480)
    $OpenFile.Size = New-Object System.Drawing.Size(150,60)
    $OpenFile.backcolor = [System.Drawing.Color]::Green
    $OpenFile.Text = "Start Staging"
    $OpenFile.Add_Click({OpenFile})
    $OpenFile.Cursor = [System.Windows.Forms.Cursors]::Hand
    $objForm.Controls.Add($OpenFile)

    ########### OpenFile Function
    Function OpenFile {
    Write-Host "Start Staging"
    }

    # Cancel Button
    $CancelButton = New-Object System.Windows.Forms.Button
    $CancelButton.Location = New-Object System.Drawing.Size(45,590)
    $CancelButton.Size = New-Object System.Drawing.Size(150,60)
    $CancelButton.backcolor = [System.Drawing.Color]::RED
    $CancelButton.Text = "Cancel"

    # Got rid of the Click event for the Cancel button, and instead just assigned

    its DialogResult property to Cancel.
    $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel

    $objForm.Controls.Add($CancelButton)

    # Setting the form's CancelButton property causes it to automatically intercept

    the Escape keystroke and
    # treat it as clicking the OK button (without having to write your own KeyDown

    events).
    $objForm.CancelButton = $CancelButton

    $objLabel = New-Object System.Windows.Forms.Label
    $objLabel.Location = New-Object System.Drawing.Size(400,38)
    $objLabel.Size = New-Object System.Drawing.Size(200,20)
    $objLabel.Text = "Please select Collection:"
    $objForm.Controls.Add($objLabel)

    # to adjust the blue background

    $objListBox = New-Object System.Windows.Forms.ListBox
    $objListBox.Location = New-Object System.Drawing.Size(230,70)
    $objListBox.Size = New-Object System.Drawing.Size(520,800)
    $objListBox.Height = 600
    $objListBox.BackColor = "cyan"
    $objListBox.Forecolor = "Blue"
    $objListBox.Font = "Arial Black"
    $objListbox.SelectionMode = "MultiSimple"

    # To Display Logo

    ########### PictureBox Label
    $PictureBoxLabel = New-Object System.Windows.Forms.Label
    $PictureBoxLabel.Text="Current Logon Screen"
    $PictureBoxLabel.Visible = $False
    $PictureBoxLabel.Location = New-Object System.Drawing.Size(20,10)
    $PictureBoxLabel.Size = New-Object System.Drawing.Size(30,50)
    $objForm.Controls.Add($PictureBoxLabel)
     
    ########### Input PictureBox
    $defbackground = [System.Drawing.Image]::Fromfile((get-item "$currentdir

    \Logo.png"));
    $PictureBox = New-Object System.Windows.Forms.PictureBox
    $PictureBox.Location = "1000, 10"
    $pictureBox.Width = 100
    $pictureBox.Height = 60
    #$PictureBox.ClientSize = "480, 300"
    $PictureBox.Image = $defbackground
    #$PictureBox.BackColor = "Black"
    $PictureBox.SizeMode = "Zoom"
    $objForm.Controls.Add($PictureBox)

    # Field test / Production result

    #$Form.Close()
    $result4 = $null
    if ($DropDown2.SelectedItem -eq $null)
    {
    $outputBox.text= " Please Select a Value from the Drop Box to Continue"
    }
    elseif ($DropDown2.SelectedItem -eq "Field Test")
    {
     $result4="$"+"true"
     $result5 = $False
    }
    elseif ($DropDown2.SelectedItem -eq "Production")
    {
    $result5 = $true
    $result4=$false
    }
    elseif ($DropDown2.SelectedItem -eq "10 % Rollout")
    {
    $result4 =  "$"+"true"
    $result5 = $False
    $Tenpercent = $true
    }


    # This Function Returns the Selected Value and their actions then Closes the

    Form

    #$Form.Close()
    $result3 = $null
    if($DropDown.SelectedItem -eq $null)
    {
    $outputBox.text = "Please Select a Value from the Drop Box to Continue"
    }
    if ($DropDown.SelectedItem -eq "Hardware")
    {
    $result3 =  "HW"
    }
    elseif ($DropDown.SelectedItem -eq "Application")
    {
    $result3 =  "SW"
    }
    elseif ($DropDown.SelectedItem -eq "Task Sequence")
    {
    $result3 = "TS"
    }        


    $i = 0

    if(($DropDown.SelectedItem -ne $null) -and ($DropDown2.SelectedItem -ne $null))
    {
        foreach ($objItem in $objListbox.SelectedItems)
                {$ok = $objItem
                $final = -join ("$ok"+","+"$result3"+","+"$result4")
                $final | Out-File "$currentdir\output.csv" -Encoding ascii -Append
                if($result4){
                #CloneCollection -SourceCollectionName "$ok" -TargetStage "PCA:" -

    SourceStage "DCA:" -StageMode $result3 -FT $result4
                }
                if($result5){
                #CloneCollection -SourceCollectionName "$ok" -TargetStage "PCA:" -

    SourceStage "DCA:" -StageMode $result3 -RuleSet $result5
                }
                $i++
                 }
    }
    $output2 = import-csv -Path "$currentdir\output.csv"

    $outputBox.text= "Staging Completed Will Proceed further if Ten Percent Rollout

    is selected"
    <#
    Function LoadForm {
    Write-Host "LoadForm Activated"
    $csvim = import-csv -Path "$currentdir\collections.csv"
    Write-host $csvim
    }
    #>

    #Add the new user to the default groups
    $csvim = import-csv -Path "$currentdir\collections.csv"
    $csvim.CollectionName | Sort-Object | ForEach-Object {[void]

    $objListBox.Items.Add($_)}  
    $objForm.Controls.Add($objListBox)
    $objForm.Topmost = $True
    $result = $objForm.ShowDialog()        


    OSLM ENGINEER - SCCM 2007 & 2012

    Saturday, March 18, 2017 11:19 AM
  • We are not going to keep incrementally editing your code with each change.  You will have to learn forms and learn how to debug forms.

    The code shows that you do not know how forms work or how PowerShell can make forms very easy. Take some time to experiment and read the documentation of Windows Forms.

    Here is a good reference for beginners: http://info.sapien.com/guis


    \_(ツ)_/

    Saturday, March 18, 2017 11:27 AM
  • I Agree with you, i am still a beginner in powershell and specially for Forms..

    could you please atlease give me hint why its not displaying the output like this (screenshot attached)

    Thank you

    Tanoj


    OSLM ENGINEER - SCCM 2007 & 2012

    Saturday, March 18, 2017 11:34 AM
  • First let's start at the very basic first step.  You need to stop posting numerous pictures and post your code once using the correct code posting tool as I have done above.  You code is unreadable and cannot be successfully copied with most browsers.

    The code you have posted is badly broken because the web site breaks lines.  The posting tool prevents this.  You also need to be sure it is properly indented so it is easy to see the flow and makes it easier to see mistakes.


    \_(ツ)_/


    • Edited by jrv Saturday, March 18, 2017 11:40 AM
    Saturday, March 18, 2017 11:37 AM
  • Remove all of these "-ErrorAction SilentlyContinue" and fix the errors.

    \_(ツ)_/

    Saturday, March 18, 2017 11:42 AM
  • Removed all of the "-ErrorAction SilentlyContinue" no errors, but no display also , blank

    As far i understand the below code is responsible to populate the list , but not sure why its not showing

    One other question is how to append the $outputBox.text  , currently it erasing existing line and only displays the last line

    #Add Collections to the Console Display
    $csvim = import-csv -Path "$currentdir\collections.csv"
    $csvim.CollectionName | Sort-Object | ForEach-Object {[void] $objListBox.Items.Add($_)}  
    $objForm.Controls.Add($objListBox)
    $objForm.Topmost = $True
    $result = $objForm.ShowDialog()

    Thank you


    OSLM ENGINEER - SCCM 2007 & 2012

    Saturday, March 18, 2017 12:04 PM
  • Please post your code as I have asked. Please also go to the link I posted and read the posts on how to use forms and look at the post for each control to learn how to use controls.  We cannot provide you with an incremental tutorial.  You need to put in some effort.

    You decorate your name with "engineer".  You need to apply good engineering discipline to coding or you will just become frustrated.  You cannot guess your way through these two difficult technologies.


    \_(ツ)_/

    Saturday, March 18, 2017 12:10 PM
  • One more bit of direction. All functional code in a form must be inside of a form event.  Code pasted in the middle of the form building code will not work as expected.  No code outside of an event will ever be executed while the form is displayed.  Functions that have been declared before the form is opened can be called from the form.


    \_(ツ)_/

    Saturday, March 18, 2017 12:17 PM
  • Please find the code below
    $currentdir = (Get-Location).Path
    $logpath = "C:\Development_Zone\Applications taging"
    
    # Erase existing input & output 
    Remove-Item -Path "$currentdir\Tenpercent.csv" -Force
    Remove-Item -Path "$currentdir\output.csv" -Force
    Remove-Item -Path "$currentdir\Applications.txt" -Force
    New-Item -Path "$currentdir\Tenpercent.csv" -ItemType file -Force | out-null
    New-Item -Path "$currentdir\output.csv" -ItemType file -Force | out-null
    New-Item -Path "$currentdir\Applications.txt" -ItemType file -Force | out-null
    Remove-Item -Path "$currentdir\collections.csv" -Force
    New-Item -Path "$currentdir\collections.csv" -ItemType file -Force | out-null
    add-content -path "$currentdir\collections.csv" -Value 
    
    "CollectionName,CollectionID" -Force
    
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
    [System.Windows.Forms.FormClosingEventHandler]
    
    
    $backcolour = [System.Drawing.Image]::Fromfile((get-item 'C:\Development_Zone
    
    \Applications taging\Logo.png'));
    $objForm = New-Object System.Windows.Forms.Form 
    $objForm.Text = "Software Staging Tool"
    $objForm.Size = New-Object System.Drawing.Size(1700,850)
    $objForm.BackColor = "White" 
    $objForm.StartPosition = "CenterScreen"
    
    
    ########### Info Box Field
    $outputBox = New-Object System.Windows.Forms.RichTextBox
    $outputBox.Location = New-Object System.Drawing.Size(755,70) 
    $outputBox.Size = New-Object System.Drawing.Size(770,590)
    $outputBox.Font = New-Object System.Drawing.Font("Lucida Console",10)
    $outputBox.ForeColor = [Drawing.Color]::Green
    $outputBox.MultiLine = $True
    $outputBox.ReadOnly = $True
    $outputBox.ScrollBars = "Vertical"
    $outputBox.Text = " This tool will help you to Stage Applications, Hardware 
    
    Drivers, Software Updates, Task Sequence from Development Environment to 
    
    Production Environment."
    $objForm.Controls.Add($outputBox)
    ############################################## end text fields
    
    
    ########### OpenFile Function - LoadCollection
    Function LoadColl {
    if($result2 -eq $D2p)
    {
    Write-Host "D2P - Selected"
    Try
    {
        Remove-Item -Path "$currentdir\collections.csv" -Force
        New-Item -Path "$currentdir\collections.csv" -ItemType file -Force | out-
    
    null
        add-content -path "$currentdir\collections.csv" -Value 
    
    "CollectionName,CollectionID" -Force
        $SiteCode="DCA"
        $SCCMServer="ABCD.com"
        # Get a list of collections and find the object to build the collection 
    
    list.
        $Collections = (Get-WmiObject -ComputerName $SCCMServer  -Namespace "root
    
    \sms\site_$SiteCode" -Class 'SMS_Collection' | Select-Object Name,CollectionID)
        foreach($Collection in $Collections)
        {
        $cn = $Collection.Name;
        $CID = $Collection.CollectionID;
        $output = -join ($cn +","+$CID)
        Add-Content -Value $output -Path "$currentdir\collections.csv" -Force
        }
    
    }
    Catch
    {
      $outputBox.Text = "Exception Found"
      $outputBox.Text =  $_.Exception.Message
    }
    }
    elseif($result2 -eq $D2p)
    {
    Write-Host "P2P - Selected"
    Try
    {
        Remove-Item -Path "$currentdir\collections.csv" -Force
        New-Item -Path "$currentdir\collections.csv" -ItemType file -Force | out-
    
    null
        add-content -path "$currentdir\collections.csv" -Value 
    
    "CollectionName,CollectionID" -Force
        $SiteCode="PCA"
        $SCCMServer="ABCD.com"
        # Get a list of collections and find the object to build the collection 
    
    list.
        $Collections = (Get-WmiObject -ComputerName $SCCMServer  -Namespace "root
    
    \sms\site_$SiteCode" -Class 'SMS_Collection' | Select-Object Name,CollectionID)
        foreach($Collection in $Collections)
        {
        $cn = $Collection.Name;
        $CID = $Collection.CollectionID;
        $output = -join ($cn +","+$CID)
        Add-Content -Value $output -Path "$currentdir\collections.csv" -Force
        }
    
    }
    Catch
    {
      $outputBox.Text = "Exception Found"
      $outputBox.Text =  $_.Exception.Message
    }
    }
    else
    {
    Write-Host "No Results for Dropdown D2P / P2P Selected"
    }
    }
    
    
    
    #Drop Down to select Dev to Prod/ Prod to Prod
    
    $objLabel3 = New-Object System.Windows.Forms.Label
    $objLabel3.Location = New-Object System.Drawing.Size(30,80) 
    $objLabel3.Size = New-Object System.Drawing.Size(200,20) 
    $objLabel3.Text = "select Dev to Prod/ Prod to Prod"
    $objForm.Controls.Add($objLabel3) 
    
    [array]$DropDownArray3 = "Dev 2 Prod", "Prod 2 Prod"
    
    $DropDown3 = new-object System.Windows.Forms.ComboBox
    $DropDown3.Location = new-object System.Drawing.Size(60,100)
    $DropDown3.Size = new-object System.Drawing.Size(130,40)
    
    ForEach ($Item3 in $DropDownArray3) {
    	$DropDown3.Items.Add($Item3)
    }
    
    $objForm.Controls.Add($DropDown3)
    
    $DropDownLabel3 = new-object System.Windows.Forms.Label
    $DropDownLabel3.Location = new-object System.Drawing.Size(10,10)
    $DropDownLabel3.size = new-object System.Drawing.Size(100,20)
    $DropDownLabel3.Text = $args
    $args = $server
    $objForm.Controls.Add($DropDownLabel3)
    
    
    # D 2 P / P 2 P
    
    $result2 = $null
    if($DropDown3.SelectedItem -eq $null)
    {
    $outputBox.text = "Please Select a Value from the Drop Box to Continue Dev 2 
    
    Prod / Prod 2 Prod"
    }
    elseif ($DropDown3.SelectedItem -eq "Dev 2 Prod") 
    {
    $result2 = $D2p
    write-host "Dev 2 Prod"
    $outputBox.text = "Development 2 production Selected"
    }
    elseif ($DropDown3.SelectedItem -eq "Prod 2 Prod") 
    {
    $result2 = $P2P
    write-host "Prod 2 Prod"
    $outputBox.text = "Production 2 production Selected"
    }
    
    
    #Drop Down to select Field Test / Production
    
    $objLabel2 = New-Object System.Windows.Forms.Label
    $objLabel2.Location = New-Object System.Drawing.Size(30,140) 
    $objLabel2.Size = New-Object System.Drawing.Size(200,20) 
    $objLabel2.Text = "Rollout Out to Field Test/ Production:"
    $objForm.Controls.Add($objLabel2) 
    
    [array]$DropDownArray2 = "Field Test", "Production" , "10 % Rollout"
    
    $DropDown2 = new-object System.Windows.Forms.ComboBox
    $DropDown2.Location = new-object System.Drawing.Size(60,160)
    $DropDown2.Size = new-object System.Drawing.Size(130,40)
    
    ForEach ($Item2 in $DropDownArray2) {
    	$DropDown2.Items.Add($Item2)
    }
    
    $objForm.Controls.Add($DropDown2)
    
    $DropDownLabel2 = new-object System.Windows.Forms.Label
    $DropDownLabel2.Location = new-object System.Drawing.Size(10,10)
    $DropDownLabel2.size = new-object System.Drawing.Size(100,20)
    $DropDownLabel2.Text = $args
    $args = $server
    $objForm.Controls.Add($DropDownLabel2)
    
    
    #Drop Down to select HW / SW
    
    $objLabel = New-Object System.Windows.Forms.Label
    $objLabel.Location = New-Object System.Drawing.Size(20,199) 
    $objLabel.Size = New-Object System.Drawing.Size(200,20) 
    $objLabel.Text = "Hardware/Application/Task Sequence:"
    $objForm.Controls.Add($objLabel) 
    
    [array]$DropDownArray = "Hardware", "Application", "Task Sequence", "Software 
    
    Update"
    
    $DropDown = new-object System.Windows.Forms.ComboBox
    $DropDown.Location = new-object System.Drawing.Size(60,225)
    $DropDown.Size = new-object System.Drawing.Size(130,30)
    
    ForEach ($Item in $DropDownArray) {
    	$DropDown.Items.Add($Item)
    }
    
    $objForm.Controls.Add($DropDown)
    
    $DropDownLabel = new-object System.Windows.Forms.Label
    $DropDownLabel.Location = new-object System.Drawing.Size(10,10)
    $DropDownLabel.size = new-object System.Drawing.Size(100,20)
    $DropDownLabel.Text = $args
    $args = $server
    $objForm.Controls.Add($DropDownLabel)
    
    
    ########### Load Collection  Button
    $OpenFile = New-Object System.Windows.Forms.Button
    $OpenFile.Location = New-Object System.Drawing.Size(45,280)
    $OpenFile.Size = New-Object System.Drawing.Size(150,60)
    $OpenFile.backcolor = [System.Drawing.Color]::CornflowerBlue
    $OpenFile.Text = "Load Collection"
    $OpenFile.Add_Click({LoadCollection})
    $OpenFile.Cursor = [System.Windows.Forms.Cursors]::Hand
    $objForm.Controls.Add($OpenFile)
    
    Function LoadCollection
    {
    LoadColl
    }
    
    ########### Check Deployment  Button
    $OpenFile = New-Object System.Windows.Forms.Button
    $OpenFile.Location = New-Object System.Drawing.Size(45,380)
    $OpenFile.Size = New-Object System.Drawing.Size(150,60)
    $OpenFile.backcolor = [System.Drawing.Color]::Pink
    $OpenFile.Text = "Check Deployment"
    $OpenFile.Add_Click({CheckDeployment})
    $OpenFile.Cursor = [System.Windows.Forms.Cursors]::Hand
    $objForm.Controls.Add($OpenFile)
    
    ########### OpenFile Function
    Function CheckDeployment {
    Write-Host "Check Deployment Button Clicked"
    }
    
    # Start Staging Button
    $OpenFile = New-Object System.Windows.Forms.Button
    $OpenFile.Location = New-Object System.Drawing.Size(45,480)
    $OpenFile.Size = New-Object System.Drawing.Size(150,60)
    $OpenFile.backcolor = [System.Drawing.Color]::Green
    $OpenFile.Text = "Start Staging"
    $OpenFile.Add_Click({OpenFile})
    $OpenFile.Cursor = [System.Windows.Forms.Cursors]::Hand
    $objForm.Controls.Add($OpenFile)
    
    ########### OpenFile Function
    Function OpenFile {
    Write-Host "Start Staging"
    }
    
    # Cancel Button
    $CancelButton = New-Object System.Windows.Forms.Button
    $CancelButton.Location = New-Object System.Drawing.Size(45,590)
    $CancelButton.Size = New-Object System.Drawing.Size(150,60)
    $CancelButton.backcolor = [System.Drawing.Color]::RED
    $CancelButton.Text = "Cancel"
    
    # Got rid of the Click event for the Cancel button, and instead just assigned 
    
    its DialogResult property to Cancel.
    $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
    
    $objForm.Controls.Add($CancelButton)
    
    # Setting the form's CancelButton property causes it to automatically intercept 
    
    the Escape keystroke and
    # treat it as clicking the OK button (without having to write your own KeyDown 
    
    events).
    $objForm.CancelButton = $CancelButton
    
    $objLabel = New-Object System.Windows.Forms.Label
    $objLabel.Location = New-Object System.Drawing.Size(400,38) 
    $objLabel.Size = New-Object System.Drawing.Size(200,20) 
    $objLabel.Text = "Please select Collection:"
    $objForm.Controls.Add($objLabel) 
    
    # to adjust the blue background
    
    $objListBox = New-Object System.Windows.Forms.ListBox 
    $objListBox.Location = New-Object System.Drawing.Size(230,70) 
    $objListBox.Size = New-Object System.Drawing.Size(520,800) 
    $objListBox.Height = 600
    $objListBox.BackColor = "cyan"
    $objListBox.Forecolor = "Blue"
    $objListBox.Font = "Arial Black"
    $objListbox.SelectionMode = "MultiSimple"
    
    # To Display Logo
    
    ########### PictureBox Label
    $PictureBoxLabel = New-Object System.Windows.Forms.Label
    $PictureBoxLabel.Text="Current Logon Screen"
    $PictureBoxLabel.Visible = $False
    $PictureBoxLabel.Location = New-Object System.Drawing.Size(20,10) 
    $PictureBoxLabel.Size = New-Object System.Drawing.Size(30,50) 
    $objForm.Controls.Add($PictureBoxLabel)
     
    ########### Input PictureBox
    $defbackground = [System.Drawing.Image]::Fromfile((get-item "$currentdir
    
    \Logo.png"));
    $PictureBox = New-Object System.Windows.Forms.PictureBox
    $PictureBox.Location = "1000, 10"
    $pictureBox.Width = 100
    $pictureBox.Height = 60
    #$PictureBox.ClientSize = "480, 300"
    $PictureBox.Image = $defbackground
    #$PictureBox.BackColor = "Black"
    $PictureBox.SizeMode = "Zoom"
    $objForm.Controls.Add($PictureBox)
    
    # Field test / Production result
    
    #$Form.Close()
    $result4 = $null
    if ($DropDown2.SelectedItem -eq $null)
    {
    $outputBox.text= " Please Select a Value from the Drop Box to Continue"
    } 
    elseif ($DropDown2.SelectedItem -eq "Field Test") 
    {
     $result4="$"+"true"
     $result5 = $False
    }
    elseif ($DropDown2.SelectedItem -eq "Production") 
    {
    $result5 = $true
    $result4=$false
    }
    elseif ($DropDown2.SelectedItem -eq "10 % Rollout") 
    {
    $result4 =  "$"+"true"
    $result5 = $False
    $Tenpercent = $true
    }
    
    
    # This Function Returns the Selected Value and their actions then Closes the 
    
    Form
    
    #$Form.Close()
    $result3 = $null
    if($DropDown.SelectedItem -eq $null)
    {
    $outputBox.text = "Please Select a Value from the Drop Box to Continue"
    }
    if ($DropDown.SelectedItem -eq "Hardware") 
    {
    $result3 =  "HW"
    }
    elseif ($DropDown.SelectedItem -eq "Application") 
    {
    $result3 =  "SW"
    }
    elseif ($DropDown.SelectedItem -eq "Task Sequence") 
    {
    $result3 = "TS"
    }    	
    
    
    $i = 0
    
    if(($DropDown.SelectedItem -ne $null) -and ($DropDown2.SelectedItem -ne $null))
    {
        foreach ($objItem in $objListbox.SelectedItems)
                {$ok = $objItem 
                $final = -join ("$ok"+","+"$result3"+","+"$result4")
                $final | Out-File "$currentdir\output.csv" -Encoding ascii -Append
                if($result4){
                #CloneCollection -SourceCollectionName "$ok" -TargetStage "PCA:" -
    
    SourceStage "DCA:" -StageMode $result3 -FT $result4
                }
                if($result5){
                #CloneCollection -SourceCollectionName "$ok" -TargetStage "PCA:" -
    
    SourceStage "DCA:" -StageMode $result3 -RuleSet $result5
                }
                $i++
                 }
    }
    $output2 = import-csv -Path "$currentdir\output.csv"
    
    $outputBox.text= "Staging Completed Will Proceed further if Ten Percent Rollout 
    
    is selected"
    <#
    Function LoadForm {
    Write-Host "LoadForm Activated"
    $csvim = import-csv -Path "$currentdir\collections.csv"
    Write-host $csvim
    }
    #>
    
    #Add Collections to the Console Display
    $csvim = import-csv -Path "$currentdir\collections.csv"
    $csvim.CollectionName | Sort-Object | ForEach-Object {[void] 
    
    $objListBox.Items.Add($_)}  
    $objForm.Controls.Add($objListBox) 
    $objForm.Topmost = $True
    $result = $objForm.ShowDialog()        
    


    OSLM ENGINEER - SCCM 2007 & 2012

    Saturday, March 18, 2017 12:26 PM
  • This is not a "here's my code dump--please fix all of this code for me so that it works the way I expect" forum. Read this first:

    This forum is for scripting questions rather than script requests


    -- Bill Stewart [Bill_Stewart]

    Saturday, March 18, 2017 2:36 PM
    Moderator
  • Sorry ... Fnally i fixed it myself and its woring like a charm

    Thank you for All your Support JRV

    Tanoj


    OSLM ENGINEER - SCCM 2007 & 2012

    Sunday, March 19, 2017 10:59 PM
  • You are supposed to mark the post that answered your question.  If you found your issues then post them and mark that as an answer.


    \_(ツ)_/

    Sunday, March 19, 2017 11:35 PM
  • Hello Jrv,

    The Main Form , is just to fill the parameters which the script 2 should use, i tried to invoke the second script like below from the main script of form, but still the form freezes

    what can i do so that the form does not freeze ?

    $params = @{result3=$result3;result4=”$result4”;result5=$result5;ok=$ok}
    & “.\Test2.ps1” @params

    Thank you

    Tanoj


    OSLM ENGINEER - SCCM 2007 & 2012

    Friday, March 24, 2017 5:35 PM
  • As long as the script is executing the form will be frozen so it all depends on what is in test2.ps1.


    \_(ツ)_/

    Friday, March 24, 2017 5:46 PM
  • Here is an example of how to build a form that doesn't freeze when doing a long task.  It also demonstrates the best way to approach building a form and how to ensure proper display of a form.

    https://1drv.ms/u/s!AjiiPtIUqzK_hYpNlZHrDsgVqgr-CA


    \_(ツ)_/

    Friday, March 24, 2017 6:26 PM
  • Hi

    is there a way that i can prevent this freezing, like the form just triggers the scrpt 2 and the script 2 runs independently

    Kind regards

    Tanoj


    OSLM ENGINEER - SCCM 2007 & 2012

    Friday, March 24, 2017 6:27 PM
  • Hi

    is there a way that i can prevent this freezing, like the form just triggers the scrpt 2 and the script 2 runs independently

    Kind regards

    Tanoj


    OSLM ENGINEER - SCCM 2007 & 2012


    You could use "Start-Job"

    \_(ツ)_/

    Friday, March 24, 2017 6:41 PM
  • Hello Jrv,

    The start-job works fine and the main form does not freeze, the second script performs action and has to send the status information to main form ($outputbox.Text += "`n" +)  , how to achieve  this and make sure the main form does not freeze

                $params = ($result3,$result4,$result5,$ok)

                "result3 is $result3,result4 is $result4,result5 is $result5,ok is $ok"
                
                Start-Job -FilePath .\test2.ps1 -ArgumentList $result3,$result4,$result5,$ok | Out-File $currentdir\Servers.txt -Append

    Thank you

    Tanoj


    OSLM ENGINEER - SCCM 2007 & 2012

    Sunday, March 26, 2017 12:52 PM
  • There is no way to understand what you are asking.


    \_(ツ)_/

    Sunday, March 26, 2017 3:34 PM
  • Hi Jrv,

    my question is exactly what is asked in this Forum

    i also see that he got a solution, two pieces of script , but i am not able to understand where to put this script   in script 1 ( main form script / console )  script2 ( this is where the actual task is executed )

    Thank you

    Tanoj


    OSLM ENGINEER - SCCM 2007 & 2012

    Sunday, March 26, 2017 4:11 PM
  • I am sorry but your question is too vague.  What is it that is not happening?  What errors are you getting?  Remember that this forum is about answering specific questions and not about providing designs or solutions.  You have to sort out the question and be able to state it clearly.

    \_(ツ)_/

    Sunday, March 26, 2017 4:27 PM
  • Hi Jrv,

    i am not getting any error, everything is working fine, i am just rying to redirect the write-host (output from 2nd script ) to form so that i can see the progress of the second script

    i am calling second script from 1st script using start-job as suggested by you and the form is not freezing, no errors.

    just want to display the output from second script on textbox of form

    Kind regards

    Tanoj


    OSLM ENGINEER - SCCM 2007 & 2012

    Sunday, March 26, 2017 9:49 PM
  • You cannot redirect Write-Host anywhere.


    \_(ツ)_/

    Sunday, March 26, 2017 10:33 PM