C# 为什么可以';t我在C中使用自定义Powershell模块#

C# 为什么可以';t我在C中使用自定义Powershell模块#,c#,powershell,C#,Powershell,我正在Visual Studio 2010中编写一个小GUI,用于我为windows powershell制作的一个自定义模块,但不幸的是,我只能使用默认模块(例如ActiveDirectory) 下面是应该运行的代码,但它没有任何作用!我有一些代码成功运行,但我不能张贴,因为它有敏感信息 请帮助我,这让我想退出,成为一名麦金塔管理员 private void getStateOfSelectedLab(object sender, EventArgs e) {

我正在Visual Studio 2010中编写一个小GUI,用于我为windows powershell制作的一个自定义模块,但不幸的是,我只能使用默认模块(例如ActiveDirectory)

下面是应该运行的代码,但它没有任何作用!我有一些代码成功运行,但我不能张贴,因为它有敏感信息

请帮助我,这让我想退出,成为一名麦金塔管理员

    private void getStateOfSelectedLab(object sender, EventArgs e)
    {
        stateBox.Items.Clear();

        stateBox.Items.Add("Please Wait (This may take a few minutes)...");

        using (PowerShell PowerShellInstance = PowerShell.Create())
        {
            PowerShellInstance.AddScript("import-module ActiveDirectory");  //import modules
            PowerShellInstance.Invoke();    //executes command pipeline

            PowerShellInstance.AddScript("import-module LANModule");
            PowerShellInstance.Invoke();

            PowerShellInstance.AddScript("Get-LabState " + selectLab.SelectedItem.ToString());
            //PowerShellInstance.AddParameter("lab", selectLab.SelectedItem.ToString());

            Collection<PSObject> theState = PowerShellInstance.Invoke();

            string[] stateOfLab = new string[theState.Count];

            int i = 0;

            foreach (PSObject result in theState)
            {
                string aResult = result.ToString();

                stateOfLab[i] = aResult;
                i++;
            }

            stateBox.Items.Clear();

            //stateBox.Items.Add(theState.Count);

            stateBox.Items.AddRange(stateOfLab);
        }
    }
private void getStateOfSelectedLab(对象发送方,事件参数e)
{
stateBox.Items.Clear();
stateBox.Items.Add(“请稍候(这可能需要几分钟)…”;
使用(PowerShell PowerShellInstance=PowerShell.Create())
{
PowerShellInstance.AddScript(“导入模块ActiveDirectory”);//导入模块
PowerShellInstance.Invoke();//执行命令管道
AddScript(“导入模块”);
PowerShellInstance.Invoke();
PowerShellInstance.AddScript(“Get LabState”+selectLab.SelectedItem.ToString());
//PowerShellInstance.AddParameter(“lab”,selectLab.SelectedItem.ToString());
集合状态=PowerShellInstance.Invoke();
string[]stateOfLab=新字符串[theState.Count];
int i=0;
foreach(状态中的PSObject结果)
{
字符串aResult=result.ToString();
stateOfLab[i]=结果;
i++;
}
stateBox.Items.Clear();
//stateBox.Items.Add(state.Count);
stateBox.Items.AddRange(stateOfLab);
}
}
下面是有效的脚本:

        using (PowerShell PowerShellInstance = PowerShell.Create())
        {
            PowerShellInstance.AddScript("import-module ActiveDirectory");  //import modules
            PowerShellInstance.Invoke();    //executes command pipeline

            PowerShellInstance.AddScript("import-module LANModule");
            PowerShellInstance.Invoke();

            //add another to the pipe, this one grabs all labs
            PowerShellInstance.AddScript("$OU = [REDACTED]");
            PowerShellInstance.AddScript("Get-ADOrganizationalUnit -Filter * -SearchBase $OU | sort-object");

            //returned collection of labs
            Collection<PSObject> allLabs = PowerShellInstance.Invoke();

            foreach (PSObject lab in allLabs)
            {
                string[] toks = lab.ToString().Split(',');
                string labName = toks[0].Substring(3);
                selectLab.Items.Add(labName);
            }
        }
    }
使用(PowerShell PowerShellInstance=PowerShell.Create())
{
PowerShellInstance.AddScript(“导入模块ActiveDirectory”);//导入模块
PowerShellInstance.Invoke();//执行命令管道
AddScript(“导入模块”);
PowerShellInstance.Invoke();
//在管道上再加一个,这一个会抓住所有的实验室
PowerShellInstance.AddScript(“$OU=[redact]”);
AddScript(“Get-ADOrganizationalUnit-Filter*-SearchBase$OU | sort object”);
//返回的实验室集合
集合allLabs=PowerShellInstance.Invoke();
foreach(所有实验室中的PSObject实验室)
{
字符串[]toks=lab.ToString().Split(',');
字符串labName=toks[0]。子字符串(3);
选择Lab.Items.Add(labName);
}
}
}

lab名称没有空格,是吗?在这一行之后
Collection theState=PowerShellInstance.Invoke()在调试器中执行检查
PowerShellInstance.Streams.Errors
。如果有一个非终止错误,这就是您可以看到它的方式。