Page 1 of 1

Create UI using PowerShell - DataGrid

Posted: Sat Apr 06, 2013 9:50 am
by jagank
Gurus, I am stuck here with datagrid. please please help!!

What am I trying to do:

I am trying to build a SQL Server Inventory for my company using powershell. Idea is to read out of AD and display it in a UI. Based on the server name selected by the user, I want give various options like system information, system update history, SQL Server Services on the system, Drive info, Databases and their physical paths and recovery model. This is just the brief outlook that I have.

I am a rookie in Powershell yet I have few Ideas I want to implement. Please help me..

Problem:

In order to achieve my goal, first step is to present the list of ad computers in the UI. I am stuck here. Below is the code:
PowerShell Code
Double-click the code block to select all.
function Get-ComputerList
{
  Import-Module ActiveDirectory
   $array = @(get-ADComputer -Filter {Name -like 'LAI*DB*' -OR  Name -like 'HOL*DB*' -OR Name -like 'LAIW2K*CL*' } -property * |Sort-Object Name `
             | Format-Table Name, Description,IPV4Address, OperatingSystem, OperatingSystemServicePack,Created,LastLogonDate -Wrap -AutoSize |Out-Default)
}
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
  
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Microsoft SQL Server Inventory Application"
$objForm.Size = New-Object System.Drawing.Size(1200,800)
$objForm.StartPosition = "CenterScreen"
$objForm.KeyPreview = $True
$objForm.Add_Shown({$objForm.Activate()})
 
$dataGrid1 = New-Object System.Windows.Forms.DataGrid
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 1200
$System_Drawing_Size.Height = 650
$dataGrid1.Size = $System_Drawing_Size
$dataGrid1.DataBindings.DefaultDataSourceUpdateMode = 0
$dataGrid1.HeaderForeColor = [System.Drawing.Color]::FromArgb(255,0,0,0)
$dataGrid1.Name = "dataGrid1"
$dataGrid1.DataMember = ""
$dataGrid1.TabIndex = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 0
$System_Drawing_Point.Y = 0
$dataGrid1.Location = $System_Drawing_Point
 
$items = Get-ComputerList
#$dataGrid1.Rows.Add($items)
foreach($row in $items)
{
 $newrow = [array]$row
   $newrow
 $dataGrid1.Rows.Add($newrow)
}
 
 
$objForm.Controls.Add($dataGrid1)
 
[void] $objForm.showdialog()
Error Message:
[You cannot call a method on a null-valued expression.
At X:\Projects\PowerShellScripts\Work Scripts\ SQL Server Inventory Manager.ps1:38 char:21
+ $dataGrid1.Rows.Add <<<< ($newrow)
+ CategoryInfo : InvalidOperation: (Add:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
]

Please help

Re: Create UI using PowerShell - DataGrid

Posted: Sat Apr 06, 2013 11:44 am
by jvierra
Start by fixing this:
PowerShell Code
Double-click the code block to select all.
function Get-ComputerList{
  Import-Module ActiveDirectory
   get-ADComputer -Filter {Name -like 'LAI*DB*' -OR  Name -like 'HOL*DB*' -OR Name -like 'LAIW2K*CL*' } -property * |
   Sort-Object Name | 
   Select-Object Name, Description,IPV4Address, OperatingSystem, OperatingSystemServicePack,Created,LastLogonDate
}
This returns what you are looking for.

It is clear that you copied and pasted various bits without an understanding of what they were doing. To learn how to use controls you can look at the blog posts on controls.

I am not sure why you would use a datagrid to display a list of computers. It makes more sense to just display a listbox of names.

Re: Create UI using PowerShell - DataGrid

Posted: Sat Apr 06, 2013 12:28 pm
by jvierra
Here is a simple demo of how to load a grid from a collection of objects:
PowerShell Code
Double-click the code block to select all.
#Generated Form Function
function GenerateForm {
	########################################################################
	# Code Generated By: SAPIEN Technologies PrimalForms (Community Edition) v1.0.8.0
	# Generated On: 4/6/2013 3:10 PM
	# Generated By: jvierra
	########################################################################
	
	#region Import the Assemblies
	[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
	[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
	#endregion
	
	#region Generated Form Objects
	$form1 = New-Object System.Windows.Forms.Form
	$button1 = New-Object System.Windows.Forms.Button
	$dataGrid1 = New-Object System.Windows.Forms.DataGrid
	$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= {
	#TODO: Place custom script here
	
	}

	$handler_form1_Load= 
	{
   	$list=[system.collections.arraylist](dir c:\ | select name, mode, creationtime)
   	$datagrid1.DataSource=$list
	
	}
	
	$OnLoadForm_StateCorrection=
	{#Correct the initial state of the form to prevent the .Net maximized form issue
		$form1.WindowState = $InitialFormWindowState
	}

	#----------------------------------------------
	#region Generated Form Code
	$form1.Text = "Primal Form"
	$form1.Name = "form1"
	$form1.DataBindings.DefaultDataSourceUpdateMode = 0
	$System_Drawing_Size = New-Object System.Drawing.Size
	$System_Drawing_Size.Width = 616
	$System_Drawing_Size.Height = 512
	$form1.ClientSize = $System_Drawing_Size
	$form1.add_Load($handler_form1_Load)
	
	$button1.TabIndex = 1
	$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 = $False
	
	$button1.Text = "Ok"
	
	$System_Drawing_Point = New-Object System.Drawing.Point
	$System_Drawing_Point.X = 507
	$System_Drawing_Point.Y = 469
	$button1.Location = $System_Drawing_Point
	$button1.DataBindings.DefaultDataSourceUpdateMode = 0
	$button1.DialogResult = 1
	$button1.add_Click($button1_OnClick)
	
	$form1.Controls.Add($button1)
	
	$System_Drawing_Size = New-Object System.Drawing.Size
	$System_Drawing_Size.Width = 600
	$System_Drawing_Size.Height = 433
	$dataGrid1.Size = $System_Drawing_Size
	$dataGrid1.DataBindings.DefaultDataSourceUpdateMode = 0
	$dataGrid1.HeaderForeColor = [System.Drawing.Color]::FromArgb(255,0,0,0)
	$dataGrid1.Name = "dataGrid1"
	$dataGrid1.DataMember = ""
	$dataGrid1.TabIndex = 0
	$System_Drawing_Point = New-Object System.Drawing.Point
	$System_Drawing_Point.X = 12
	$System_Drawing_Point.Y = 12
	$dataGrid1.Location = $System_Drawing_Point
	
	$form1.Controls.Add($dataGrid1)
	
	#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
All of the code is generated by PrimalFOrms CE and two lines area added with an editor. The two lines are:
PowerShell Code
Double-click the code block to select all.
$list=[system.collections.arraylist](dir c:\ | select name, mode, creationtime)
   	$datagrid1.DataSource=$list
That is all it takes. Just wrap the obejcts in an arraylist and use as the datasource. Everything else is automatic.

Re: Create UI using PowerShell - DataGrid

Posted: Sun Apr 07, 2013 1:01 am
by jagank
jvierra, I admit that I have used help online and pasted most of the code. could you please route me to an article or websites where I can learn more about controls. I would love to learn!

Will wait for your reply..

Thanks
Jagan K

Re: Create UI using PowerShell - DataGrid

Posted: Sun Apr 07, 2013 11:49 am
by jvierra
This sites blog is a good place to satrt:
http://www.sapien.com/blog/topics/spotl ... -controls/

Also look into the evluation software as it is also a good way to get started.
http://www.sapien.com/downloads