Import-Moud在c#powershell命令中不起作用 [英] Import-Moud dose not work in c# powershell command
本文介绍了Import-Moud在c#powershell命令中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
InitialSessionState iss = InitialSessionState.CreateDefault();
iss.ImportPSModule(new string[] { "activedirectory" });
Runspace myRunSpace = RunspaceFactory.CreateRunspace(iss);
myRunSpace.Open();
Pipeline pipeLine = myRunSpace.CreatePipeline();
Command myCommand = new Command("Get-ADUser");
myCommand.Parameters.Add("Filter", "sAMAccountName -eq 'user1'");
//myCommand.Parameters.Add("IncludeDeletedObjects");
pipeLine.Commands.Add(myCommand);
//Command restoreCommand = new Command("Restore-ADObject");
//pipeLine.Commands.Add(restoreCommand);
Console.WriteLine("Before Invoke");
Collection<PSObject> commandResults = pipeLine.Invoke();
Console.WriteLine("After Invoke");
foreach (PSObject cmdlet in commandResults)
{
//Console.WriteLine("Inside foreach");
string cmdletName = cmdlet.BaseObject.ToString();
System.Diagnostics.Debug.Print(cmdletName);
Console.WriteLine(cmdletName);
}
Console.ReadLine();
异常:"Get-ADUser"条目无法识别为cmdlet,函数,脚本文件或可以运行程序的名称。请检查名称拼写,如果包含路径,请确保路径正确并再试一次
Exception: The "Get-ADUser" entry can not be recognized as a cmdlet, a function, a script file, or a name that can run a program. Please check the name of the spelling, if the path is included, make sure the path is correct and try again
2,带AddScript的ps.AddCommand
2、ps.AddCommand with AddScript
这是我的其他代码:
Here is my other code:
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
using (PowerShell ps = PowerShell.Create())
{
//ps.AddCommand("Import-Module").AddParameter("Name", "RemoteDesktop");
ps.AddScript("Import-Module").AddParameter("Name", "RemoteDesktop");
ps.Invoke();
if (ps.Streams.Error.Count > 0)
Console.WriteLine(ps.Streams.Error[0].Exception);
ps.Commands.Clear();
ps.Runspace = runspace;
ps.AddCommand("Add-RDVirtualDesktopToCollection");
var results = ps.Invoke();
if (ps.Streams.Error.Count > 0)
Console.WriteLine(ps.Streams.Error[0].Exception);
// Do something with result ...
}
runspace.Close();
ps.AddCommand(" Import-Module")。AddParameter(" Name"," RemoteDesktop");
ps.AddCommand("Import-Module").AddParameter("Name", "RemoteDesktop");
推荐答案
你好,
这是一个英语论坛,请将外语改为英语。
This is an English language forum, please alter the foreign language to English.
这篇关于Import-Moud在c#powershell命令中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文