none
Ausgabe eines Skriptes an Skript mit ListBox übergeben? RRS feed

  • Frage

  • Guten Morgen

    zuerst etwas Code...

    #ORDNER
    
    'Support Ordner wird erstellt'
    
    mkdir C:\Support #Support Ordner erstellen 
    
    'Support Ordner erfolgreich erstellt'
    
    
    #KOPIEREN
    
    'TeamViewerQS 9 wird kopiert'
    
    Copy-Item T:\1-edv\Install\Software\TeamViewerQS\TeamViewerQS_v9.exe C:\Support\TeamViewerQS_v9.exe #TeamViewer Quick Support Modul nach C: kopieren
    
    'TeamViewerQS 9 erfolgreich kopiert'
    
    
    #VERKNUEPFUNG
    
    'TeamViewerQS 9 Verknuepfung wird erstellt'
    
    $wshShellObject = New-Object -com WScript.Shell #Erstellt das neue Objekt "Verknüpfung"
    $userProfileFolder = (get-childitem env:USERPROFILE).Value #Ruft den Profilordner des aktuellen Users auf
    $wshShellLink = $wshShellObject.CreateShortcut($userProfileFolder+"\Desktop\TeamViewerQS 9.lnk") #Speicherort Verknüpfung und Name der Verknüpfung
    $wshShellLink.TargetPath = "C:\Support\TeamViewerQS_v9.exe" #Verknüpfung - Ziel
    $wshShellLink.IconLocation = "C:\Support\TeamViewerQS_v9.exe" #Icon-Pfad
    $wshShellLink.WorkingDirectory = "C:\Support\" #Verknüpfung - Ausführen in
    $wshShellLink.Save() #Erstellen der Verknüpfung
    
    'TeamViewerQS 9 Verknuepfung erfolgreich erstellt'

    Dieses Skript lässt sich sauber ausführen. Ich bekomme zudem die gewünschten Textzeilen (z.B. "TeamViewerQS 8 Verknuepfung erfolgreich erstellt) in der Ausgabe angezeigt.

    Nun habe ich folgendes Test-Skript, dass mir eine grafische Oberfläche erzeugt und mit der ich das obere Script ausführen möchte.

    function GenerateForm {
    
        [reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
        [reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
    
        $form1 = New-Object System.Windows.Forms.Form
        $button1 = New-Object System.Windows.Forms.Button
        $listBox1 = New-Object System.Windows.Forms.ListBox
        $checkBox3 = New-Object System.Windows.Forms.CheckBox
        $checkBox2 = New-Object System.Windows.Forms.CheckBox
        $checkBox1 = New-Object System.Windows.Forms.CheckBox
        $InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
    
        $b1= $false
        $b2= $false
        $b3= $false
    
        #----------------------------------------------
        #Generated Event Script Blocks
        #----------------------------------------------
    
        $handler_button1_Click= 
            {
                $listBox1.Items.Clear();    
    
                #if ($checkBox1.Checked)    {  $listBox1.Items.Add( "Checkbox 1 is checked"  ) }
                if ($checkBox1.Checked)    { .\CopyTeamViewer9QS }
    
                if ($checkBox2.Checked)    {  $listBox1.Items.Add( "Checkbox 2 is checked"  ) }    
    
                if ($checkBox3.Checked)    {  $listBox1.Items.Add( "Checkbox 3 is checked"  ) }
    
                if ( !$checkBox1.Checked -and !$checkBox2.Checked -and !$checkBox3.Checked ) {   $listBox1.Items.Add("Bitte gewünschte Programme auswählen...")} 
            }
    
        $OnLoadForm_StateCorrection=
        {#Correct the initial state of the form to prevent the .Net maximized form issue
        $form1.WindowState = $InitialFormWindowState
        }
    
    
        #GUI
        $form1.Text = "Install Tool"
        $form1.Name = "form1"
        $form1.DataBindings.DefaultDataSourceUpdateMode = 0
        $System_Drawing_Size = New-Object System.Drawing.Size
        $System_Drawing_Size.Width = 450
        $System_Drawing_Size.Height = 236
        $form1.ClientSize = $System_Drawing_Size
    
        $button1.TabIndex = 4
        $button1.Name = "button1"
        $System_Drawing_Size = New-Object System.Drawing.Size
        $System_Drawing_Size.Width = 75
        $System_Drawing_Size.Height = 23
        $button1.Size = $System_Drawing_Size
        $button1.UseVisualStyleBackColor = $True
    
        $button1.Text = "Run Script"
    
        $System_Drawing_Point = New-Object System.Drawing.Point
        $System_Drawing_Point.X = 27
        $System_Drawing_Point.Y = 156
        $button1.Location = $System_Drawing_Point
        $button1.DataBindings.DefaultDataSourceUpdateMode = 0
        $button1.add_Click($handler_button1_Click)
    
        $form1.Controls.Add($button1)
    
        $listBox1.FormattingEnabled = $True
        $System_Drawing_Size = New-Object System.Drawing.Size
        $System_Drawing_Size.Width = 301
        $System_Drawing_Size.Height = 212
        $listBox1.Size = $System_Drawing_Size
        $listBox1.DataBindings.DefaultDataSourceUpdateMode = 0
        $listBox1.Name = "listBox1"
        $System_Drawing_Point = New-Object System.Drawing.Point
        $System_Drawing_Point.X = 137
        $System_Drawing_Point.Y = 13
        $listBox1.Location = $System_Drawing_Point
        $listBox1.TabIndex = 3
    
        $form1.Controls.Add($listBox1)
    
    
        $checkBox3.UseVisualStyleBackColor = $True
        $System_Drawing_Size = New-Object System.Drawing.Size
        $System_Drawing_Size.Width = 104
        $System_Drawing_Size.Height = 24
        $checkBox3.Size = $System_Drawing_Size
        $checkBox3.TabIndex = 2
        $checkBox3.Text = "CheckBox 3"
        $System_Drawing_Point = New-Object System.Drawing.Point
        $System_Drawing_Point.X = 27
        $System_Drawing_Point.Y = 75
        $checkBox3.Location = $System_Drawing_Point
        $checkBox3.DataBindings.DefaultDataSourceUpdateMode = 0
        $checkBox3.Name = "checkBox3"
    
        $form1.Controls.Add($checkBox3)
        
    
        $checkBox2.UseVisualStyleBackColor = $True
        $System_Drawing_Size = New-Object System.Drawing.Size
        $System_Drawing_Size.Width = 104
        $System_Drawing_Size.Height = 24
        $checkBox2.Size = $System_Drawing_Size
        $checkBox2.TabIndex = 1
        $checkBox2.Text = "CheckBox 2"
        $System_Drawing_Point = New-Object System.Drawing.Point
        $System_Drawing_Point.X = 27
        $System_Drawing_Point.Y = 44
        $checkBox2.Location = $System_Drawing_Point
        $checkBox2.DataBindings.DefaultDataSourceUpdateMode = 0
        $checkBox2.Name = "checkBox2"
    
        $form1.Controls.Add($checkBox2)
    
    
    
        $checkBox1.UseVisualStyleBackColor = $True
        $System_Drawing_Size = New-Object System.Drawing.Size
        $System_Drawing_Size.Width = 104
        $System_Drawing_Size.Height = 24
        $checkBox1.Size = $System_Drawing_Size
        $checkBox1.TabIndex = 0
        $checkBox1.Text = "CheckBox 1"
        $System_Drawing_Point = New-Object System.Drawing.Point
        $System_Drawing_Point.X = 27
        $System_Drawing_Point.Y = 13
        $checkBox1.Location = $System_Drawing_Point
        $checkBox1.DataBindings.DefaultDataSourceUpdateMode = 0
        $checkBox1.Name = "checkBox1"
    
        $form1.Controls.Add($checkBox1)
    
    
        #Save the initial state of the form
        $InitialFormWindowState = $form1.WindowState
        #Init the OnLoad event to correct the initial state of the form
        $form1.add_Load($OnLoadForm_StateCorrection)
        #Show the Form
        $form1.ShowDialog()| Out-Null
    
        } #End Function
    
    #Call the Function
    GenerateForm

    Sobald ich Checkbox 1 ausgewählt habe und das ganze starte, wird das erste Skript auch sauber ausgeführt, allerdings bekomme ich die gewünschte Ausgabe, wie in dem ersten Skript,  nicht in der Box angezeigt.

    Wie kann ich das Ganze lösen?

    Ich bedanke mich im voraus!

    Gruß Tobi


    • Bearbeitet TVTobi Donnerstag, 9. April 2015 08:53
    Donnerstag, 9. April 2015 06:58

Antworten

  • Das nachträgliche Hinzufügen von Listbox-items ist nicht so simpel.
    Aber warum benutzt du eine Listbox als Textbox? Ich würde das  mit einer Textbox oder RichTextbox lösen, die sind für diesen Einsatzzweck gedacht.
    Z.b. so:

     

        function GenerateForm {
    
            [reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
            [reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
    
            $form1 = New-Object System.Windows.Forms.Form
            $button1 = New-Object System.Windows.Forms.Button
            $textbox = New-Object System.Windows.Forms.Textbox 
            $checkBox3 = New-Object System.Windows.Forms.CheckBox
            $checkBox2 = New-Object System.Windows.Forms.CheckBox
            $checkBox1 = New-Object System.Windows.Forms.CheckBox
            $InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
    
            $b1= $false
            $b2= $false
            $b3= $false
    
            #----------------------------------------------
            #Generated Event Script Blocks
            #----------------------------------------------
    
            $handler_button1_Click= 
                {
                    $textbox.Items.Clear();    
    
                    #if ($checkBox1.Checked)    {  $textbox.AppendText( "Checkbox 1 is checked"  ) }
                    if ($checkBox1.Checked)    { .\CopyTeamViewer9QS }
    
                    if ($checkBox2.Checked)    {  $textbox.AppendText( "Checkbox 2 is checked`n"  ) }    
    
                    if ($checkBox3.Checked)    {  $textbox.AppendText( "Checkbox 3 is checked`n"  ) }
    
                    if ( !$checkBox1.Checked -and !$checkBox2.Checked -and !$checkBox3.Checked ) {   $textbox.AppendText("Bitte gewünschte Programme auswählen...`n")} 
                }
    
            $OnLoadForm_StateCorrection=
            {#Correct the initial state of the form to prevent the .Net maximized form issue
            $form1.WindowState = $InitialFormWindowState
            }
    
    
            #GUI
            $form1.Text = "Install Tool"
            $form1.Name = "form1"
            $form1.DataBindings.DefaultDataSourceUpdateMode = 0
            $System_Drawing_Size = New-Object System.Drawing.Size
            $System_Drawing_Size.Width = 450
            $System_Drawing_Size.Height = 236
            $form1.ClientSize = $System_Drawing_Size
    
            $button1.TabIndex = 4
            $button1.Name = "button1"
            $System_Drawing_Size = New-Object System.Drawing.Size
            $System_Drawing_Size.Width = 75
            $System_Drawing_Size.Height = 23
            $button1.Size = $System_Drawing_Size
            $button1.UseVisualStyleBackColor = $True
    
            $button1.Text = "Run Script"
    
            $System_Drawing_Point = New-Object System.Drawing.Point
            $System_Drawing_Point.X = 27
            $System_Drawing_Point.Y = 156
            $button1.Location = $System_Drawing_Point
            $button1.DataBindings.DefaultDataSourceUpdateMode = 0
            $button1.add_Click($handler_button1_Click)
    
            $form1.Controls.Add($button1)
    
            $textbox.Multiline = $True
            $System_Drawing_Size = New-Object System.Drawing.Size
            $System_Drawing_Size.Width = 301
            $System_Drawing_Size.Height = 212
            $textbox.Size = $System_Drawing_Size
            $textbox.DataBindings.DefaultDataSourceUpdateMode = 0
            $textbox.Name = "listBox1"
            $System_Drawing_Point = New-Object System.Drawing.Point
            $System_Drawing_Point.X = 137
            $System_Drawing_Point.Y = 13
            $textbox.Location = $System_Drawing_Point
            $textbox.TabIndex = 3
    
            $form1.Controls.Add($textbox)
    
    
            $checkBox3.UseVisualStyleBackColor = $True
            $System_Drawing_Size = New-Object System.Drawing.Size
            $System_Drawing_Size.Width = 104
            $System_Drawing_Size.Height = 24
            $checkBox3.Size = $System_Drawing_Size
            $checkBox3.TabIndex = 2
            $checkBox3.Text = "CheckBox 3"
            $System_Drawing_Point = New-Object System.Drawing.Point
            $System_Drawing_Point.X = 27
            $System_Drawing_Point.Y = 75
            $checkBox3.Location = $System_Drawing_Point
            $checkBox3.DataBindings.DefaultDataSourceUpdateMode = 0
            $checkBox3.Name = "checkBox3"
    
            $form1.Controls.Add($checkBox3)
            
    
            $checkBox2.UseVisualStyleBackColor = $True
            $System_Drawing_Size = New-Object System.Drawing.Size
            $System_Drawing_Size.Width = 104
            $System_Drawing_Size.Height = 24
            $checkBox2.Size = $System_Drawing_Size
            $checkBox2.TabIndex = 1
            $checkBox2.Text = "CheckBox 2"
            $System_Drawing_Point = New-Object System.Drawing.Point
            $System_Drawing_Point.X = 27
            $System_Drawing_Point.Y = 44
            $checkBox2.Location = $System_Drawing_Point
            $checkBox2.DataBindings.DefaultDataSourceUpdateMode = 0
            $checkBox2.Name = "checkBox2"
    
            $form1.Controls.Add($checkBox2)
    
    
    
            $checkBox1.UseVisualStyleBackColor = $True
            $System_Drawing_Size = New-Object System.Drawing.Size
            $System_Drawing_Size.Width = 104
            $System_Drawing_Size.Height = 24
            $checkBox1.Size = $System_Drawing_Size
            $checkBox1.TabIndex = 0
            $checkBox1.Text = "CheckBox 1"
            $System_Drawing_Point = New-Object System.Drawing.Point
            $System_Drawing_Point.X = 27
            $System_Drawing_Point.Y = 13
            $checkBox1.Location = $System_Drawing_Point
            $checkBox1.DataBindings.DefaultDataSourceUpdateMode = 0
            $checkBox1.Name = "checkBox1"
    
            $form1.Controls.Add($checkBox1)
    
    
            #Save the initial state of the form
            $InitialFormWindowState = $form1.WindowState
            #Init the OnLoad event to correct the initial state of the form
            $form1.add_Load($OnLoadForm_StateCorrection)
            #Show the Form
            $form1.ShowDialog()| Out-Null
    
            } #End Function
    
        #Call the Function
        GenerateForm
     

    Text fügst du mit $textbox.AppendText() hinzu. Achte drarauf das du Zeilenumbrüche ("`n") dort einfügst wo du sie haben möchtest. Oben habe ich das beispielhaft schonmal gemacht.

    Grüße, Denniver


    Blog: http://bytecookie.wordpress.com

    Kostenloser Powershell Snippet Manager v3: Link
    (Schneller, besser + einfacher scripten.)

    Hilf mit und markiere hilfreiche Beiträge mit dem "Abstimmen"-Button (links) und Beiträge die eine Frage von dir beantwortet haben, als "Antwort" (unten).
    Warum das Ganze? Hier gibts die Antwort.


    Donnerstag, 9. April 2015 12:53
    Moderator