使用C#DLL中定义的PowerShell中的函数时出现错误

时间:2012-07-25 13:16:34

标签: c# dll powershell casting

我正在开发一个项目,将定制的c#.DLL集成到PowerShell中。但是,我遇到了问题 将C#Objects转换为PowerShell可以理解的不可以的形式。我搜索过谷歌 像一百万次,并尝试了一些不同的东西,但没有一个是成功的。

当我使用对象数组调用SNC-getVlan时,会发生以下错误:

“使用”1“参数调用”printObject“的异常:”无法将类型为'System.Management.Automation.PSObject'的对象强制转换为'Objects.blablazzz.DC_Object'“

我发布了一些代码的小子集,希望你们能看到我做错了什么。

我正在使用的课程:

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
}

打印对象的函数:

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.

fromJSON函数是实际返回发送到printObject的对象的函数,不确定 如果重要但我会发布它。

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模块/脚本:

#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命令(dll已加载):

PS C:\$variable = SNC-Get-VLAN
PS C:\printObject($variable)

我必须注意,我的printDebug函数在SNC-getHost上使用时工作正常,但不起作用 SNC-getVlan,SNC-Get-Vlan返回一个值数组,而SNC-getHost只返回一个值(它仍然是 虽然在数组中但它看起来不像PowerShell将其保存在数组中。

1 个答案:

答案 0 :(得分:0)

对于可能遇到同样问题的人。解决方案是更改函数的返回值和DC_Object的参数,因为这是顶部封装对象。这解决了所有铸造问题。