我不理解我遇到的NewNotImplementedException运行时错误

7

我正在尝试实现一个远程运行空间,需要连接信息来与Exchange通信并导入模块以与活动目录进行通信。这是有问题的代码:

runspace = System.Management.Automation.Runspaces.RunspaceFactory.
CreateRunspace(psConnectionInfo);
runspace.InitialSessionState.ImportPSModule(new[] { "ActiveDirectory" });
runspace.Open();

我得到的运行时错误是:
``` 无法执行操作,因为操作“文件:行:列中偏移量为32的NewNotImplementedException”未实现 ```
如果我省略`runspaceInitialSessionState`行,则不会出现错误,但PowerShell命令`SetADServerSettings`无法运行,因为它无法被识别。
堆栈跟踪:
``` 无法执行操作,因为操作“文件:行:列中偏移量为32的NewNotImplementedException”未实现。 在System.Management.Automation.RemoteRunspace.get_InitialSessionState() 在ManageUserForwardsWS.ManageUserForwards.SetExchangeCredentials(String userName, String PwString) in c:\Users\rtanner.CATNET\Documents\Visual Studio 2013\Projects\ManageUserForwardsWS\ManageUserForwardsWS\ManageUserForwards.asmx.cs:line 122 ```
我也可以使用以下代码生成同样的错误。
        Pipeline pipeline = runspace.CreatePipeline();

        PowerShell powershell = PowerShell.Create();
        powershell.Runspace = pipeline.Runspace;

        powershell.Runspace.InitialSessionState.ImportPSModule(new[] { "ActiveDirectory" });

以下是StackTrace:

无法执行操作,因为操作“文件中偏移量为32的NewNotImplementedException : 行:0:0”未实现。 at System.Management.Automation.RemoteRunspace.get_InitialSessionState() at ManageUserForwardsWS.ManageUserForwards.SetForward(String sAMAccountName, String fowardAddress) in c:\Users\rtanner.CATNET\Documents\Visual Studio 2013\Projects\ManageUserForwardsWS\ManageUserForwardsWS\ManageUserForwards.asmx.cs:line 151

这些额外的信息有帮助吗?有什么修复的想法吗?


1
你介意为那个错误添加堆栈跟踪吗?这可能会提供一些见解。 - OnoSendai
第122行(请参见堆栈跟踪)是“ImportPSModule()”行。 - caspersgrin
欢迎来到SO,Rob。+1给你的问题,因为你展现了很好的努力。不过,你之前的评论应该真正地被编辑进问题中,因为它可能会帮助其他人来帮助你,而你也不想让它隐藏在评论中。 - Christoffer Lette
这可能是您的ActiveDirectory模块上出现了错误,或者是自动生成的方法自动实现了NotImplementedException。查看此线程:https://dev59.com/z2zXa4cB1Zd3GeqPRDSF - OnoSendai
是的,我认为你说得对,我将让我的Windows管理员为我检查一下。在MD shell内,我输入命令powershell进入powershell命令窗口。在这个命令窗口中,我输入命令:import-module ActiveDirectory,它可以正常工作,然后输入Set-AdServerSettings -ViewEntireForest $True。返回的响应基本上是未能识别它作为cmdlet、脚本或可执行程序的名称。这是错误的,因为该cmdlet在ActiveDirectory模块中。 - caspersgrin
2个回答

1

它可能同时存在,但我之前在ActiveDirectory中使用过它。问题是导入模块时失败了。代码在到达Set-AdServerSettings cmdlet之前就失败了。我可以通过另一种方式在PSImport上生成错误,但这是不同的错误,并且我已更新我的原始帖子以包含它。 - caspersgrin

0

远程Runspace构造函数不会初始化和使用InitialSessionState对象。它仅适用于本地runspaces。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接