nightroman / Mdbc

MongoDB Cmdlets for PowerShell
Apache License 2.0
141 stars 16 forks source link

Add-Mdbc Data is not taking a PSObject #63

Closed francescasuba closed 3 years ago

francescasuba commented 3 years ago

`Import-Module -Name HPEOneView.600

$user = 'SampleUsername'

$pass = ConvertTo-SecureString 'SamplePassword' -AsPlainText -Force

$cred = New-Object System.Management.Automation.PSCredential ($user, $pass)

$hosts = @('sample.host.com', 'sample2.host.come') foreach($h in $hosts){ Connect-OVMgmt -Hostname $h -Credential $cred $finalResults = Get-OVAlert -Severity Critical -Count 200 #| New-MdbcData -Id {$_.Id} Disconnect-OVMgmt -Hostname $h }

Connect-Mdbc -ConnectionString SampleConnectionString -DatabaseName SampleDBName -CollectionName "SampleTable"

$Database = Get-MdbcDatabase -Name SampleDBName

$Collection = Get-MdbcCollection -Name "SampleTable" -Database $Database

Add-MdbcData -InputObject $finalResults -Collection $Collection`

I have two OneView hosts, and I want to use the Get-OVAlert command for both of those hosts to pull the critical alerts. I then have to take those alerts ($finalResults) and put it in a MongoDB. For some reason, if I go a GetType() on the variable $finalResults, it reads as an array PSObject. However, the function Add-MdbcData does not read it as a PSObject, it reads it as an HPEOneView.Library.ApplianceConnection. The error reads:

"Add-MdbcData : Cannot convert 'System.Management.Automation.PSCustomObject' to 'BsonDocument'. -- Cannot convert 'HPEOneView.Library.ApplianceConnection' to 'BsonValue'."

nightroman commented 3 years ago

What is the error? (You copy/pasted a different text instead of the error)

The similar scenario works for me:

Import-Module Mdbc
Connect-Mdbc -NewCollection

$res = Get-Process *chrome* | New-MdbcData -Id {$_.Id} -Property Name, WorkingSet
Add-MdbcData -InputObject $res

Get-MdbcData
francescasuba commented 3 years ago

Apologies, my comment has been edited

nightroman commented 3 years ago

Well, the error tells it all. Mdbc does not know how to make BsonValue from 'HPEOneView.Library.ApplianceConnection' (one of the PSCustomObject "properties"). You have to convert manually. Unfortunately (inevitably) some types are too complex for auto conversion.

nightroman commented 3 years ago

You can use the -Convert script, something like:

... | New-MdbcData -Id {$_.Id} -Convert {
    if ($_ is [HPEOneView.Library.ApplianceConnection]) {
        # convert $_ to some PSCustomObject, Hashtable, etc. and return it instead of $_
    }
}
francescasuba commented 3 years ago

https://github.com/francescasuba/sample/blob/3b2aeeab80cfd00c55012eaa83e622cc2edcf12d/oneview

I am getting the error: New-MdbcData : Object reference not set to an instance of an object.

francescasuba commented 3 years ago

`foreach($alert in $alerts){ New-MdbcData -InputObject $alert -NewId -Convert { if ($alert.type -eq [HPEOneView.Library.ApplianceConnection]) {

convert $ to some PSCustomObject, Hashtable, etc. and return it instead of $

            $finalResults = [pscustomobject]$alert
        }
    }

`

This is working better, but it has split the alerts every three lines instead of just one alert

nightroman commented 3 years ago

I am getting the error: New-MdbcData : Object reference not set to an instance of an object.

because you do not input anything to New-MdbcData. OK, do not use it all. I recommend to convert your HPEOneView.Library.ApplianceConnection objects manually to Hashtable and send these tables to Add-MdbcData. This should work. I am closing, it's now PowerShell programming issue, not Mdbc.

francescasuba commented 3 years ago

Add-MdbcData : Cannot convert 'HPEOneView.Library.ApplianceConnection' to 'BsonValue'. At E:\node2\node\Default\http\update-Oneview.ps1:140 char:42 $alert | Convert-ObjectToHashTable | Add-MdbcData


     CategoryInfo          : InvalidData: (System.Collections.Hashtable:PSObject) [Add-MdbcData], ArgumentException
     FullyQualifiedErrorId : BsonValue,Mdbc.Commands.AddDataCommand

@nightroman I understand. Just so you know, I converted the objects to hashtables and still erroring
nightroman commented 3 years ago

OK. How do you convert? What is Convert-ObjectToHashTable? What properties of your input objects do you want to store?

francescasuba commented 3 years ago

Convert-ObjectToHashTable Function: https://kpatnayakuni.com/2019/11/26/powershell-convert-object-properties-into-hashtable/

Propoerties: Severity, Resource, Created, Modified, State, Description

nightroman commented 3 years ago

This cmdlet makes shallow conversion, and you need deep. If properties are complex then Convert-ObjectToHashTable should be applied to them as well.

What are the types of these properties: Severity, Resource, Created, Modified, State, Description? Are there other properties (i.e. where is Id, maybe other properties are troublemakers)?

nightroman commented 3 years ago
# Assuming Severity, Resource, Created, Modified, State, Description are simple types, try this

$alert | Select-Object Severity, Resource, Created, Modified, State, Description | Add-MdbcData

# But if they are not simple then convert them...
francescasuba commented 3 years ago

foreach($alert in $alerts){ Add-MdbcData -InputObject $alert -Convert {$_ | Convert-ObjectToHashtable} -NewId -Collection $Collection }

This actually worked! :) Piped the Convert-ObjectToHashtable function as the -Convert script and it worked like a charm. Thanks for sticking it out!

nightroman commented 3 years ago

I am really glad it worked for you. And the way you do this is exactly how it is all designed. -Convert with $_ is called only for pieces that cannot be converted. In this scenario even shallow Convert-ObjectToHashtable helps because the whole convert process is recursive (deep). Whew! :)

Like I said, It's all rather about PowerShell and serialization to BSON, not Mdbc as such. Have a nice weekend!