早上好,我有一个问题想O负载PowerShell的模块调用MSOnline在ASP.NET 下面这个链接之后加载PowerShell的模块“MSOnline”在ASP.NET Web应用程序: 错误,而试图
这是一个Button,它通过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)。但我不知道为什么没有列出它是否安装它。感谢您的帮助。我发现MSOnline .dll文件位于:C:\ windows \ system32 \ WindowsPowerShell \ v1.0 \ Modules \ MSOnline 但是,如果我尝试从Visual Studio手动添加它说“MSOnline不存在” 。 这是很奇怪的,因为都有相同的权限到文件夹。这是怎么回事! :S本是如此令人沮丧...