Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法识别导入模块-带c的Powershell#_C#_Powershell_Exchange Server_Windows Server 2012 - Fatal编程技术网

C# 无法识别导入模块-带c的Powershell#

C# 无法识别导入模块-带c的Powershell#,c#,powershell,exchange-server,windows-server-2012,C#,Powershell,Exchange Server,Windows Server 2012,我面临着一个非常奇怪的情况 我想使用命令“Import Module”来导入“ActiveDirectory”模块。我做的第一件事是在我的服务器(windows server 2012 R2)上安装该模块。完成第一步后,我尝试使用参数“ActiveDirectory”调用命令“Import Module” 以下是我正在使用的代码: // Prepare the credentials that will be used when connecting // to the server. More

我面临着一个非常奇怪的情况

我想使用命令“Import Module”来导入“ActiveDirectory”模块。我做的第一件事是在我的服务器(windows server 2012 R2)上安装该模块。完成第一步后,我尝试使用参数“ActiveDirectory”调用命令“Import Module”

以下是我正在使用的代码:

// Prepare the credentials that will be used when connecting
// to the server. More info on the user to use on the notes
// below this code snippet.
string runasUsername = @"MarioKart 8";
string runasPassword = "MarioKart";
SecureString ssRunasPassword = new SecureString();
foreach (char x in runasPassword)
    ssRunasPassword.AppendChar(x);
PSCredential credentials =
new PSCredential(runasUsername, ssRunasPassword);

// Prepare the connection
var connInfo = new WSManConnectionInfo(
   new Uri("MarioKart8Server"),
   "http://schemas.microsoft.com/powershell/Microsoft.Exchange",
   credentials);
connInfo.AuthenticationMechanism =
    AuthenticationMechanism.Basic;
connInfo.SkipCACheck = true;

connInfo.SkipCNCheck = true;

// Create the runspace where the command will be executed
var runspace = RunspaceFactory.CreateRunspace(connInfo);

// create the PowerShell command
var command = new Command("Import-Module");
command.Parameters.Add("Name","ActiveDirectory"); //I tried new string[] {"ActiveDirectory"} too.

// Add the command to the runspace's pipeline
runspace.Open();
var pipeline = runspace.CreatePipeline();
pipeline.Commands.Add(command);

// Execute the command
var results = pipeline.Invoke();

if (results.Count > 0)
      System.Diagnostics.Debug.WriteLine("SUCCESS");
else
      System.Diagnostics.Debug.WriteLine("FAIL");
好的,当我运行这个时,我有一个异常:

术语“导入模块”不能识别为cmdlet、函数、脚本文件或可操作程序的名称

请注意,我可以毫无问题地运行基本命令,如“新建邮箱”,因此问题实际上是“导入模块”

我真的不知道该怎么办,我做了很多研究,看起来有些人也有同样的问题,但我没有找到任何书面的解决方案

请注意,当我直接在服务器的powershell上运行“导入模块”时,一切都正常


我了解到这可能是powershell版本的问题,我使用的版本是4.0(在服务器上)

我怀疑您正在使用Exchange远程管理会话。如果是,则这是一个“无语言”受限会话。它将提供Exchange cmdlet和一些通用Powershell命令(如Get命令),但没有Import Module cmdlet

IMHO,拥有这两组cmdlet的最简单方法是从通用PS会话开始,导入AD模块,然后连接到Exchange管理会话,执行导入PSSession,并对Exchange管理内容使用隐式远程处理。如果您这样做,您不仅限于在Exchange服务器上运行它,而且不必安装Exchange管理工具

编辑:执行此操作的Powershell命令为:

Import-Module ActiveDirectory

$EXSession = new-pssession -configurationname Microsoft.Exchange -ConnectionURI http://<Exchange server name>/powershell/ -authentication kerberos 

Import-PSSession $EXSession
导入模块ActiveDirectory
$EXSession=new pssession-configurationname Microsoft.Exchange-ConnectionURIhttp:///powershell/ -身份验证kerberos
导入PSSession$EXSession

并不是说您必须在您的环境中提供要连接到的Exchange服务器的名称

我怀疑您正在使用Exchange远程管理会话。如果是,则这是一个“无语言”受限会话。它将有Exchange cmdlet和一些通用Powershell命令(如Get命令)可用,但它没有Import Module cmdlet。哦,好的,我对这个(Powershell,Exchange,…)很陌生。如果我不能用这种方法做到这一点,我应该怎么做?我希望访问尽可能多的属性(ad属性)。谢谢你的帮助!:)最简单的方法是从通用PS会话开始,导入AD模块,然后连接到Exchange管理会话,执行导入PSSession,并对Exchange管理内容使用隐式远程处理。如果您这样做,您不仅限于在Exchange服务器上运行,而且不必安装Exchange管理工具。我正在阅读此解决方案atm!是的,我认为这是唯一的方法这不是唯一的方法,但我认为这是最好的方法。嗨,我正在尝试这样做,但我真的不知道如何实现这一点,它看起来真的很复杂。。。你能给我一个代码样本或什么吗?谢谢,我不能给你密码。我是Powershell脚本编写员和Exchange管理员。我理解您想要实现的目标,以及为什么这些课程无法加载该模块。我可以在本机Powershell中执行此操作,但我无法告诉您如何在C#中执行此操作。您能给我一个代码示例(Powershell)吗?谢谢您或者您可以在会话中设置
$ExecutionContext.SessionState.LanguageMode=“FullLanguage”
,然后导入模块将在其中工作。