Jump to content
Software FX Community
Sign in to follow this  
IGSFX

Scriptblock support in chart parameters

Recommended Posts

In a previous post we exposed how the out-chart and out-map cmdlets support scriptblocks in parameters that specify fields, such as -values. We also showed how it is also supported in almost any out-gauge parameter. However, support for scriptblocks in other chart/map parameters was not available at the time the blog entry was written.

We have recently added support for scriptblocks in almost any chart/map parameter, but some additional configuration is required to use it. Let me start explaining how PowerGadgets plots the data received from a PowerShell pipe. Unlike gauges, which normally display one value at the time, charts and maps are used to display a number of values. Based on that fact, PowerGadgets inspects the piped data and plots all numeric properties found, unless you select one or more fields (properties) through the -values and -label parameters. For example, if you want to create a chart of the CPU used by the running processes, you can execute the following command:

get-process | out-chart -values CPU -label Name

Resulting in the following chart:

Posted Image

On the other hand, if you want to use another property of the piped data in a chart parameter, for example the chart title, you would need to pass one value instead of a collection of values, because otherwise the chart wouldn't know which of the members of the collection should be used. To illustrate this case, let's say for a moment that you want to create the same chart shown before but refreshed every five seconds. Also, the chart title will be used to show the last time the data was acquired. For such purpose, let's create the ProcessesWithTime.ps1 script as follows:

$datetime = get-date
$processes = get-process

$obj = new-object System.object
add-member -inputobject $obj -membertype NoteProperty -Name DateTime -value $datetime
add-member -inputobject $obj -membertype NoteProperty -Name Processes -value $processes
$obj

Essentially this script creates a new object with a property containing the current date & time and another property with a collection of Process objects. If you pipe the script to an out-chart cmdlet you will get a "No data available" message, since PowerGadgets could not find any numeric property in the piped data. Therefore, you need to specify which of the object's properties contains the data to be plotted, through the -DataObject property. You can now invoke the following:

ProcessesWithTime | out-chart -dataobject Processes -values CPU -label Name

Now, let's add the current datetime to the chart's title and refresh it every 5 seconds:

ProcessesWithTime | out-chart -dataobject Processes -values CPU -label Name -titles_0_text {$_.DateTime} -refresh 0:0:5

Now, every 5 seconds your chart will be refreshed with current running processes data and the time shown in the title as follows:

Posted Image

There are many uses for scriptblocks in chart parameters such as the value of a CustomGridLine or setting colors based on conditions not related to the data being plotted (such as in the RandomData example of our previous scriptblock post), among others.

Note: this feature was added in the PowerGadgets build 1.0.2595, which was not publicly available at the time of posting this blog entry. If you want to get an interim build containing this feature, please contact our tech support (see website for details).

IvanG

Share this post


Link to post
Share on other sites

Hi Ivan,

If I already created an new object in my script.

====

$record = new-object -typename system.object

$record | add-member -membertype noteproperty -name count -value 0

[void]$foreach.movenext()

$record | add-member -membertype noteproperty -name name -value $level

$listing+=$record

=====

And also create a new object for the current date. Like:

=====

$datetime = get-date

$obj = new-object System.object

add-member -inputobject $obj -membertype NoteProperty -Name DateTime -value $datetime

=====

How do I get the right result in the Chart PowerGadget? Do I need to use the -dataobject property?

| out-chart -datasourcesettings_style +Transpose -series_0_color Red

-series_1_color Blue -series_2_color Yellow -titles_0_Text "OpsMgr 2007 Alerts" -titles_1_Text {$_.DateTime} -refresh 0:0:5

Regards,

Stefan Stranger

Share this post


Link to post
Share on other sites

Is this feature still working?

It doesn't seem to work in the latest build. I just copied the code from this article and I get a runtime exception and the whole PS crashes.

Cosmin.

Share this post


Link to post
Share on other sites
Sign in to follow this  

×
×
  • Create New...