Saturday 12 September 2020

powershell 30 UI 3 comboBox, dataGridView

select drive from comboBox, dataGridView shows drive info

services

processes

 
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()

$Form                            = New-Object system.Windows.Forms.Form
$Form.ClientSize                 = New-Object System.Drawing.Point(1026,725)
$Form.text                       = "Form"
$Form.TopMost                    = $false

$DataGridView1                   = New-Object system.Windows.Forms.DataGridView
$DataGridView1.width             = 1013
$DataGridView1.height            = 657
$DataGridView1.location          = New-Object System.Drawing.Point(5,60)

$ComboBox1                       = New-Object system.Windows.Forms.ComboBox
$ComboBox1.text                  = "comboBox"
$ComboBox1.width                 = 356
$ComboBox1.height                = 20
@('Service','Process','Drive') | ForEach-Object {[void] $ComboBox1.Items.Add($_)}
$ComboBox1.location              = New-Object System.Drawing.Point(10,20)
$ComboBox1.Font                  = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$ComboBox1.BackColor             = [System.Drawing.ColorTranslator]::FromHtml("#f8e71c")

$Form.controls.AddRange(@($DataGridView1, $ComboBox1))

$ComboBox1.Add_SelectedIndexChanged( { comboBox_IndexChange })

function comboBox_IndexChange {
    $selection = $ComboBox1.SelectedItem.ToString()
    
    switch ($selection) {
        'Service'{ $DataGridView1.dataSource = Get-Service | ConvertTo-Datatable}
        'Process'{ $DataGridView1.dataSource = Get-Process | ConvertTo-Datatable}
        'Drive'{ $DataGridView1.dataSource = Get-PSDrive | ConvertTo-Datatable}
        Default {}
    }
}

function ConvertTo-DataTable {
    [CmdletBinding()]
    Param(
        [Parameter(Position=0, Mandatory=$true, ValueFromPipeline = $true)]
        [PSObject[]]$InputObject
    )

    Begin {
        $dataTable = New-Object System.Data.DataTable
        $first = $true

        function _GetSafeTypeName($type) {
            # internal helper function to return the correct typename for a datatable
            $types = @('System.Boolean', 'System.Byte', 'System.SByte', 'System.Char', 'System.Datetime',
                       'System.TimeSpan', 'System.Decimal', 'System.Double', 'System.Guid', 'System.Single')
            $ints  = @('System.Int16', 'System.Int32', 'System.Int64')
            $uints = @('System.UInt16', 'System.UInt32', 'System.UInt64')

            if ($types -contains $type) { return "$type" }
            # if the type is Int or UInt, always return the largest variety
            if ($ints  -contains $type) { return 'System.Int64' }
            if ($uints -contains $type) { return 'System.UInt64' }
            return 'System.String'
        }
    }
    Process {
        foreach ($object in $InputObject) {
            $dataRow = $dataTable.NewRow()
            foreach($property in $object.PSObject.Properties) {
                # read the data type for this property and make sure it is a valid type for a DataTable
                $dataType = _GetSafeTypeName $property.TypeNameOfValue
                # ensure the property name does not contain invalid characters
                $propertyName = $property.Name -replace '[\W\p{Pc}-[,]]', '_' -replace '_+', '_'
                if ($first) {
                    $dataColumn = New-Object System.Data.DataColumn $propertyName, $dataType
                    $dataTable.Columns.Add($dataColumn)
                }
                if ($property.Gettype().IsArray -or ($property.TypeNameOfValue -like '*collection*')) {
                    $dataRow.Item($propertyName) = $property.Value | ConvertTo-XML -As String -NoTypeInformation -Depth 1
                }
                else {
                    $value = if ($null -ne $property.Value) { $property.Value } else { [System.DBNull]::Value }
                    $dataRow.Item($propertyName) = $value -as $dataType
                }
            }
            $dataTable.Rows.Add($dataRow)
            $first = $false
        }
    }
    End {
        Write-Output @(,($dataTable))
    }
}
#Write your logic code here

[void]$Form.ShowDialog()

No comments:

Post a Comment