Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/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
C# 如何使用Powershell包装器System.Management.Automation从集合中添加/删除元素_C#_Powershell_Exchange Server - Fatal编程技术网

C# 如何使用Powershell包装器System.Management.Automation从集合中添加/删除元素

C# 如何使用Powershell包装器System.Management.Automation从集合中添加/删除元素,c#,powershell,exchange-server,C#,Powershell,Exchange Server,我正在尝试将别名集合添加到Exchange server。这只能通过Powershell cmdlet完成。 由于Microsoft在powershell下有包装器,分布式调用只能在运行空间中完成,因此我使用System.Management.Automation实用程序来实现这一点。 添加别名的命令如下所示: Set-Mailbox -Identity john@contoso.com -EmailAddresses @{add=”john@northamerica.contoso.com”}

我正在尝试将别名集合添加到Exchange server。这只能通过Powershell cmdlet完成。 由于Microsoft在powershell下有包装器,分布式调用只能在运行空间中完成,因此我使用System.Management.Automation实用程序来实现这一点。 添加别名的命令如下所示:

Set-Mailbox -Identity john@contoso.com -EmailAddresses @{add=”john@northamerica.contoso.com”}
其中Set Mailbox是一个命令,所有其他字段都是参数,@add显示我们向现有集合添加新元素

由于Exchange运行空间在PSLanguageMode.NoLanguage模式下运行,因此只能执行命令,不能执行脚本。在这种方法中,出现了以下例外情况:

Command addAliasCommand = new Command("Set-Mailbox -Identity john@contoso.com -EmailAddresses @{add=”john@northamerica.contoso.com”}", true);
只能执行带有参数的清除命令:

Command addAliasCommand = new Command("Set-Mailbox", true);
addAliasCommand.Parameters.Add("identity", "test@test.onmicrosoft.com");
addAliasCommand.Parameters.Add("EmailAddresses", "testing.alias10@test.onmicrosoft.com, testing.alias11@test.onmicrosoft.com");
但这种方法的问题是,当我想添加/删除新别名时,它完全重写了别名集合

问题是如何添加指针@add,以显示这些值已添加到ProxyAddressCollection的现有集合中

完整代码:

System.Security.SecureString secureString = new System.Security.SecureString();

foreach (char c in Password)
    secureString.AppendChar(c);

PSCredential credential = new PSCredential(AdminLogin, secureString);
WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("https://ps.outlook.com/PowerShell"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credential);
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
connectionInfo.SkipCACheck = true;
connectionInfo.SkipCNCheck = true;

connectionInfo.MaximumConnectionRedirectionCount = 4;
IList<string> gmResults = null;

using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))
{
    runspace.Open();

    using (Pipeline plPileLine = runspace.CreatePipeline())
    {
        try
        {
            Command addAliasCommand = new Command("Set-Mailbox", true);
            addAliasCommand.Parameters.Add("identity", "test@test.onmicrosoft.com");
            addAliasCommand.Parameters.Add("EmailAddresses", "testing.alias10@test.onmicrosoft.com, testing.alias11@test.onmicrosoft.com");

            var rsResultsresults = plPileLine.Invoke();

            if (!string.IsNullOrEmpty(resultObjectName))
            {
                gmResults =
                    rsResultsresults.Select(obj => obj.Members[resultObjectName].Value.ToString()).ToList();
            }

            plPileLine.Stop();
        }
        catch (Exception e)
        {
            return null;
        }
        finally
        {
            runspace.Close();
            runspace.Dispose();
        }
    }
    runspace.Close();
}
System.Security.SecureString SecureString=new System.Security.SecureString();
foreach(密码中的字符c)
AppendChar(c);
PSCredential=新PSCredential(AdminLogin,secureString);
WSManConnectionInfo connectionInfo=新的WSManConnectionInfo(新的Uri(“https://ps.outlook.com/PowerShell"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange“,凭证);
connectionInfo.AuthenticationMechanism=AuthenticationMechanism.Basic;
connectionInfo.SkipCACheck=true;
connectionInfo.skipncheck=true;
connectionInfo.MaximumConnectionRedirectionCount=4;
IList gmResults=null;
使用(Runspace Runspace=RunspaceFactory.CreateRunspace(connectionInfo))
{
Open();
使用(Pipeline plPileLine=runspace.CreatePipeline())
{
尝试
{
Command addAliasCommand=new命令(“设置邮箱”,true);
addAliasCommand.Parameters.Add(“标识”test@test.onmicrosoft.com");
addAliasCommand.Parameters.Add(“电子邮件地址”,“测试”。alias10@test.onmicrosoft.com,测试。alias11@test.onmicrosoft.com");
var rsResultsresults=plPileLine.Invoke();
如果(!string.IsNullOrEmpty(resultObjectName))
{
gmResults=
rsResultsresults.Select(obj=>obj.Members[resultObjectName].Value.ToString()).ToList();
}
plPileLine.Stop();
}
捕获(例外e)
{
返回null;
}
最后
{
runspace.Close();
Dispose();
}
}
runspace.Close();
}

我添加了相同的问题。我最后用了这个:

var pipeline = runspace.CreatePipeline();

string cmdAlias = "Set-Mailbox " + username + "@" + domainName + " -EmailAddresses @{Add='" + username + "@" + domainNameAlias + "'}";
pipeline.Commands.AddScript(cmdAlias);
pipeline.Invoke();
@{add=”john@northamerica.contoso.com“}
实际上是一个
@{key=value}
结构,因此您可以执行以下操作:

Command addAliasCommand = new Command("Set-Mailbox", true);
addAliasCommand.Parameters.Add("identity", "john@contoso.com");

var addresses = new Hashtable();
addresses.Add("add", "john@northamerica.contoso.com");
addAliasCommand.Parameters.Add("EmailAddresses", addresses);