Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.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
PowerShell/Asp.net c#/Exchange_C#_Powershell_Exchange Server - Fatal编程技术网

PowerShell/Asp.net c#/Exchange

PowerShell/Asp.net c#/Exchange,c#,powershell,exchange-server,C#,Powershell,Exchange Server,我正在尝试使用从C#调用的PowerShell将用户添加到Office365组 我试过PS脚本,它很有效 当我从C#调用它时,它不会 以下是我的脚本代码: $username = "email@domain.com"; $pass_word = "PassWord"; $ListName = "group-mail@domain.com"; $email = "user-mail@domain.com"; $pas

我正在尝试使用从C#调用的PowerShell将用户添加到Office365组

我试过PS脚本,它很有效

当我从C#调用它时,它不会

以下是我的脚本代码:

$username = "email@domain.com";
$pass_word = "PassWord";
$ListName = "group-mail@domain.com";
$email = "user-mail@domain.com";

$password = ConvertTo-SecureString $pass_word -AsPlainText -Force
$psCred = New-Object System.Management.Automation.PSCredential -ArgumentList ($username, $password)

Import-Module ExchangeOnlineManagement
Connect-ExchnageOnline -Credential $psCred

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $psCred -Authentication Basic -AllowRedirection
Import-PSSession $Session

Add-UnifiedGroupLinks -Identity $ListName –LinkType Member –Links $email

Remove-PSSession $Session
C#代码:

要帮忙吗?谢谢


有人吗??救命!:)

在Powershell代码中,您正在调用Connect ExchangeOnline,然后手动创建会话。您应该只使用connect-ExchangeOnline(如果您在Exchange Online上拥有正确的权限,这将导入您需要的所有cmdlet)。老实说,对于C#应用程序,我永远不会采用这种方法:我只会使用MS Graphc将成员添加到组中。你能给我一些可以测试的示例吗?谢谢
RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create ();
Runspace runspace = RunspaceFactory.CreateRunspace (runspaceConfiguration);
runspace.Open ();

RunspaceInvoke runSpaceInvoker = new RunspaceInvoke (runspace);

string scriptfile = HttpContext.Current.Server.MapPath (@"scriptFile.ps1");

Pipeline pipeline = runspace.CreatePipeline ();
Command myCommand = new Command (scriptfile);

pipeline.Commands.Add (myCommand);

pipeline.Invoke ();
pipeline.Dispose ();
runspace.Close ();
runspace.Dispose ();