早上好,我在尝试在 ASP.NET 中加载名为 MSOnline 的 Powershell 模块时遇到了一个问题。
这是一个通过 C# 中的 Code Behind 运行脚本的按钮。当我尝试加载模块时,它引发了异常。
protected void Bexecute_Click(object sender, EventArgs e)
{
// Import-Module MSOnline
try
{
//Loading MSOnline using a InitialSessionState
InitialSessionState iss = InitialSessionState.CreateDefault();
iss.ImportPSModule(new string[] { "MSOnline" });
//Before the solution iss has an exception of "not found" the MSOnline Module
//Create a runspace
Runspace test = RunspaceFactory.CreateRunspace(iss);
//Open a Runspace
test.Open();
//Create a Pipeline
Pipeline pipeLine = test.CreatePipeline();
//Create the StringBuilder to get append the script
StringBuilder script = new StringBuilder();
//Take the script from Tscript (TextBoxt with ID="Tscript" in default.aspx
script.AppendLine(@"Get-Module -ListAvailable");
//Add the script and commands to the pipeline;
pipeLine.Commands.AddScript(script.ToString());
//Execute script and get the PSObject Results collection
Collection<PSObject> resultObjects = pipeLine.Invoke();
//Close the Runspace
test.Close();
if (resultObjects.Count > 0)
{
/*** DO SOMETHING WITH THE PSObjects ***/
}
}
catch (System.Management.Automation.RuntimeException ex)
{
status.AppendLine(ex.Message);
}
}
我需要的是可能出错的地方。我引用了 system.management.automation (dll)。但我不知道如果安装了它为什么没有列出。感谢您的帮助。
I have found MSOnline .dll files in : C:\windows\system32\WindowsPowerShell\v1.0\Modules\MSOnline But If I tried to add manually from visual studio it says "that MSOnline doesn't exist". This is quite weird, because all have same permissions to folder. What's going on! :S This is so frustrating...