Powershell script

Hello, i have created powershell monitoring script but looks like it does work properly on target machine.

$ScomAPI = New-Object -comObject “MOM.ScriptAPI”
$PropertyBag = $ScomAPI.CreatePropertyBag()

function RegisterSnapIn($snapIn, $visibleName)
{
if(Get-Module $snapIn)
{
return
}

$nstPath = “HKLM:\SOFTWARE\Microsoft\Microsoft Dynamics NAV\130\Service”

$snapInAssembly = Join-Path (Get-ItemProperty -path $nstPath).Path “$snapIn.psd1”
if(!(Test-Path $snapInAssembly)) { $snapInAssembly = Join-Path (Get-ItemProperty -path $nstPath).Path “$snapIn.dll” }
Import-Module $snapInAssembly -ErrorVariable errorVariable -ErrorAction SilentlyContinue
if (Check-ErrorVariable -eq $true)
{
if ((Get-PSSnapin -Name $snapIn -ErrorAction SilentlyContinue) -eq $null)
{
if ((Get-PSSnapin -Registered $snapIn -ErrorAction SilentlyContinue) -eq $null)
{
write-host -fore Red “Couldn’t register $visibleName”
write-host -fore Red “Some cmdlets may not be available`n”
}
else
{
Add-PSSnapin $snapIn
}
}
}
}
function Check-ErrorVariable
{
return ($errorVariable -ne $null -and $errorVariable.Count -gt 0)
}

RegisterSnapIn “Microsoft.Dynamics.Nav.Management” “Microsoft Dynamics NAV Management Snap-in”
RegisterSnapIn “Microsoft.Dynamics.Nav.Apps.Management” “Microsoft Dynamics NAV App Management Snap-in”

$State=Get-NAVTenant -ServerInstance Dev1|select State

$PropertyBag.AddValue(“state”,[string]$State)

Send output to SCOM

$PropertyBag

I have tried to use $PropertyBag.AddValue(‘state’,$State)

but with this I got error The Microsoft Operations Manager Expression Filter Module failed to #query the delivered item, item was dropped.

Property Expression: Property[@Name=‘state’]

The script itself, started from powershell console, works fine on target machine.

In Windows Powershell log i can see that powershell starting every 2 mins, right on schedule.

Right now the state is Mounted and it should be Critical, but it is Marked as Healthy.

Have you tried to see what the propertybag contains?

try running the script, and ending it with the following:

$ScomAPI.Return($PropertyBag)

Sometimes the return value can be in a wrong format, and you just need to convert it.

Hello,

thank you for reply

With $PropertyBag.AddValue(“state”,$State) it returned

Type mismatch (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))

With $PropertyBag.AddValue(“state”,[string]$State) it returned

<DataItem type=\“System.PropertyBagData\” time=\“2020-03-09T20:43:50.3938101+03:00\” sourceHealthServiceId=\"05249305-8CC2-3E

47-75AF-C729AD9C98D0\"><Property Name=\“state\” VariantType=\“8\”>@{State=Mounted}</Property></DataItem>

 

So am i correct that i need parse {State=Mounted}?

Hi Alexey,

what does $state output ?

and what does $state.gettype() output ?

Hello, Skovsen

I have fixed the issue today. i have changed

$State=Get-NAVTenant -ServerInstance Dev1|select State

$PropertyBag.AddValue(“state”,[string]$State)

To

$State=Get-NAVTenant -ServerInstance App|select State
$ST=$State.State

$PropertyBag.AddValue(“State”,[string]$ST)

and the it started work correctly

$PropertyBag.AddValue(“state”,$State) returns

<DataItem type=“System.PropertyBagData” time=“2020-03-10T14:33:30.9390000+03:00” sourceHealthServiceId=“03B6E26F-DEDD-BE
B3-28B6-E85A9D5F405E”><Property Name=“State” VariantType=“8”>Operational</Property></DataItem>

So the issue was, as you have siad before, type and value mismatch.

Now, the monitor returns correct state

3.png

Thank you very much!

Hi Alexey,

Awesome you got it working :slight_smile:

Glad to hear it!