Function GetProcesses{
}
Function GetProcesses{ ……………_
This was the summary situation which we ended up with from the last post.
An approximate schematic summary of the situation at which we have so far arrived atheader1 |
header2 |
A Header |
Last Header |
<-- This bit with the created “column” is part of the main ListView object |
|
0\ 345 |
SubItem |
SubItem |
SubItem |
<-- This brown thing is a ListViewItem object. It has an Item number of 0,
and an Item identifier/name of 345 |
Item 0 |
1\ 232 |
SubItem |
SubItem |
SubItem |
<-- This blue thing is a ListViewItem object. It has an Item number of 1,
and an Item identifier/name of 232 |
Item 1 |
2\ 36 |
SubItem |
SubItem |
SubItem |
<-- This purple thing is a ListViewItem object. It has an Item number of 2
and an Item identifier/name of 36 |
Item 2 |
__
In the above schematic we are showing 4 objects. The last three belong to the first one, ( after they have been ned to it ).
The values in the first column somehow belong to the main ListView object.
SubItems are Added to the ListViewItems
Everything we need comes from early on in the function via
$Processes = Get-Process | Select-Object Id,ProcessName,Handles,NPM,PM,WS,VM,CPU,Path, # and I think this is getting us an array of objects.
Write-Host $Processes
will get us one of these for every Process
@{Id=5800; ProcessName=AggregatorHost; Handles=88; NPM=6112; PM=1200128; WS=2211840; VM=2203375075328; CPU=1,0625; Path=C:\WINDOWS\System32\AggregatorHost.exe}
Similarly in PowerShell typing $Processes would get us a lot of things like this
Code:
# Id : 5800
# ProcessName : AggregatorHost
# Handles : 88
# NPM : 6112
# PM : 1200128
# WS : 2211840
# VM : 2203375075328
# CPU : 1,0625
# Path : C:\WINDOWS\System32\AggregatorHost.exe
The next line,
( and a similar one used a bit further down in the main function Loop for all processes, ForEach ($Process in $Processes){ ),
could do with some elaboration possibly
$AProcessProperties = $Processes[0].psObject.Properties
( _____ $Process.psObject.Properties )
The way the script uses those last two lines, is such that $AProcessProperties is going to be the same as the first one of $Process.psObject.Properties , because that lattter is used in ForEach ($Process in $Processes){
psObject
The best guess I can give for this at the moment is this:
Things generally in PowerShell are objects, or can usually be taken/ used as if they were.
The .psObject for a thing is some sort of wrapper allowing the PowerShell command type things to somehow make use of the newest sort of OOP stuff which often goes under the use of the word .Net
Anoother approximation could be to say it returns something similar to the Class of the thing we apply it to. It then follows that the .Properties is going to return the object things.
As it happens we seem to be going around in circles in the given script that accompanies the video:
We started with these, Id,ProcessName,Handles,NPM,PM,WS,VM,CPU,Path , and the two script bits using the .psObject seem to have the main purpose of returning them.
Perhaps I can save a lot of time by giving a version of the given script, and then a script that seems to do exactly the same!
Code:
Function GetProcesses{
$listview_Processes.Items.Clear()
$listview_Processes.Columns.Clear()
$Processes = Get-Process | Select Id,ProcessName,Handles,NPM,PM,WS,VM,CPU,Path
$ProcessProperties = $Processes[0].psObject.Properties
$ProcessProperties | ForEach-Object { $listview_Processes.Columns.Add("$($_.Name)") }
ForEach ($Process in $Processes){
$ProcessListViewItem = New-Object System.Windows.Forms.ListViewItem($Process.Id)
$Process.psObject.Properties | Where {$_.Name -ne "Id"} | ForEach-Object {
$ColumnName = $_.Name ; $ProcessListViewItem.SubItems.Add("$($Process.$ColumnName)")
}
$listview_Processes.Items.Add($ProcessListViewItem)
}
$listview_Processes.AutoResizeColumns("HeaderSize")
}
Code:
Function GetProcesses{
$listview_Processes.Items.Clear()
$listview_Processes.Columns.Clear()
$Processes = Get-Process | Select-Object Id,ProcessName,Handles,NPM,PM,WS,VM,CPU,Path
ForEach ($Hdr in @("Id", "ProcessName", "Handles", "NPM", "PM", "WS", "VM", "CPU", "Path")){$listview_Processes.Columns.Add($Hdr)}
ForEach ($Process in $Processes){
$ProcessListViewItem = New-Object System.Windows.Forms.ListViewItem($Process.Id)
ForEach ($Hdr in @( "ProcessName", "Handles", "NPM", "PM", "WS", "VM", "CPU", "Path")){$ProcessListViewItem.SubItems.Add("$($Process.$Hdr)")}
$listview_Processes.Items.Add($ProcessListViewItem)
}
$listview_Processes.AutoResizeColumns("HeaderSize")
}
Here is a comparison of a Write-Host line added to show for the first psObject returned thing, and then the same got from the PowerShell window
Code:
int Id=5236 string ProcessName=AggregatorHost int Handles=88 long NPM=6112 long PM=1142784 long WS=5099520 long VM=2203375075328 System.Double CPU=0,578125 System.String Path=C:\WINDOWS\System32\AggregatorHost.exe
Code:
PS C:\WINDOWS\system32> $Processes = Get-Process | Select Id,ProcessName,Handles,NPM,PM,WS,VM,CPU,Path
PS C:\WINDOWS\system32> $ProcessProperties = $Processes[0].psObject.Properties
PS C:\WINDOWS\system32> $ProcessProperties
MemberType : NoteProperty
IsSettable : True
IsGettable : True
Value : 5236
TypeNameOfValue : System.Int32
Name : Id
IsInstance : True
MemberType : NoteProperty
IsSettable : True
IsGettable : True
Value : AggregatorHost
TypeNameOfValue : System.String
Name : ProcessName
IsInstance : True
MemberType : NoteProperty
IsSettable : True
IsGettable : True
Value : 88
TypeNameOfValue : System.Int32
Name : Handles
IsInstance : True
MemberType : NoteProperty
IsSettable : True
IsGettable : True
Value : 6112
TypeNameOfValue : System.Int64
Name : NPM
IsInstance : True
MemberType : NoteProperty
IsSettable : True
IsGettable : True
Value : 1142784
TypeNameOfValue : System.Int64
Name : PM
IsInstance : True
MemberType : NoteProperty
IsSettable : True
IsGettable : True
Value : 5099520
TypeNameOfValue : System.Int64
Name : WS
IsInstance : True
MemberType : NoteProperty
IsSettable : True
IsGettable : True
Value : 2203375075328
TypeNameOfValue : System.Int64
Name : VM
IsInstance : True
MemberType : NoteProperty
IsSettable : True
IsGettable : True
Value : 0,578125
TypeNameOfValue : System.Double
Name : CPU
IsInstance : True
MemberType : NoteProperty
IsSettable : True
IsGettable : True
Value : C:\WINDOWS\System32\AggregatorHost.exe
TypeNameOfValue : System.String
Name : Path
IsInstance : True
Bookmarks