C# powershell导入模块工作不正常

C# powershell导入模块工作不正常,c#,powershell,powershell-3.0,C#,Powershell,Powershell 3.0,如何在C#中验证模块(导入模块)命令是否成功导入模块?我有自己的自定义powershell,它导入到Windows powershell中。我编写了C#代码,将我的自定义powershell导入到Windows powershell中,当我尝试从代码执行自定义powershell命令时,它不会返回任何结果,而当我从Windows powershell执行相同的命令时(通过导入模块并编写自定义powershell命令)它正在工作 InitialSessionState initial = Init

如何在C#中验证模块(导入模块)命令是否成功导入模块?我有自己的自定义powershell,它导入到Windows powershell中。我编写了C#代码,将我的自定义powershell导入到Windows powershell中,当我尝试从代码执行自定义powershell命令时,它不会返回任何结果,而当我从Windows powershell执行相同的命令时(通过导入模块并编写自定义powershell命令)它正在工作

InitialSessionState initial = InitialSessionState.CreateDefault();
initial.ImportPSModule(new string[] { "ABCD" });
Runspace runspace = RunspaceFactory.CreateRunspace(initial);
initial.ThrowOnRunspaceOpenError = true;
runspace.Open();
RunspaceInvoke runSpaceInvoker = new  RunspaceInvoke(runspace); 
runSpaceInvoker.Invoke("Set-ExecutionPolicy Unrestricted");
PowerShell ps = PowerShell.Create();
ps.Runspace = runspace;      
string script = System.IO.File.ReadAllText(@"D:\Export-Pipeline-Script.txt");
ps.AddScript(script);
ps.Invoke();
ps.Commands.Clear();
ps.AddCommand("Test2");
Collection<PSObject> results1 = ps.Invoke();
foreach (PSObject outputItem in results1)
{
    if (outputItem != null)
    {
        Console.WriteLine(outputItem.ToString());
    }
}  
InitialSessionState initial=InitialSessionState.CreateDefault();
initial.ImportPSModule(新字符串[]{“ABCD”});
Runspace Runspace=RunspaceFactory.CreateRunspace(初始值);
initial.ThrowOnRunspaceOpenError=true;
Open();
RunspaceInvoke runSpaceInvoker=新的RunspaceInvoke(runspace);
调用(“设置ExecutionPolicy Unrestricted”);
PowerShell ps=PowerShell.Create();
ps.运行空间=运行空间;
string script=System.IO.File.ReadAllText(@“D:\Export Pipeline script.txt”);
ps.AddScript(脚本);
ps.Invoke();
ps.Commands.Clear();
ps.AddCommand(“Test2”);
集合结果1=ps.Invoke();
foreach(结果1中的PSObject输出项)
{
if(outputItem!=null)
{
Console.WriteLine(outputItem.ToString());
}
}  

在AddCommand中,Test2是一个函数,我在其中编写了自定义powershell命令。在上述代码中,results1始终写入计数为“0”然而,当我将自定义powershell命令更改为windows powershell命令(如Get Process)时,它可以工作。

以下是一个对我有效的示例。我创建了一个PS模块和一个自定义PS脚本,并在您的C#代码中使用了它们。这可能会给您一些关于如何使您的工作的线索

C:\Temp\PowerShell.Module.psm1

这是自定义powerhell。C:\Temp\PS\GetProc.ps1

使用此代码时:

InitialSessionState initial = InitialSessionState.CreateDefault();
initial.ImportPSModule(new string[] { @"C:\Temp\PowerShell.Module.psm1" });
Runspace runspace = RunspaceFactory.CreateRunspace(initial);
initial.ThrowOnRunspaceOpenError = true;
runspace.Open();
RunspaceInvoke runSpaceInvoker = new  RunspaceInvoke(runspace); 
runSpaceInvoker.Invoke("Set-ExecutionPolicy Unrestricted");
PowerShell ps = PowerShell.Create();
ps.Runspace = runspace;      
string script = System.IO.File.ReadAllText(@"C:\Temp\Export-Pipeline-Script.txt");
ps.AddScript(script);
ps.Invoke();
ps.Commands.Clear();
ps.AddCommand("GetProc");
Collection<PSObject> results1 = ps.Invoke();
foreach (PSObject outputItem in results1)
{
    if (outputItem != null)
    {
        Console.WriteLine(outputItem.ToString());
    }
} 
InitialSessionState initial=InitialSessionState.CreateDefault();
initial.ImportPSModule(新字符串[]{@“C:\Temp\PowerShell.Module.psm1”});
Runspace Runspace=RunspaceFactory.CreateRunspace(初始值);
initial.ThrowOnRunspaceOpenError=true;
Open();
RunspaceInvoke runSpaceInvoker=新的RunspaceInvoke(runspace);
调用(“设置ExecutionPolicy Unrestricted”);
PowerShell ps=PowerShell.Create();
ps.运行空间=运行空间;
string script=System.IO.File.ReadAllText(@“C:\Temp\Export Pipeline script.txt”);
ps.AddScript(脚本);
ps.Invoke();
ps.Commands.Clear();
ps.AddCommand(“GetProc”);
集合结果1=ps.Invoke();
foreach(结果1中的PSObject输出项)
{
if(outputItem!=null)
{
Console.WriteLine(outputItem.ToString());
}
} 
结果:


您使用代码截图只是为了避免偷懒复制吗?文本总是更好。我在OP中使用了我自己的示例截图,但没有提供足够的关于PS代码的信息。因此,我希望我的示例只是一个示例。这也使OP避免偷懒复制。