HewlettPackard / POSH-HPEOneView

PowerShell language bindings library for HPE OneView.
http://hewlettpackard.github.io/POSH-HPEOneView/
126 stars 52 forks source link

Cannot convert 'System.Management.Automation.PSCustomObject' to 'BsonDocument'. -- Cannot convert 'HPEOneView.Library.ApplianceConnection' to 'BsonValue'. #576

Closed francescasuba closed 3 years ago

francescasuba commented 3 years ago

`Import-Module -Name HPEOneView.600

$user =

$pass = ConvertTo-SecureString -AsPlainText -Force

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

$hosts = , foreach($h in $hosts){ Connect-OVMgmt -Hostname $h -Credential $cred $finalResults = Get-OVAlert -Severity Critical -Count 10 | ConvertFrom-String Disconnect-OVMgmt -Hostname $h }

$finalResults.GetType()

Connect-Mdbc -ConnectionString -DatabaseName -CollectionName "OVTest"

$Database = Get-MdbcDatabase -Name

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

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

It is showing the error:

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

However, when I do the PS Function GetType on $finalResults, the variable I need to input as an object, it shows as an object. But the error is saying it is an ApplianceConnection

ChrisLynchHPE commented 3 years ago

What are you trying to do? Connect to the appliance's internal database? If so thag is not possible, as the only way to get to that data (limited) is using the native Cmdlets the library provides or use the REST API natively.

francescasuba commented 3 years ago

No, I just want to grab the list of alerts from OneView as a PSObject and use another external module to input that PSObject into my Mongo database

francescasuba commented 3 years ago

To add it to my Mongo database, I need a PSObject

francescasuba commented 3 years ago

I am using the Mdbc Powershell module, if that helps

ChrisLynchHPE commented 3 years ago

I'm sorry, add what exactly? What object? Please provide more context to what you are trying to do.

ChrisLynchHPE commented 3 years ago

I see now. Your use of ConvertFrom-String is wrong here:

$finalResults = Get-OVAlert -Severity Critical -Count 10 | ConvertFrom-String

The return here would be objects, not strings. So not sure why you are doing that pipeline operation.

francescasuba commented 3 years ago

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 and put it in a MongoDB.

francescasuba commented 3 years ago

I took ConvertFrom-String out and it's still showing the same error. It's reading those alerts as an HPEOneView.Library.Appliance.Connection

ChrisLynchHPE commented 3 years ago

This is an issue with the target Cmdlet, not this library. You need to manually convert the objects returned from Get-OVAlert to something the target Cmdlet supports.

francescasuba commented 3 years ago

What is the object type of the output of Get-OVAlert function? A PSObject?

francescasuba commented 3 years ago

The target cmdlet supports PSObjects, which I thought Get-OVAlert returned

francescasuba commented 3 years ago

This was solved when I converted the object to a hashtable.

ChrisLynchHPE commented 3 years ago

The returned object is a PSObject. One of the properties is not. It is the ApplianceConnection property, which is a custom strongly typed class.