使用ASP.NET C#连接到poweshell [英] Connect to poweshell using ASP.NET C#

查看:93
本文介绍了使用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", "user@domain.com");
 
      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", "user@domain.com");

            pipelineGetMailBox.Commands.Add(getMailBox);

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

            pipelineGetMailBox.Dispose();

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

推荐答案

你在哪里删除现有的邮箱?

还要检查当前尝试触发的命令是否在powershell中手动工作。



你还必须开火作为'点'脚本。那将是:



Where are you deleting the existing mailboxes?
Also check if the command you currently trying to fire works manually in the powershell.

You also have to fire is as a 'dot' script. That would be:

. .\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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆