栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【.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、配置文件

 
 
 
 
 
 

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
 /// 
        /// 运行power shell命令
        /// 
        /// 
        /// 
        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 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;
            }
        }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1035920.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号