【问题标题】:Using C# To Return Data From PowerShell使用 C# 从 PowerShell 返回数据
【发布时间】:2015-05-04 20:36:06
【问题描述】:

我正在尝试将 PrimarySMTPAddress 返回到一个变量,在 Powershell 中我运行的代码是这样的:

Get-Mailbox -identity UserName | select PrimarySMTPAddress

它返回正确的值,我想在我的 C# 代码中得到它,我做了以下操作:

string getPrimarySMTP = "Get-Mailbox -identity " + username + "| select PrimarySMTPAddress";

var runSpace = RunspaceFactory.CreateRunspace(Utility.CreateConnectionInfo());
runSpace.Open();
var pipeline = runSpace.CreatePipeline();

pipeline.Commands.AddScript(getPrimarySMTP);
var primarySmtp = pipeline.Invoke();
runSpace.Dispose();

我希望这会返回相同的数据,但事实并非如此。我只是得到一个例外:

“select”一词未被识别为 cmdlet、函数的名称, 脚本文件或可运行的程序。检查名称的拼写,或 如果包含路径,请验证路径是否正确并重试。

这是从 powershell 命令返回值的方式吗?

【问题讨论】:

  • 我对 C# 几乎一无所知,但 Select-Object 可以代替它吗?也许它确实不知道别名
  • @JStellato 这个问题怎么样了?任何一个答案都有帮助吗?

标签: c# powershell exchange-server


【解决方案1】:

对于什么版本的 Exchange ?对于 2010 年,您需要使用 Remote Powershell 参见 https://msdn.microsoft.com/en-us/library/office/ff326159%28v=exchg.150%29.aspx 。 (即使在 2007 年,您的代码也可以工作,因为您还没有加载管理单元)。

干杯 格伦

【讨论】:

    【解决方案2】:

    您可能需要在管道之前添加一个额外的空格字符。它被连接到用户名,结果字符串变为... -identity UserName| select ..."

    以下是更正后的陈述:

    string getPrimarySMTP = "Get-Mailbox -identity " + username + " | select PrimarySMTPAddress";
    

    【讨论】:

      【解决方案3】:

      感谢您提出这个问题,它帮助我找到了我需要的答案。我的代码在 Exchange 2013 环境中使用 RemoteRunspace 导致以下结果:

          try
          {
              var target = new Uri(Uri);
              SecureString PSPassword = new SecureString();
              foreach (char c in ConfigurationManager.AppSettings["Password"])
              {
                  PSPassword.AppendChar(c);
              }
              //var cred = (PSCredential)null;
              PSCredential cred = new PSCredential(ConfigurationManager.AppSettings["Username"], PSPassword);
              WSManConnectionInfo connectionInfo = new WSManConnectionInfo(target, shell, cred);
              connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Default;
              connectionInfo.OperationTimeout = 1 * 60 * 1000; // 4 minutes.
              connectionInfo.OpenTimeout = 1 * 30 * 1000; // 1 minute.
              using (Runspace remoteRunspace = RunspaceFactory.CreateRunspace(connectionInfo))
              {
                  remoteRunspace.Open();
                  using (PowerShell powershell = PowerShell.Create())
                  {
                      powershell.Runspace = remoteRunspace;
                      powershell.AddScript(PSSnapin);
                      powershell.Invoke();
                      powershell.Streams.ClearStreams();
                      powershell.Commands.Clear();
                      Pipeline pipeline = remoteRunspace.CreatePipeline();
                      Command getMailBox = new Command("Get-Mailbox");
                      getMailBox.Parameters.Add("Identity", Username);
                      Command cmd = new Command("Select-Object");
                      string[] Parameter = new string[] { "PrimarySMTPAddress" };
                      cmd.Parameters.Add("Property", Parameter);
                      pipeline.Commands.Add(getMailBox);
                      pipeline.Commands.Add(cmd);
                      Collection<PSObject> results = pipeline.Invoke();
                      primarySMTPAddress = results[0].ToString();
                      primarySMTPAddress = primarySMTPAddress.ToUpper().Replace("@{PRIMARYSMTPADDRESS=", "");
                      primarySMTPAddress = primarySMTPAddress.ToUpper().Replace("}", "");
                  }
                  remoteRunspace.Close();
              }
              return primarySMTPAddress;
          }
          catch
          {
              return "Error";
          }
      

      希望这对将来的任何人都有帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-20
        • 2019-07-20
        • 1970-01-01
        相关资源
        最近更新 更多