本文介绍了使用ASP.NET C#连接到poweshell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用.Net C#连接到power-shell。我需要获取邮箱的详细信息,创建新邮箱和删除现有邮箱。

我使用Get-Mailbox,New-Mailbox& 删除 - 邮箱。



但是当我调用命令时,它会将我抛出错误。



术语Get-Mailbox未被识别为cmdlet,函数,脚本文件或可操作程序的名称。检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试。



我连接到power-shell远程。



我尝试过:



I'm trying to connect to connect to power-shell using .Net C#. I need to get details of mailbox, create new mailbox and delete existing mailbox.
I use "Get-Mailbox","New-Mailbox" & "Remove-Mailbox".

But when I invoke the command it throws me below error.

The term 'Get-Mailbox' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included,verify that the path is correct and try again.

I connecting to power-shell remotely.

What I have tried:

Runspace runspace = null;
string shellUri = "http://schemas.microsoft.com/powershell/Microsoft.PowerShell";
PSCredential remoteCredential = new PSCredential(@"userid", StringToSecureString("password"));

WSManConnectionInfo connectionInfo = new WSManConnectionInfo(false, "ip address",5985, "/wsman", shellUri, remoteCredential, 1 * 60 * 1000);
connectionInfo.OperationTimeout = 4 * 60 * 1000;
runspace = RunspaceFactory.CreateRunspace(connectionInfo);
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
runspace.Open();
    using (PowerShell ps = PowerShell.Create())
    {
       ps.Runspace = runspace;
       ps.Commands.AddCommand("Get-Mailbox");
       ps.Commands.AddParameter("Identity", "[email protected]");

      var result = ps.Invoke();
      foreach (PSObject res in result)
      {
        Console.WriteLine("{0,-25}1}",res.Members["DisplayName"].Value,res.Members["PrimarySMTPAddress"].Value);
       }
      }
            runspace.Close();
            runspace.Dispose();



这个




And this

string shellUri = "http://schemas.microsoft.com/powershellMicrosoft.PowerShell";
PSCredential remoteCredential = new PSCredential(@"userid", StringToSecureString("password"));

WSManConnectionInfo connectionInfo = new WSManConnectionInfo(false, "ip address", 5985, "/wsman", shellUri, remoteCredential, 1 * 60 * 1000);
connectionInfo.OperationTimeout = 4 * 60 * 1000;
Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo);
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
runspace.Open();

            //Get Mail Box
            Pipeline pipelineGetMailBox = runspace.CreatePipeline();

            Command getMailBox = new Command("Get-Mailbox");
            getMailBox.Parameters.Add("OrganizationalUnit", "OU Name");
            getMailBox.Parameters.Add("Identity", "[email protected]");

            pipelineGetMailBox.Commands.Add(getMailBox);

            Collection<PSObject> mailboxGetResult = pipelineGetMailBox.Invoke();

            pipelineGetMailBox.Dispose();

            runspace.Close();
            runspace.Dispose();

推荐答案

. .\Get-Mailbox.{extension}





你还应该使用set-ExecutionPolicy Unrestricted或set-ExecutionPolicy AllSigned

结帐:



首先确保您可以在PowerShell中手动运行它。然后使用相同的参数等将脚本复制到您的代码中。



You should also use set-ExecutionPolicy Unrestricted or set-ExecutionPolicy AllSigned
Checkout: Execution Policy instructions

First make sure you can run it manually in the PowerShell. Than copy the script to your code with the same params etc.


这篇关于使用ASP.NET C#连接到poweshell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-15 22:57