I’m working on a project to integrate a custom made c# .DLL into powershell. However, I’m having problems
with casting the C# Objects into a form that PowerShell can understand which it cannot. I’ve searched google
like a million times and tried a few different things but none of them were succesfull.
The following error occurs when I’m calling SNC-getVlan with an array of objects:
“Exception calling “printObject” with “1” argument(s): “Unable to cast object of type ‘System.Management.Automation.PSObject’ to type ‘Objects.blablazzz.DC_Object'”
I’m posting some small subsets of my code hoping you guys can see what I’m doing wrong.
Classes I’m using:
public class DC_Object
{
public string name = "undefined";
}
public class Cmdb_Host : DC_Object
{
//Lots of properties
}
public class Cmdb_Vlan : DC_Object
{
//Lots of properties
}
Function that prints the objects:
public static void printObject(Object[] objects)
{
foreach (Object o in objects)
{
string name = ((DC_Object)o).name; //I'm assuimg things go wrong in here.
The fromJSON function is the function that actually returns the object that is sent into printObject, not sure
if it matters but I’ll post it anyways.
static public Object[] fromJSON(string input)
{
//Check the string to see to what object it should convert to
switch (input.Substring(0, 16))
{
//Reuest for a host (Host Table)
case "{\"u_cmdb_ci_host":
if (input[18] == '[') // We have an array
{
Container_Host c_host = JsonConvert.DeserializeObject<Container_Host>(input);
return c_host.u_cmdb_ci_host;
}
else // We have a single object
{
Container_Host_Single c_host = JsonConvert.DeserializeObject<Container_Host_Single>(input);
Container_Host h = new Container_Host();
h.u_cmdb_ci_host = new Cmdb_Host[1];
h.u_cmdb_ci_host[0] = c_host.u_cmdb_ci_host;
return h.u_cmdb_ci_host;
}
//Request for a VLAN (Network Table)
case "{\"cmdb_ci_ip_net":
Container_Vlan c_vlan = JsonConvert.DeserializeObject<Container_Vlan>(input);
return c_vlan.cmdb_ci_ip_network;
}
return null;
}
Powershell Module/Script:
#Loads in the custom DLL created for this specific project.
[Reflection.Assembly]::LoadFrom(“C:\Users\Joey\Documents\PSScripts\DataCollector\DataCollect.dll”)
# Creates a new Client object that handles all communication between the PowerShell module and the
# sncdb-worker at server side.
$client = new-object blablazzz.Sender;
[blablazzz.Config]::Configure("C:\Users\Joey\Documents\PSScripts\DataCollector\asp4all.ini")
$client.Connect();
# This functions returns a Host Machine (Virtual or Physical) in object notation to for easy post-processing
# in PowerShell.
Function SNC-GetHost($hostz = "blabla")
{
return $client.sendMessage([blablazzz.Parser]::getHost($hostz));
}
# This function returns VLAN information in object notation. This is a collection most of the time.
function SNC-GetVLAN($vlan = "TLS-TL2-CPH-TEST")
{
return $client.sendMessage([blablazzz.Parser]::getVlan($vlan));
}
Function printObject($hostz)
{
[blablazzz.Formatter]::printObject($hostz)
}
PowerShell commands (dll is already loaded):
PS C:\$variable = SNC-Get-VLAN
PS C:\printObject($variable)
I’ll have to note that my printDebug function works fine when used on SNC-getHost but doesn’t work on
SNC-getVlan, SNC-Get-Vlan returns an array of values while SNC-getHost only returns one value (it’s still
in an array though but it doesn’t look like PowerShell kept it in an array).
For the people who might come across the same problem. The solution was to change the return value of my functions and the arguments to DC_Object since this is the top encapsulating object. This solved all casting problems.