Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/6.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# PSCommand在Windows应用商店应用程序上不起作用_C#_Powershell_Windows Runtime_Exchange Server_Office365 - Fatal编程技术网

C# PSCommand在Windows应用商店应用程序上不起作用

C# PSCommand在Windows应用商店应用程序上不起作用,c#,powershell,windows-runtime,exchange-server,office365,C#,Powershell,Windows Runtime,Exchange Server,Office365,我正在构建一个windows应用商店应用程序。我需要通过powershell命令连接到exchange server。为了执行我的命令,我编写了以下代码 PSCommand d_cmd = new PSCommand(); d_cmd.AddCommand("$secure_string_pwd = convertto-securestring 'testpa' -asplaintext -force"); d_cmd.AddCommand("$u

我正在构建一个windows应用商店应用程序。我需要通过powershell命令连接到exchange server。为了执行我的命令,我编写了以下代码

PSCommand d_cmd = new PSCommand();
            d_cmd.AddCommand("$secure_string_pwd = convertto-securestring 'testpa' -asplaintext -force");
            d_cmd.AddCommand("$username = 'test@email.com'");
            d_cmd.AddCommand("$cred = New-Object System.Management.Automation.PSCredential $username, $secure_string_pwd");
            d_cmd.AddCommand("Import-Module MSOnline");
            d_cmd.AddCommand("$O365Cred = Get-Credential");
            d_cmd.AddCommand("$O365Session = New-PSSession –ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $O365Cred -Authentication Basic -AllowRedirection");
            d_cmd.AddCommand("Import-PSSession $O365Session");
当我执行上述操作时,我得到如下错误

在模块mscorlib.dll中找不到类型System.SystemException

当我删除system.management.automation时,该错误消失,但未执行powershell命令


有谁能建议我如何在windows应用商店应用程序中执行powershell命令吗?

您是否以交互方式运行此命令?因为“获取无参数的凭据”会打开一个对话框,询问凭据。另外,您没有使用
$cred
。看起来您应该在以
“$O365Session=New PSSession
。如果我也评论了Get Credential,它给出的是相同的错误。我认为这是system.management.automation DLL的问题。有其他选择吗?当我注释掉上面的所有语句时,它给出了一个错误,如“在模块mscorlib.dll中找不到类型System.SystemException”。当我删除参考dll System.management.automation时,则没有错误。有什么解决方案吗?你是说当你删除参考DLL system.management.automation时,它会工作吗?还有,当你说当你把所有的陈述都注释掉的时候,你的意思是除了第一个之外的所有陈述?