PowerShell/Asp.net c#/Exchange
我正在尝试使用从C#调用的PowerShell将用户添加到Office365组 我试过PS脚本,它很有效 当我从C#调用它时,它不会 以下是我的脚本代码: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
$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 ();