Poweshell Form and Functions

This topic contains 0 replies, has 1 voice, and was last updated by Profile photo of Forums Archives Forums Archives 5 years, 5 months ago.

  • Author
    Posts
  • #5466

    by lhead001 at 2013-01-23 23:39:19

    I have been working on some helpdesk forms to automate some tasks. I am having some troubles getting everything to display on the form (labels) and the return isn't what was expected.

    Import-Module ActiveDirectory

    Function Manufacturer {

    Get-WmiObject Win32_Bios -ComputerName $objComboBox.SelectedItem | Select-Object Manufacturer

    }

    Function SerialNumber {

    Get-WmiObject Win32_Bios -ComputerName $objComboBox.SelectedItem | Select-Object SerialNumber

    }

    Function Version {

    Get-WmiObject Win32_Bios -ComputerName $objComboBox.SelectedItem | Select-Object SMBIOSBIOSVersion

    }

    ############################
    # Form Creation #
    ############################

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

    #----------------------------------------------
    #region Generated Form Objects
    #----------------------------------------------

    [System.Windows.Forms.Application]::EnableVisualStyles()

    $objForm = New-Object System.Windows.Forms.Form
    $objComboBox = New-Object System.Windows.Forms.ComboBox
    $objButtonOK = New-Object System.Windows.Forms.Button
    $objLabelManufacturer = New-Object System.Windows.Forms.Label
    $objTextManufacturer = New-Object System.Windows.Forms.TextBox
    $objLabelSerialNumber = New-Object System.Windows.Forms.Label
    $objTextSerialNumber = New-Object System.Windows.Forms.TextBox
    $objLabelVersion = New-Object System.Windows.Forms.Label
    $objTextVersion = New-Object System.Windows.Forms.TextBox
    $objButtonCancel = New-Object System.Windows.Forms.Button

    $objForm.ClientSize = New-Object System.Drawing.Size(407, 390)
    $objForm.TopMost = $true
    $objForm.StartPosition = "CenterScreen"
    $objForm.Text = "BIOS Information"

    $computerNames = Get-ADGroupMember "" -Recursive | ForEach-Object {$_.Name}

    $objComboBox.Location = New-Object System.Drawing.Point(25, 55)
    $objComboBox.Size = New-Object System.Drawing.Size(350, 310)

    foreach($computer in $computerNames)
    {
    $objComboBox.Items.add($computer)
    }
    $objForm.Controls.Add($objComboBox)

    $objButtonOK.Location = New-Object System.Drawing.Point(25, 20)
    $objButtonOK.Size = New-Object System.Drawing.Size (98, 23)
    $objButtonOK.Text = "OK"
    $objButtonOK.add_Click({$objTextManufacturer.Text = Manufacturer;$objTextSerialNumber.Text = SerialNumber;$objTextVersion.Text = Version})
    $objForm.Controls.Add($objButtonOK)

    $objButtonCancel.Location = New-Object System.Drawing.Size(200,20)
    $objButtonCancel.Size = New-Object System.Drawing.Size(98,23)
    $objButtonCancel.Text = "Cancel"
    $objButtonCancel.Add_Click({$objForm.Close()})
    $objForm.Controls.Add($objButtonCancel)

    $objLabelManufacturer.Location = New-Object System.Drawing.Point(25, 90)
    $objLabelManufacturer.Size = New-Object System.Drawing.Size(100, 200)
    $objLabelManufacturer.Text = "Manufacturer"
    $objForm.Controls.Add($objLabelManufacturer)

    $objTextManufacturer.Location = New-Object System.Drawing.Point(127, 90)
    $objTextManufacturer.Size = New-Object System.Drawing.Size(250, 200)
    $objTextManufacturer.Text = ""
    $objForm.Controls.Add($objTextManufacturer)

    $objLabelSerialNumber.Location = New-Object System.Drawing.Point(25, 135)
    $objLabelSerialNumber.Size = New-Object System.Drawing.Size(100, 200)
    $objLabelSerialNumber.Text = "Serial Number"
    $objForm.Controls.Add($objLabelSerialNumber)

    $objTextSerialNumber.Location = New-Object System.Drawing.Point(127, 135)
    $objTextSerialNumber.Size = New-Object System.Drawing.Size(250, 200)
    $objTextSerialNumber.Text = ""
    $objForm.Controls.Add($objTextSerialNumber)

    $objLabelVersion.Location = New-Object System.Drawing.Point(25, 180)
    $objLabelVersion.Size = New-Object System.Drawing.Size(100, 200)
    $objLabelVersion.Text = "Version"
    $objForm.Controls.Add($objLabelVersion)

    $objTextVersion.Location = New-Object System.Drawing.Point(127, 180)
    $objTextVersion.Size = New-Object System.Drawing.Size(250, 200)
    $objTextVersion.Text = ""
    $objForm.Controls.Add($objTextVersion)

    $objForm.Add_Shown({$objForm.Activate()})
    [void]$objForm.showdialog()

    The form loads and allows me to choose from the combo box and then allows me to run the functions, but the return is not what was expected. For instance the return for the BIOS Version is [quote]@{SMBIOSBIOSVersion=786G6 v01.15}[/quote]

    I was only expecting everything after the equals sign.

    I am sure there is a way to streamline the functions as well, but my coding abilities are limited. Any help you can provide will be greatly appreciated.

    Thanks in advance,

    Lowell

    by sunnyc7 at 2013-01-24 07:48:53

    lhead001
    You need to include -ExpandProperty after your select.

    —CODE—-[code2=powershell]Function Manufacturer {

    Get-WmiObject Win32_Bios -ComputerName $objComboBox.SelectedItem | Select-Object -ExpandProperty Manufacturer

    }

    Function SerialNumber {

    Get-WmiObject Win32_Bios -ComputerName $objComboBox.SelectedItem | Select-Object -ExpandProperty SerialNumber

    }

    Function Version {

    Get-WmiObject Win32_Bios -ComputerName $objComboBox.SelectedItem | Select-Object -ExpandProperty SMBIOSBIOSVersion

    }[/code2]

    by lhead001 at 2013-01-24 18:26:18

    sunnyC7,

    Perfect. Worked great for getting the text to display correctly in the text boxes, but I am still having troubles with the labels for Serial Number and Version. They are not displaying. Attaching a link to a screen shot of the form.

    https://skydrive.live.com/redir?resid=9 ... zyqfX35srI

    Lowell

You must be logged in to reply to this topic.