Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Can you return multiple data items in bag from powershell script?

969 views
Skip to first unread message

Jeff

unread,
Nov 20, 2008, 10:11:25 AM11/20/08
to
In my PS, I am getting multiple instances and values to be mapped as
performance counter, but the property bag output only shows the last
entry with this example:


$oAPI = New-Object -comObject "MOM.ScriptAPI"
$A=something

foreach ($objItem in $A)
{
$Name=X
$Value=Y

$oBag = $oAPI.CreateTypedPropertyBag(2)
$oBag.AddValue('PerfInstance', $Name)
$oBag.AddValue('PerfValue', $Value)
$oAPI.AddItem($oBag)
}
$oAPI.Return($oBag)


----
If I do this, then I see mutiple pairs, but in the same data item and
I dont think that is valid

$oBag = $oAPI.CreateTypedPropertyBag(2)
$oBag.AddValue('PerfInstance', $Name)
$oBag.AddValue('PerfValue', $Value)
$oAPI.AddItem($oBag)
$oAPI.Return($oBag)
}

Does anyone have a working example of how to map perf data back from a
powershell script that returns multiple data items?

Thanks... Jeff

Mike Eisenstein

unread,
Nov 20, 2008, 2:12:10 PM11/20/08
to
Two things everyone should know about returning multiple property bags,
regardless of the scripting language:

After doing AddItem(oBag), be sure to set oBag=Nothing before doing another
CreatePropertyBag (typed or otherwise) call.

When finished, use oAPI.ReturnItems() to return multiple property bags.

--
Mike Eisenstein
http://sentryboy.spaces.live.com/blog/


"Jeff" <jef...@gmail.com> wrote in message
news:a2ef25c4-2ab0-4bd2...@13g2000yql.googlegroups.com...

Jeff

unread,
Nov 21, 2008, 11:42:44 AM11/21/08
to
On Nov 20, 2:12 pm, "Mike Eisenstein" <sentryboyAThotmailDOTcom>
wrote:

> Two things everyone should know about returning multiple property bags,
> regardless of the scripting language:
>
> After doing AddItem(oBag), be sure to set oBag=Nothing before doing another
> CreatePropertyBag (typed or otherwise) call.
>
> When finished, use oAPI.ReturnItems() to return multiple property bags.
>
> --
> Mike Eisensteinhttp://sentryboy.spaces.live.com/blog/
>
> "Jeff" <jeff...@gmail.com> wrote in message

>
> news:a2ef25c4-2ab0-4bd2...@13g2000yql.googlegroups.com...
>
>
>
> > In my PS, I am getting multiple instances and values to be mapped as
> > performance counter, but the property bag output only shows the last
> > entry with this example:
>
> > $oAPI = New-Object -comObject "MOM.ScriptAPI"
> > $A=something
>
> > foreach ($objItem in $A)
> > {
> > $Name=X
> > $Value=Y
>
> > $oBag = $oAPI.CreateTypedPropertyBag(2)
> > $oBag.AddValue('PerfInstance', $Name)
> > $oBag.AddValue('PerfValue', $Value)
> > $oAPI.AddItem($oBag)
> > }
> > $oAPI.Return($oBag)
>
> > ----
> > If I do this, then I see mutiple pairs, but in the same data item and
> > I dont think that is valid
>
> > $oBag = $oAPI.CreateTypedPropertyBag(2)
> > $oBag.AddValue('PerfInstance', $Name)
> > $oBag.AddValue('PerfValue', $Value)
> > $oAPI.AddItem($oBag)
> > $oAPI.Return($oBag)
> > }
>
> > Does anyone have a working example of how to map perf data back from a
> > powershell script that returns multiple data items?
>
> > Thanks... Jeff- Hide quoted text -
>
> - Show quoted text -

This worked, thanks... forgot about the ReturnItems()


$oBag = $oAPI.CreateTypedPropertyBag(2)
$oBag.AddValue('PerfInstance', $dbname)
$oBag.AddValue('PerfValue', $dbsizeMB)
$oAPI.AddItem($oBag)
clear-variable -name oBag
}
$oAPI.ReturnItems()

willy mario

unread,
Nov 14, 2011, 5:09:47 AM11/14/11
to
Hum

In powershell you cannot use a code simply translated from the VB source.
In the loop create multiple $bag instances, then simply "print" $bag for each instance...
CreateTypedPropertyBag(2) is used for performance typed bag.

It work usingthe following code:

...
@($Databases) | % {
$db = $_

if( $db.DatabaseSize -match ".*\((.*) bytes\)" )
{
$size= $matches[1].Replace(",","")
$bag = $mom.CreateTypedPropertyBag(2)
$bag.addValue("CounterName" ,"DatabaseSize")
$bag.addValue("InstanceName", $db.Name)
$bag.addValue("CounterValue", $size)
$bag
}

if( $db.AvailableNewMailboxSpace -match ".*\((.*) bytes\)" )
{

$size= $matches[1].Replace(",","")
$bag = $mom.CreateTypedPropertyBag(2)
$bag.addValue("CounterName" ,"AvailableNewMailboxSpace")
$bag.addValue("InstanceName", $db.Name)
$bag.addValue("CounterValue", $size)
$bag
}
}
$bag = $mom.CreateTypedPropertyBag(2)
$bag.addValue("CounterName" ,"MailboxCount")
$bag.addValue("InstanceName", "DAG_$DAGName")
$bag.addValue("CounterValue", $DAGmbsCount )
$bag
</ScriptBody>
......
..

> On Thursday, November 20, 2008 2:12 PM Mike Eisenstein wrote:

> Two things everyone should know about returning multiple property bags,
> regardless of the scripting language:
>
> After doing AddItem(oBag), be sure to set oBag=Nothing before doing another
> CreatePropertyBag (typed or otherwise) call.
>
> When finished, use oAPI.ReturnItems() to return multiple property bags.
>
> --
> Mike Eisenstein
> http://sentryboy.spaces.live.com/blog/
>
>
> "Jeff" <jef...@gmail.com> wrote in message
> news:a2ef25c4-2ab0-4bd2...@13g2000yql.googlegroups.com...
>>> On Saturday, November 22, 2008 10:31 PM Jeff wrote:

>>> On Nov 20, 2:12=A0pm, "Mike Eisenstein" <sentryboyAThotmailDOTcom>
>>> wrote:
>>> ther
>>>
>>> This worked, thanks... forgot about the ReturnItems()
>>>
>>>
>>> $oBag =3D $oAPI.CreateTypedPropertyBag(2)
0 new messages