【.Net】在.net项目中执行power shell命令

前言

在工作中经常会通过执行power shell命令来完成一系列操作,在.net中也可以直接调用power shell命令,具体过程如下

实现过程

1、所需dll

Microsoft.Exchange.WebServices   15.0.0.0

System.Management.Automation  3.0.0.0

2、配置文件

 <!--Exchange powershell 用户名 域名\域管理员-->
 <add key="SYS.Exchange.Name" value="域名\administrator"/>
 <!--Exchange powershell 密码 域账号密码-->
 <add key="SYS.Exchange.Password" value="123456!"/>
 <!--Exchange powershell 地址-->
 <add key="SYS.Exchange.URI" value="http://ocs-dev-aqqx.oms.com/PowerShell"/>

3、完整代码

此代码执行的power shell命令为如下,作用是为了解决问题:The specified folder could not be found in the store

Add-MailboxPermission -Identity “liuzhenhe@oms.com” -User "administrator@oms.com" -AccessRights FullAccess
 /// <summary>
        /// 运行power shell命令
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public string runPowerShell(string user)
        {
            string errorMsg = "";
            string url = ConfigurationManager.AppSettings["SYS.Exchange.URI"];
            string username = ConfigurationManager.AppSettings["SYS.Exchange.Name"];
            string pwd = ConfigurationManager.AppSettings["SYS.Exchange.Password"];
            System.Security.SecureString password = new System.Security.SecureString();
            foreach (char x in pwd)
            {
                password.AppendChar(x);
            }
            string liveIdconnectionUri = url;
            PSCredential credential = new PSCredential(username, password);
            WSManConnectionInfo connectionInfo = new WSManConnectionInfo((new Uri(liveIdconnectionUri)), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credential);
            connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos;

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

                try
                {
                    PowerShell ps = PowerShell.Create();
                    PSCommand command = new PSCommand();
                    command.AddCommand("Add-MailboxPermission");
                    command.AddParameter("Identity", user);
                    command.AddParameter("User", "administrator@oms.com");
                    command.AddParameter("AccessRights", "FullAccess");

                    ps.Commands = command;
                    ps.Runspace = runspace;
                    Collection<PSObject> results = ps.Invoke();

                    if (ps.HadErrors)
                    {
                        errorMsg = "Enable-Mailbox ERROR:";
                        if (ps.Streams.Error.Count > 0)
                        {
                            for (int i = 0; i < ps.Streams.Error.Count; i++)
                            {
                                errorMsg += ps.Streams.Error[i].Exception.ToString();
                            }
                        }
                    }
                    else
                    {
                        errorMsg = "";
                    }

                }
                catch (Exception ex)
                {
                    errorMsg = "Enable-Mailbox ERROR:";
                    if (ex.InnerException != null && ex.InnerException.Message != null)
                    {
                        errorMsg += ex.InnerException.Message;
                    }
                    else
                    {
                        errorMsg += ex.ToString();
                    }
                }
                finally
                {
                    runspace.Dispose();
                    runspace = null;
                }
                return errorMsg;
            }
        }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一起来学吧

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值