As you probably know, PowerShell is built on .NET, to be more precise Windows PowerShell is built on the .NET Framework, where PowerShell Core is built on .NET Core.
When you work with PowerShell in many cases you won’t be very concerned about this fact, but in some cases you can’t ignore it.
The other day while working on a PowerCLI script to get and set the logforwarding for a vCenter Server Appliance (vCSA), see also this older post.
The “get” part worked well. To retrieve the hostname, the port and protocol of the forwarding log servers run the following line of code:
(Get-CisService -name 'com.vmware.appliance.logging.forwarding').get()
For the set part, I created:
$spec = New-Object PSObject -Property @{ hostname="logger1.net" port=514 protocol="UDP" } (Get-CisService -name 'com.vmware.appliance.logging.forwarding').set($spec)
However this failed, creating the following error message:
Parameter 'cfg_list' expects values of type 'System.Collections.Generic.List`1[[System.Management.Automation.PSObject, System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]' but received value of type 'System.Management.Automation.PSObject'. At line:1 char:1 + (Get-CisService -name 'com.vmware.appliance.logging.forwarding').set( ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : OperationStopped: (:) [], CisException + FullyQualifiedErrorId : VMware.VimAutomation.Cis.Core.Types.V1.CisException
From the documentation, it was already known to me that a vCSA supports a total of 3 log forwarding hosts – hence the ‘cfg_list’, but how to interpret this error message? The parameter ‘cfg_list’ must be of a certain type, but how to solve this. Luckily my colleague Bouke (you can see what is on his mind on https://www.jume.nl ), quickly showed me the solution by specifying the variable in the correct type.
The following piece of code does the ‘set’ job. The solution is in the first line; setting the correct type (variable $speclist) for the ‘cfg_list’ parameter.
$speclist = [System.Collections.Generic.List[PSobject]]::new() $spec = New-Object PSObject -Property @{ hostname="logger1.net" port=514 protocol="UDP" } $speclist.add($spec) $spec = New-Object PSObject -Property @{ hostname="logger2.net" port=514 protocol="UDP" } $speclist.add($spec) (Get-CisService -name 'com.vmware.appliance.logging.forwarding').set($speclist)
As always, I thank you for reading.