none
Cant store Listbox.selecteditem in Variable RRS feed

  • Question

  • Hi Everybody,

    im build a littl Powershell Programm, and thought everything was going well, but now i hit a wall.

    Im Trying to store the selected item of 3 Listboxes in 3 variables but it wont work. if i Close the form and look into listbox11.selecteditem for example it Shows the value but while the form is open it wont store the value in the variable.

    I tryed so many sollutions but just cant seem to get it to work. maybe i dont see the error after looking at the script for so Long, i hope you can help me :-)

    $id=[System.Security.Principal.WindowsIdentity]::GetCurrent()
    $principal=new-object System.Security.Principal.WindowsPrincipal($id)
    if(!$principal.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)) {
    $powershell=[System.Diagnostics.Process]::GetCurrentProcess()
    $psi=new-object System.Diagnostics.ProcessStartInfo $powershell.path
    $Script=$MyInvocation.MyCommand.Path
    $prm=$script
    foreach($a in $args) {
        $prm+=' '+$a
        }
    $psi.Arguments=$prm
    $psi.Verb="runas"
        [System.Diagnostics.Process]::Start($psi) | Out-Null
    return;
    }    
    
    
    $Network = Get-NetAdapter | where status -eq up | Select-Object -ExpandProperty Name
    
    Function GetIP {
        Get-NetIPAddress -InterfaceAlias $Network | where IPaddress -Like *.*.*.*|
        select-object -Property ipaddress,interfacealias
        }
    
    $Interfaces = getip
    $IPS = $interfaces.ipaddress
    $Adapters = $interfaces.interfacealias
    
    
    Function GetHost {
        $GetHost = [System.Net.Dns]::GetHostEntry($IP).hostname
        $GetHost
    
    }
    $DNSHost = GetHost
    
    Function GetLocalHost{
        $GetlocalHost = Get-Content env:computername
        $GetlocalHost
    }
    $LOKHost = GetLocalHost
    
    Function Setip {
        $InterIndex = Get-NetIPAddress -InterfaceAlias $Network | select-object -exp InterfaceIndex | Select-Object -First 1
        Remove-NetIPAddress -IPAddress $IP
        new-NetIPAddress -Interfaceindex "$InterIndex" -IPAddress $EnterIP -PrefixLength 24
    }
    
    Function GetDesc{
        $GetDesc = Get-WmiObject -Class Win32_OperatingSystem |Select Description
        $GetDesc | Select-Object -ExpandProperty Description
    }
    $Desc = Getdesc
    
    Function SetDesc {
        $SetDesc = Set-ItemProperty -Path HKLM:\system\ControlSet001\Services\LanmanServer\Parameters -name srvcomment -value $NEWDesc
        }
    
    Function Sethost{
      Rename-Computer $newlokHost  
        }
    
    
    $wscDefinition =@”
    
            // WSC_SECURITY_PROVIDER as defined in Wscapi.h or http://msdn.microsoft.com/en-us/library/bb432509(v=vs.85).aspx
    
                  [Flags]
    
            public enum WSC_SECURITY_PROVIDER : int
    
            {
    
                WSC_SECURITY_PROVIDER_FIREWALL = 1,                      // The aggregation of all firewalls for this computer.
    
                WSC_SECURITY_PROVIDER_AUTOUPDATE_SETTINGS = 2,    // The automatic update settings for this computer.
    
                WSC_SECURITY_PROVIDER_ANTIVIRUS = 4,                     // The aggregation of all antivirus products for this computer.
    
                WSC_SECURITY_PROVIDER_ANTISPYWARE = 8,                   // The aggregation of all anti-spyware products for this computer.
    
                WSC_SECURITY_PROVIDER_INTERNET_SETTINGS = 16,     // The settings that restrict the access of web sites in each of the Internet zones for this computer.
    
                WSC_SECURITY_PROVIDER_USER_ACCOUNT_CONTROL = 32,  // The User Account Control (UAC) settings for this computer.
    
                WSC_SECURITY_PROVIDER_SERVICE = 64,                      // The running state of the WSC service on this computer.
    
                WSC_SECURITY_PROVIDER_NONE = 0,                                // None of the items that WSC monitors.
    
                         
    
                         // All of the items that the WSC monitors.
    
                WSC_SECURITY_PROVIDER_ALL = WSC_SECURITY_PROVIDER_FIREWALL | WSC_SECURITY_PROVIDER_AUTOUPDATE_SETTINGS | WSC_SECURITY_PROVIDER_ANTIVIRUS |
    
                WSC_SECURITY_PROVIDER_ANTISPYWARE | WSC_SECURITY_PROVIDER_INTERNET_SETTINGS | WSC_SECURITY_PROVIDER_USER_ACCOUNT_CONTROL |
    
                WSC_SECURITY_PROVIDER_SERVICE | WSC_SECURITY_PROVIDER_NONE
    
            }
    
     
    
            [Flags]
    
            public enum WSC_SECURITY_PROVIDER_HEALTH : int
    
            {
    
                WSC_SECURITY_PROVIDER_HEALTH_GOOD,                // The status of the security provider category is good and does not need user attention.
    
                WSC_SECURITY_PROVIDER_HEALTH_NOTMONITORED, // The status of the security provider category is not monitored by WSC. 
    
                WSC_SECURITY_PROVIDER_HEALTH_POOR,                // The status of the security provider category is poor and the computer may be at risk.
    
                WSC_SECURITY_PROVIDER_HEALTH_SNOOZE,              // The security provider category is in snooze state. Snooze indicates that WSC is not actively protecting the computer.
    
                WSC_SECURITY_PROVIDER_HEALTH_UNKNOWN
    
            }
    
     
    
                  // as defined in http://msdn.microsoft.com/en-us/library/bb432506(v=vs.85).aspx
    
            [DllImport("wscapi.dll")]
    
            private static extern int WscGetSecurityProviderHealth(int inValue, ref int outValue);
    
     
    
                  // code to call our interop function and return the relevant result based on what input value we provide
    
            public static WSC_SECURITY_PROVIDER_HEALTH GetSecurityProviderHealth(WSC_SECURITY_PROVIDER inputValue)
    
            {
    
                int inValue = (int)inputValue;
    
                int outValue = -1;
    
     
    
                int result = WscGetSecurityProviderHealth(inValue, ref outValue);
    
     
    
                foreach (WSC_SECURITY_PROVIDER_HEALTH wsph in Enum.GetValues(typeof(WSC_SECURITY_PROVIDER_HEALTH)))
    
                    if ((int)wsph == outValue) return wsph;
    
     
    
                return WSC_SECURITY_PROVIDER_HEALTH.WSC_SECURITY_PROVIDER_HEALTH_UNKNOWN;
    
            }
    
    “@
    
    $wscType=Add-Type -memberDefinition $wscDefinition -name “wscType” -UsingNamespace “System.Reflection”,“System.Diagnostics” -PassThru
    
    $Virstat = if ($wscType[0]::GetSecurityProviderHealth($wscType[1]::WSC_SECURITY_PROVIDER_ANTIVIRUS) -eq $wscType[2]::WSC_SECURITY_PROVIDER_HEALTH_GOOD)
    {
    
      “Anti-virus running and up-to-date.”
    
    }
    
    else
    {
      "Antivirus disabled or not up-to-Date"
            
            }
    
    #function GenerateForm {
    ########################################################################
    # Code Generated By: SAPIEN Technologies PrimalForms (Community Edition) v1.0.10.0
    # Generated On: 10.10.2014 14:43
    # Generated By: Markus.Soeller
    ########################################################################
    
    #region Import the Assemblies
    [System.Reflection.Assembly]::loadwithpartialname("System.Drawing") | Out-Null
    [System.Reflection.Assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
    #endregion
    
    #region Generated Form Objects
    $form1 = New-Object System.Windows.Forms.Form
    $button1 = New-Object System.Windows.Forms.Button
    $ListBox12 = New-Object System.Windows.Forms.ListBox
    $ListBox13 = New-Object System.Windows.Forms.ListBox
    $ListBox11 = New-Object System.Windows.Forms.ListBox
    $label8 = New-Object System.Windows.Forms.Label
    $label7 = New-Object System.Windows.Forms.Label
    $textBox3 = New-Object System.Windows.Forms.TextBox
    $textBox2 = New-Object System.Windows.Forms.TextBox
    $textBox1 = New-Object System.Windows.Forms.TextBox
    $listBox5 = New-Object System.Windows.Forms.ListBox
    $listBox4 = New-Object System.Windows.Forms.ListBox
    $listBox3 = New-Object System.Windows.Forms.ListBox
    $listBox2 = New-Object System.Windows.Forms.ListBox
    $button3 = New-Object System.Windows.Forms.Button
    $button2 = New-Object System.Windows.Forms.Button
    $statusBar1 = New-Object System.Windows.Forms.StatusBar
    $label6 = New-Object System.Windows.Forms.Label
    $label5 = New-Object System.Windows.Forms.Label
    $label4 = New-Object System.Windows.Forms.Label
    $label3 = New-Object System.Windows.Forms.Label
    $label2 = New-Object System.Windows.Forms.Label
    $label1 = New-Object System.Windows.Forms.Label
    $panel1 = New-Object System.Windows.Forms.Panel
    $button4 = New-Object System.Windows.Forms.Button
    $InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
    #endregion Generated Form Objects
    
    #----------------------------------------------
    #Generated Event Script Blocks
    #----------------------------------------------
    #Provide Custom Code for events specified in PrimalForms.
    $button1_OnClick= 
    {
    $form1.Close()
    
    }
    
    $handler_label3_Click= 
    {
    #TODO: Place custom script here
    
    }
    
    $handler_textBox1_TextChanged= 
    {
    #TODO: Place custom script here
    
    }
    
    $button4_OnClick= 
    {
    $SelectIp | out-host
    $selectInterface | out-host
    $selecttype | out-host
    }
    
    $handler_label2_Click= 
    {
    #TODO: Place custom script here
    
    }
    
    $handler_label6_Click= 
    {
    #TODO: Place custom script here
    
    }
    
    $handler_label5_Click= 
    {
    #TODO: Place custom script here
    
    }
    
    $handler_label4_Click= 
    {
    #TODO: Place custom script here
    
    }
    
    $button2_OnClick= 
    {
    $NEWDesc = $textBox2.Text
    $Listbox2.items.RemoveAt(0)
    setdesc
    $desc=getdesc
    $Listbox2.items.add($Desc)
    
    }
    
    $handler_label7_Click= 
    {
    #TODO: Place custom script here
    
    }
    
    $handler_label1_Click= 
    {
    #TODO: Place custom script here
    
    }
    
    $button3_OnClick= 
    {
    $NewlokHost = $textbox3.Text
    $Listbox3.items.RemoveAt(0)
    sethost
    $LOKHost = GetLocalHost
    $Listbox3.items.add($LOKHost)
    }
    
    $OnLoadForm_StateCorrection=
    {#Correct the initial state of the form to prevent the .Net maximized form issue
    	$form1.WindowState = $InitialFormWindowState
    }
    
    #----------------------------------------------
    #region Generated Form Code
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Height = 512
    $System_Drawing_Size.Width = 1010
    $form1.ClientSize = $System_Drawing_Size
    $form1.DataBindings.DefaultDataSourceUpdateMode = 0
    $form1.ForeColor = [System.Drawing.Color]::FromArgb(255,0,0,0)
    $form1.Name = "form1"
    $form1.Text = "After Deployment Check"
    
    $button1.BackColor = [System.Drawing.Color]::FromArgb(255,153,180,209)
    
    $button1.DataBindings.DefaultDataSourceUpdateMode = 0
    
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 786
    $System_Drawing_Point.Y = 421
    $button1.Location = $System_Drawing_Point
    $button1.Name = "button1"
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Height = 49
    $System_Drawing_Size.Width = 169
    $button1.Size = $System_Drawing_Size
    $button1.TabIndex = 24
    $button1.Text = "Exit Deployment Check"
    $button1.UseVisualStyleBackColor = $False
    $button1.add_Click($button1_OnClick)
    
    $form1.Controls.Add($button1)
    
    #------------------------------------------------------------------------------------------------
    
    $ListBox12.DataBindings.DefaultDataSourceUpdateMode = 0
    $ListBox12.FormattingEnabled = $True
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 162
    $System_Drawing_Point.Y = 295
    $ListBox12.Location = $System_Drawing_Point
    $ListBox12.Name = "ListBox12"
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Height = 49
    $System_Drawing_Size.Width = 133
    $ListBox12.Size = $System_Drawing_Size
    $ListBox12.TabIndex = 22
    $form1.Controls.Add($ListBox12)
    
    Foreach ($IP in $IPs) {
    $ListBox12.items.add($IP)
    }
    
    $ListBox12.add_click({$SelectIP = $ListBox12.SelectedItem})
    #--------------------------------------------------------------------------------------------------
    
    $ListBox13.DataBindings.DefaultDataSourceUpdateMode = 0
    $ListBox13.FormattingEnabled = $True
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 300
    $System_Drawing_Point.Y = 295
    $ListBox13.Location = $System_Drawing_Point
    $ListBox13.Name = "ListBox13"
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Height = 49
    $System_Drawing_Size.Width = 133
    $ListBox13.Size = $System_Drawing_Size
    $ListBox13.TabIndex = 24
    $form1.Controls.Add($ListBox13)
    
    Foreach ($Adapter in $Adapters) {
    $Listbox13.items.add($Adapter)
    }
    
    $listbox13.add_click({$selectinterface = $Listbox13.SelectedItem})
    #--------------------------------------------------------------------------------------------------
    
    $ListBox11.DataBindings.DefaultDataSourceUpdateMode = 0
    $ListBox11.FormattingEnabled = $True
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 160
    $System_Drawing_Point.Y = 357
    $ListBox11.Location = $System_Drawing_Point
    $ListBox11.Name = "ListBox1"
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Height = 49
    $System_Drawing_Size.Width = 278
    $ListBox11.Size = $System_Drawing_Size
    $ListBox11.TabIndex = 20
    
    $form1.Controls.Add($ListBox11)
    $InterfaceAliases="Internal","External"
    
    Foreach ($Interfacealias in $Interfacealiases) {
    $Listbox11.items.add($Interfacealias)
    }
    
    $ListBox11.add_click({$selecttype = $listbox11.selecteditem})
    
    #---------------------------------------------------------------------------------------------------
    
    $label8.BackColor = [System.Drawing.Color]::FromArgb(255,153,180,209)
    $label8.DataBindings.DefaultDataSourceUpdateMode = 0
    
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 13
    $System_Drawing_Point.Y = 357
    $label8.Location = $System_Drawing_Point
    $label8.Name = "label8"
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Height = 49
    $System_Drawing_Size.Width = 142
    $label8.Size = $System_Drawing_Size
    $label8.TabIndex = 19
    $label8.Text = "Neuer Interface Name:"
    
    $form1.Controls.Add($label8)
    
    #-------------------------------------------------------------------------------------------------------
    
    $label7.BackColor = [System.Drawing.Color]::FromArgb(255,153,180,209)
    $label7.DataBindings.DefaultDataSourceUpdateMode = 0
    $label7.ForeColor = [System.Drawing.Color]::FromArgb(255,0,0,0)
    
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 13
    $System_Drawing_Point.Y = 421
    $label7.Location = $System_Drawing_Point
    $label7.Name = "label7"
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Height = 27
    $System_Drawing_Size.Width = 142
    $label7.Size = $System_Drawing_Size
    $label7.TabIndex = 18
    $label7.Text = "Neue IP Adresse:"
    $label7.add_Click($handler_label7_Click)
    
    $form1.Controls.Add($label7)
    
    #---------------------------------------------------------------------------------------------------------
    
    $textBox3.DataBindings.DefaultDataSourceUpdateMode = 0
    $textBox3.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",12.75,0,3,0)
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 634
    $System_Drawing_Point.Y = 137
    $textBox3.Location = $System_Drawing_Point
    $textBox3.Name = "textBox3"
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Height = 27
    $System_Drawing_Size.Width = 321
    $textBox3.Size = $System_Drawing_Size
    $textBox3.TabIndex = 17
    
    $form1.Controls.Add($textBox3)
    
    #----------------------------------------------------------------------------------------------------------
    
    $textBox2.DataBindings.DefaultDataSourceUpdateMode = 0
    $textBox2.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",12.75,0,3,0)
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 634
    $System_Drawing_Point.Y = 98
    $textBox2.Location = $System_Drawing_Point
    $textBox2.Name = "textBox2"
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Height = 27
    $System_Drawing_Size.Width = 321
    $textBox2.Size = $System_Drawing_Size
    $textBox2.TabIndex = 16
    
    $form1.Controls.Add($textBox2)
    
    #-----------------------------------------------------------------------------------------------------------
    
    $textBox1.DataBindings.DefaultDataSourceUpdateMode = 0
    $textBox1.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",12.75,0,3,0)
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 160
    $System_Drawing_Point.Y = 421
    $textBox1.Location = $System_Drawing_Point
    $textBox1.Name = "textBox1"
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Height = 27
    $System_Drawing_Size.Width = 278
    $textBox1.Size = $System_Drawing_Size
    $textBox1.TabIndex = 15
    $textBox1.add_TextChanged($handler_textBox1_TextChanged)
    
    $form1.Controls.Add($textBox1)
    
    #------------------------------------------------------------------------------------------------------------
    
    $listBox5.DataBindings.DefaultDataSourceUpdateMode = 0
    $listBox5.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",12.75,0,3,0)
    $listBox5.FormattingEnabled = $True
    $listBox5.ItemHeight = 20
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 160
    $System_Drawing_Point.Y = 216
    $listBox5.Location = $System_Drawing_Point
    $listBox5.Name = "listBox5"
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Height = 24
    $System_Drawing_Size.Width = 276
    $listBox5.Size = $System_Drawing_Size
    $listBox5.TabIndex = 14
    
    $form1.Controls.Add($listBox5)
    $Listbox5.items.add($VIRStat)
    
    #-------------------------------------------------------------------------------------------------------------
    
    $listBox4.DataBindings.DefaultDataSourceUpdateMode = 0
    $listBox4.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",12.75,0,3,0)
    $listBox4.FormattingEnabled = $True
    $listBox4.ItemHeight = 20
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 160
    $System_Drawing_Point.Y = 178
    $listBox4.Location = $System_Drawing_Point
    $listBox4.Name = "listBox4"
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Height = 24
    $System_Drawing_Size.Width = 277
    $listBox4.Size = $System_Drawing_Size
    $listBox4.TabIndex = 13
    
    $form1.Controls.Add($listBox4)
    $Listbox4.items.add($DNSHost)
    
    #-------------------------------------------------------------------------------------------------------------
    
    $listBox3.DataBindings.DefaultDataSourceUpdateMode = 0
    $listBox3.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",12.75,0,3,0)
    $listBox3.FormattingEnabled = $True
    $listBox3.ItemHeight = 20
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 160
    $System_Drawing_Point.Y = 137
    $listBox3.Location = $System_Drawing_Point
    $listBox3.Name = "listBox3"
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Height = 24
    $System_Drawing_Size.Width = 277
    $listBox3.Size = $System_Drawing_Size
    $listBox3.TabIndex = 12
    
    $form1.Controls.Add($listBox3)
    $Listbox3.items.add($LOKHost)
    
    #-------------------------------------------------------------------------------------------------------------
    
    $listBox2.DataBindings.DefaultDataSourceUpdateMode = 0
    $listBox2.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",12.75,0,3,0)
    $listBox2.FormattingEnabled = $True
    $listBox2.ItemHeight = 20
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 161
    $System_Drawing_Point.Y = 97
    $listBox2.Location = $System_Drawing_Point
    $listBox2.Name = "listBox2"
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Height = 24
    $System_Drawing_Size.Width = 278
    $listBox2.Size = $System_Drawing_Size
    $listBox2.TabIndex = 11
    
    $form1.Controls.Add($listBox2)
    $Listbox2.items.add($Desc)
    
    #-------------------------------------------------------------------------------------------------------------
    
    $button3.DataBindings.DefaultDataSourceUpdateMode = 0
    
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 445
    $System_Drawing_Point.Y = 135
    $button3.Location = $System_Drawing_Point
    $button3.Name = "button3"
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Height = 32
    $System_Drawing_Size.Width = 183
    $button3.Size = $System_Drawing_Size
    $button3.TabIndex = 9
    $button3.Text = "Set Host Name"
    $button3.UseVisualStyleBackColor = $True
    $button3.add_Click($button3_OnClick)
    
    $form1.Controls.Add($button3)
    
    #-------------------------------------------------------------------------------------------------------------
    
    $button2.DataBindings.DefaultDataSourceUpdateMode = 0
    
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 445
    $System_Drawing_Point.Y = 97
    $button2.Location = $System_Drawing_Point
    $button2.Name = "button2"
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Height = 30
    $System_Drawing_Size.Width = 183
    $button2.Size = $System_Drawing_Size
    $button2.TabIndex = 8
    $button2.Text = "Set Computer Description"
    $button2.UseVisualStyleBackColor = $True
    $button2.add_Click($button2_OnClick)
    
    $form1.Controls.Add($button2)
    
    #-------------------------------------------------------------------------------------------------------------
    
    $statusBar1.DataBindings.DefaultDataSourceUpdateMode = 0
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 0
    $System_Drawing_Point.Y = 490
    $statusBar1.Location = $System_Drawing_Point
    $statusBar1.Name = "statusBar1"
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Height = 22
    $System_Drawing_Size.Width = 1010
    $statusBar1.Size = $System_Drawing_Size
    $statusBar1.TabIndex = 6
    $statusBar1.Text = "Deployment Check"
    
    $form1.Controls.Add($statusBar1)
    
    $label6.BackColor = [System.Drawing.Color]::FromArgb(255,153,180,209)
    $label6.DataBindings.DefaultDataSourceUpdateMode = 0
    
    
    #-------------------------------------------------------------------------------------------------------------
    
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 13
    $System_Drawing_Point.Y = 216
    $label6.Location = $System_Drawing_Point
    $label6.Name = "label6"
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Height = 30
    $System_Drawing_Size.Width = 142
    $label6.Size = $System_Drawing_Size
    $label6.TabIndex = 5
    $label6.Text = "Antiviren Status:"
    $label6.add_Click($handler_label6_Click)
    
    $form1.Controls.Add($label6)
    
    #-------------------------------------------------------------------------------------------------------------
    
    $label5.BackColor = [System.Drawing.Color]::FromArgb(255,153,180,209)
    $label5.DataBindings.DefaultDataSourceUpdateMode = 0
    
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 13
    $System_Drawing_Point.Y = 178
    $label5.Location = $System_Drawing_Point
    $label5.Name = "label5"
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Height = 30
    $System_Drawing_Size.Width = 142
    $label5.Size = $System_Drawing_Size
    $label5.TabIndex = 4
    $label5.Text = "Hostname [DNS]:"
    $label5.add_Click($handler_label5_Click)
    
    $form1.Controls.Add($label5)
    
    #-------------------------------------------------------------------------------------------------------------
    
    $label4.BackColor = [System.Drawing.Color]::FromArgb(255,153,180,209)
    $label4.DataBindings.DefaultDataSourceUpdateMode = 0
    
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 13
    $System_Drawing_Point.Y = 137
    $label4.Location = $System_Drawing_Point
    $label4.Name = "label4"
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Height = 32
    $System_Drawing_Size.Width = 142
    $label4.Size = $System_Drawing_Size
    $label4.TabIndex = 3
    $label4.Text = "Hostname [Lokal]:"
    $label4.add_Click($handler_label4_Click)
    
    $form1.Controls.Add($label4)
    
    #-------------------------------------------------------------------------------------------------------------
    
    $label3.BackColor = [System.Drawing.Color]::FromArgb(255,153,180,209)
    $label3.DataBindings.DefaultDataSourceUpdateMode = 0
    
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 13
    $System_Drawing_Point.Y = 97
    $label3.Location = $System_Drawing_Point
    $label3.Name = "label3"
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Height = 30
    $System_Drawing_Size.Width = 142
    $label3.Size = $System_Drawing_Size
    $label3.TabIndex = 2
    $label3.Text = "Computer Description:"
    $label3.add_Click($handler_label3_Click)
    
    $form1.Controls.Add($label3)
    
    #-------------------------------------------------------------------------------------------------------------
    
    $label2.BackColor = [System.Drawing.Color]::FromArgb(255,153,180,209)
    $label2.DataBindings.DefaultDataSourceUpdateMode = 0
    
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 13
    $System_Drawing_Point.Y = 295
    $label2.Location = $System_Drawing_Point
    $label2.Name = "label2"
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Height = 49
    $System_Drawing_Size.Width = 142
    $label2.Size = $System_Drawing_Size
    $label2.TabIndex = 1
    $label2.Text = "IP Adresse:"
    $label2.add_Click($handler_label2_Click)
    
    $form1.Controls.Add($label2)
    
    #-------------------------------------------------------------------------------------------------------------
    
    $label1.BackColor = [System.Drawing.Color]::FromArgb(255,153,180,209)
    $label1.DataBindings.DefaultDataSourceUpdateMode = 0
    
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 12
    $System_Drawing_Point.Y = 9
    $label1.Location = $System_Drawing_Point
    $label1.Name = "label1"
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Height = 20
    $System_Drawing_Size.Width = 143
    $label1.Size = $System_Drawing_Size
    $label1.TabIndex = 0
    $label1.Text = "Deployment Check"
    $label1.add_Click($handler_label1_Click)
    
    $form1.Controls.Add($label1)
    
    #-------------------------------------------------------------------------------------------------------------
    
    $panel1.BackColor = [System.Drawing.Color]::FromArgb(255,0,102,204)
    
    $panel1.DataBindings.DefaultDataSourceUpdateMode = 0
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 12
    $System_Drawing_Point.Y = 261
    $panel1.Location = $System_Drawing_Point
    $panel1.Name = "panel1"
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Height = 209
    $System_Drawing_Size.Width = 754
    $panel1.Size = $System_Drawing_Size
    $panel1.TabIndex = 23
    
    $form1.Controls.Add($panel1)
    
    #-------------------------------------------------------------------------------------------------------------
    
    $button4.DataBindings.DefaultDataSourceUpdateMode = 0
    
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 433
    $System_Drawing_Point.Y = 34
    $button4.Location = $System_Drawing_Point
    $button4.Name = "button4"
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Height = 49
    $System_Drawing_Size.Width = 183
    $button4.Size = $System_Drawing_Size
    $button4.TabIndex = 21
    $button4.Text = "Set Network Adapter"
    $button4.UseVisualStyleBackColor = $True
    $button4.add_Click($button4_OnClick)
    
    $panel1.Controls.Add($button4)
    
    #-------------------------------------------------------------------------------------------------------------
    
    #endregion Generated Form Code
    
    #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
    

    best regards markus

    Thursday, October 16, 2014 12:09 PM

Answers

  • I should also note specifically - you cannot get output from a form while the form is displayed.

    It is also not possible to understand your question.  Are you asking how to return the value of a listbox or how to determine the selected item?


    ¯\_(ツ)_/¯

    • Marked as answer by MavRaven Thursday, October 16, 2014 2:56 PM
    Thursday, October 16, 2014 2:17 PM
  • CE is a bit of an abandoned product but it will generate forms.

    TO return a form just create a global in the onload call.

    $global:ThisForm=$form1

    Now the form is available after it has closed.  Do not remove the function wrapper.

    Avoid wrapping everythingin functions as it make the code unmaintainable and leads to subtle, difficult to find, errors.

    Example - this is all of the code needed to replace 30 + lines of poorly written code:

    $adapters=Get-NetAdapter |?{$_.Status -eq 'Up'}|Get-NetIPAddress -AddressFamily IPv4
    $IPAddress=$adapters.IPAddresss
    $Adapters=$IPAddress.InterfaceAlias
    $DNSHost = [System.Net.Dns]::GetHostEntry($IP).hostname
    $LOKHost = Get-Content env:computername
    

    All of this should be in the "onload" and the results should be directly assigned to form objects.  Avoid interceding variables.


    ¯\_(ツ)_/¯

    • Marked as answer by MavRaven Thursday, October 16, 2014 3:04 PM
    Thursday, October 16, 2014 2:59 PM

All replies

  • I do not think anyone is going to try and decode 1000 lines of code.  You will need to create a simple example of what you are trying to d.

    Variables within forms are removed when the for closes.  YOU either need to devise a way to return the data as an object or you need to re trun  the form object and parse that.

    Search for "powershell returning  data from windows forms"


    ¯\_(ツ)_/¯

    Thursday, October 16, 2014 2:02 PM
  • A quick look shows that you have hacked a Sapien generated form.  By hacking it you have altered the fundamental behavior and design of the Sapien model.  While this is possible it is the worst way to accomplish your ends.

    Leave the formin a separate file.  Pass inany callvariables as a Parm statement and return an object that summarizes the result or return the form.

    Generally when designing with forms we would not do any processing outside of a form.  Trying to split this requires a good understanding of both technologies.  It will take you sometime.  Start with PowerShell. Until you are skilled try to keep your forms simple and your process simple.

    You would do best to post Sapien forms questions in the Sapien forum as their product is specific and does forms in a specific way..


    ¯\_(ツ)_/¯

    Thursday, October 16, 2014 2:12 PM
  • I should also note specifically - you cannot get output from a form while the form is displayed.

    It is also not possible to understand your question.  Are you asking how to return the value of a listbox or how to determine the selected item?


    ¯\_(ツ)_/¯

    • Marked as answer by MavRaven Thursday, October 16, 2014 2:56 PM
    Thursday, October 16, 2014 2:17 PM
  • Hi jrv,

    thanks for your replys and sorry for posting 1000 lines of code.

    i should have explained further, my question should have been why the variable where i wanted to store the value i selected in the listbox was empty after closing the form.

    But you already answered tha for me thanks, i wasnt aware that its not possible because i just started with winforms and powershell and before that only used powershell scripts and no forms.

    Keeping your answer in Mind i might already have the solution i was searching for.

    Besides that i think your right i shouldnt have hacked the sapien form, if i have the time i will go back to primalforms ce and export the form again and do a fresh start, might be better this way.

    Also not beeing able to get output while displaying a form was bugging me but thanks to you i know why it cant work.

    thanks i will try to solve my problem and post if im successfull :-)

    Thursday, October 16, 2014 2:38 PM
  • CE is a bit of an abandoned product but it will generate forms.

    TO return a form just create a global in the onload call.

    $global:ThisForm=$form1

    Now the form is available after it has closed.  Do not remove the function wrapper.

    Avoid wrapping everythingin functions as it make the code unmaintainable and leads to subtle, difficult to find, errors.

    Example - this is all of the code needed to replace 30 + lines of poorly written code:

    $adapters=Get-NetAdapter |?{$_.Status -eq 'Up'}|Get-NetIPAddress -AddressFamily IPv4
    $IPAddress=$adapters.IPAddresss
    $Adapters=$IPAddress.InterfaceAlias
    $DNSHost = [System.Net.Dns]::GetHostEntry($IP).hostname
    $LOKHost = Get-Content env:computername
    

    All of this should be in the "onload" and the results should be directly assigned to form objects.  Avoid interceding variables.


    ¯\_(ツ)_/¯

    • Marked as answer by MavRaven Thursday, October 16, 2014 3:04 PM
    Thursday, October 16, 2014 2:59 PM
  • Hi again,

    thanks again, i was able to solve my problem, the biggest problem was that i was looking for the value in the variables after the form closed but now i know that this is not possible.

    After knowing that i changed the action what my button is supposed to do, to the actual thing i wanted to do and it works :-)

    thank you so much i was trying and searching the internet for the past 2 days and couldnt find the answer while it was myself wo was the problem.

    still i think i will go back to primalforms ce and export the form again and do it cleaner this time :-)

    Thursday, October 16, 2014 3:00 PM
  • Example two:

    $button2_OnClick= 
    {
         $NEWDesc = $textBox2.Text
         $Listbox2.items.RemoveAt(0)
         setdesc
         $desc=getdesc
         $Listbox2.items.add($Desc)
    }
    

    Unnecessary.  What is it that you are trying do?  Why does this require two functions for a simple assignment?

    Things should be done in the handling control's event structure. Avoid using only buttons to execute code.


    ¯\_(ツ)_/¯

    Thursday, October 16, 2014 3:03 PM
  • CE is a bit of an abandoned product but it will generate forms.

    TO return a form just create a global in the onload call.

    $global:ThisForm=$form1

    Now the form is available after it has closed.  Do not remove the function wrapper.

    Avoid wrapping everythingin functions as it make the code unmaintainable and leads to subtle, difficult to find, errors.

    Example - this is all of the code needed to replace 30 + lines of poorly written code:

    $adapters=Get-NetAdapter |?{$_.Status -eq 'Up'}|Get-NetIPAddress -AddressFamily IPv4
    $IPAddress=$adapters.IPAddresss
    $Adapters=$IPAddress.InterfaceAlias
    $DNSHost = [System.Net.Dns]::GetHostEntry($IP).hostname
    $LOKHost = Get-Content env:computername
    

    All of this should be in the "onload" and the results should be directly assigned to form objects.  Avoid interceding variables.


    ¯\_(ツ)_/¯


    thanks i will go trough my script and clean it up, at the beginning i thought building functions was a good idea but i quickly realised that its not really helpful in this scenario so i will stop doing so in the future for similar projects. :-)
    Thursday, October 16, 2014 3:06 PM
  • Here is an example of how we would do the action:

    $button2_OnClick={
         # (???) $Listbox2.items.RemoveAt(0)
         $os=Get-WmiObject Win32_OperatingSystem
         $os.Description=$textBox2.Text
         $os.SetInfo()
         # (???) $Listbox2.items.add($Desc)
    }

    It is very unclear what you are trying to do to the listbox.


    ¯\_(ツ)_/¯

    Thursday, October 16, 2014 3:09 PM
  • Here is an example of how we would do the action:

    $button2_OnClick={
         # (???) $Listbox2.items.RemoveAt(0)
         $os=Get-WmiObject Win32_OperatingSystem
         $os.Description=$textBox2.Text
         $os.SetInfo()
         # (???) $Listbox2.items.add($Desc)
    }

    It is very unclear what you are trying to do to the listbox.


    ¯\_(ツ)_/¯

    i was trying to refresh the listbox by removing and adding the values because i dont know better.

    the Form ist supposed to be open even after i change something like the description and directly show what i changed without starting the form again.

    That is also why im using buttons to execute the codes.

    Thursday, October 16, 2014 3:14 PM
  • Here is another one with unnecessary complexity and vagueness:

    $button3_OnClick= {
        $Listbox3.items.RemoveAt(0) # (???)
        Rename-Computer $textbox3.Text  
        $Listbox3.items.add($textbox3.Text) # (???)
    }

    Note that rename computer is only useful after a restart.

    You would want to use a workflow to manage the application of changes.


    ¯\_(ツ)_/¯

    Thursday, October 16, 2014 3:14 PM
  • Here is an example of how we would do the action:

    $button2_OnClick={
         # (???) $Listbox2.items.RemoveAt(0)
         $os=Get-WmiObject Win32_OperatingSystem
         $os.Description=$textBox2.Text
         $os.SetInfo()
         # (???) $Listbox2.items.add($Desc)
    }

    It is very unclear what you are trying to do to the listbox.


    ¯\_(ツ)_/¯

    i was trying to refresh the listbox by removing and adding the values because i dont know better.

    the Form ist supposed to be open even after i change something like the description and directly show what i changed without starting the form again.

    That is also why im using buttons to execute the codes.

    Why do you need to refresh the listbox?   Just adding a value refreshes it.  Are you saying you are trying to remove an item?  Be clear.  It appears you do not understand how Windows controls are designed to work.


    ¯\_(ツ)_/¯

    Thursday, October 16, 2014 3:17 PM
  • sorry, yes i dont want the old value to still stand there in the listbox i want it to only show the new value after i changed the description or the ip address for example.

    Thursday, October 16, 2014 3:22 PM
  • All of your code for loading the listboxes is in the wrong place.  It all needs to be in the Form "Load" event.  THat is why thereis a "Form OnLoad" event.


    ¯\_(ツ)_/¯


    • Edited by jrv Thursday, October 16, 2014 3:32 PM
    Thursday, October 16, 2014 3:30 PM
  • Here is a good place to start to learn how to use forms:

    http://www.sapien.com/blog/topics/user-interface-design-for-administrators/


    ¯\_(ツ)_/¯

    Thursday, October 16, 2014 3:32 PM