при попытке выполнить командлет exchange powershell new-mailBox не удалось в c # - удаленный доступ на сервере Exchange 2016
Добавить учетную запись пользователя в другую группу для выполнения прав доступа
string connectionUri = "http://xxx.contosoe.com/powershell/";
string loginPassword = "password";
SecureString secpassword = new SecureString();
foreach (char c in loginPassword)
{
secpassword.AppendChar(c);
}
PSCredential credential = new PSCredential(@"doamin\user", secpassword);
Runspace runspace = RunspaceFactory.CreateRunspace(rsConfig);
PowerShell powershell = PowerShell.Create();
powershell.Commands.AddCommand("Import-Module").AddArgument("ActiveDirectory");
//
PSCommand command = new PSCommand();
command.AddCommand("New-PSSession");
command.AddParameter("ConfigurationName", "Microsoft.Exchange");
command.AddParameter("ConnectionUri", new Uri(connectionUri));
command.AddParameter("Credential", credential);
command.AddParameter("Authentication", "KerBeros");
powershell.Commands = command;
runspace.Open();
powershell.Runspace = runspace;
Collection<PSObject> result = powershell.Invoke();
if (powershell.Streams.Error.Count > 0 || result.Count != 1)
{
throw new Exception("Fail to establish the connection");
}
powershell = PowerShell.Create();
command = new PSCommand();
command.AddCommand("Set-Variable");
command.AddParameter("Name", "ra");
command.AddParameter("Value", result[0]);
powershell.Commands = command;
powershell.Runspace = runspace;
powershell.Invoke();
powershell = PowerShell.Create();
command = new PSCommand();
command.AddScript("Import-PSSession -Session $ra");
powershell.Commands = command;
powershell.Runspace = runspace;
powershell.Invoke();
Pipeline pipeLine = runspace.CreatePipeline();
var emailCreateCommand = CreateEmailCommand("name", "displayName", "alias");
pipeLine.Commands.Add(emailCreateCommand);
Collection<PSObject> result_ = pipeLine.Invoke();
трассировка стека.... System.TypeInitializationException: инициализатор типа для «Microsoft.Exchange.Management.Common.NewUserBase».---> System.UnauthorizedAccessException: доступ к разделу реестра «Global» запрещен.Microsoft.Win32.RegistryKey.Win32Error (Int32 errorCode, String str) Microsoft.Win32.RegistryKey.InternalGetValue (имя строки, Object defaultValue, логическое значение doNotExpand, логическое checkSecurity).GetData (Строковый элемент) System.Diagnostics.PerformanceCounterLib.GetPerformanceData (Строковый элемент), String machineName) Microsoft.Exchange.Management.Common.NewUserBase..cctor ()
Microsoft.Exchange.Management.Common.NewUserBase.InternalBeginProcessing()
Microsoft.Exchange.Management.RecipientTasks.NewMailboxBase.InternalBeginProcessing()
Microsoft.Exchange.Management.RecipientTasks.NewMailboxOrSyncMailbox.InternalBeginProcessing()
Microsoft.Exchange.Configuration.Tasks.Task.<BeginProcessing>b__83_1()
Microsoft.Exchange.Configuration.Tasks.Task.InvokeRetryableFunc(String funcName, Action func, Boolean terminatePipelineIfFailed)