为TFS中指定组中的项目添加和删除指定权限C#代码实现

本文提供了一段自动化脚本,用于在Team Foundation Server (TFS)中为指定组添加或移除指定权限。脚本通过调用TFSSecurity.exe命令来实现这一功能,包括添加权限、移除权限、执行添加权限操作以及执行移除权限操作。同时,脚本还展示了如何获取项目集下所有项目的权限组(项目组),并返回正确输出流和错误输出流。
        /// <summary>
        /// 为指定组添加指定权限
        /// </summary>
        /// <param name="PermissionsName">权限名称</param>
        /// <param name="GroupName">组名称</param>
        /// <param name="ProjectName">组所属的项目</param>
        /// <returns></returns>
        public static string AddPowerForGroup(string PermissionsName, string GroupName, string ProjectName, string CollectionURI)
        {
            StringBuilder sp = new StringBuilder();
            sp.Append("/a+ Project FrameworkGlobalSecurity #PermissionsName "+'"'+"n:[#ProjectName]\\#GroupName"+'"'+" ALLOW /collection:#CollectionURI");
            sp.Replace("#PermissionsName", PermissionsName);
            sp.Replace("#GroupName", GroupName);
            sp.Replace("#ProjectName", ProjectName);
            sp.Replace("#CollectionURI", CollectionURI);
            return sp.ToString();
        }

    /// <summary>
        /// 为指定组移出指定权限
        /// </summary>
        /// <param name="PermissionsName">权限名称</param>
        /// <param name="GroupName">组名称</param>
        /// <param name="ProjectName" >所属的团队项目的名称,例如:surekamspm3</param>
        /// <returns></returns>
        public static string MovePowerForGroup(string PermissionsName, string GroupName, string ProjectName, string CollectionURI)
        {
            StringBuilder sp = new StringBuilder();
            sp.Append("/a- Server FrameworkGlobalSecurity #PermissionsName "+'"'+"n:[#ProjectName]\\#GroupName"+'"'+" ALLOW /collection:#CollectionURI");
            sp.Replace("#PermissionsName", PermissionsName);
            sp.Replace("#GroupName", GroupName);
            sp.Replace("#ProjectName", ProjectName);
            sp.Replace("#CollectionURI", CollectionURI);
            return sp.ToString();
        }

       /// <summary>
        /// 调用tfssecurity为指定组授予指定的权限
        /// </summary>
        /// <param name="PermissionsName">权限名称</param>
        /// <param name="GroupName">组名称</param>
        /// <returns></returns>
        public static string ExcuteAddPowerForGroup(string PermissionsName, string GroupName, string ProjectName, string CollectionURI)
        {
            Process p = new Process();
            string cmdStr = AddPowerForGroup(PermissionsName, GroupName,ProjectName, CollectionURI);
            p.StartInfo.FileName = "C:\\Program Files (x86)\\Microsoft Visual Studio 10.0\\Common7\\IDE\\TFSSecurity.exe";

            //执行命令及参数
            p.StartInfo.Arguments = cmdStr;
            p.StartInfo.UseShellExecute = false; //关闭Shell的使用
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = true;//设置不显示窗口
            p.Start();//启动进程

            string standardOutput = p.StandardOutput.ReadToEnd();
            string ErrorOutput = p.StandardError.ReadToEnd();
            string message = standardOutput + "#" + ErrorOutput;
            return message;
        }

        /// <summary>
        /// 为指定的组删除指定的权限
        /// </summary>
        /// <param name="PermissionsName">权限名称</param>
        /// <param name="GroupName">组名称</param>
        /// <returns></returns>
        public static string ExcuteMovePowerForGroup(string PermissionsName, string GroupName, string ProjectName, string CollectionURI)
        {
            Process p = new Process();
            string cmdStr = MovePowerForGroup(PermissionsName, GroupName, ProjectName, CollectionURI);
            p.StartInfo.FileName = "C:\\Program Files (x86)\\Microsoft Visual Studio 10.0\\Common7\\IDE\\TFSSecurity.exe";

            //执行命令及参数
            p.StartInfo.Arguments = cmdStr;
            p.StartInfo.UseShellExecute = false; //关闭Shell的使用
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = true;//设置不显示窗口
            p.Start();//启动进程

            string standardOutput = p.StandardOutput.ReadToEnd();
            string ErrorOutput = p.StandardError.ReadToEnd();
            string message = standardOutput + "#" + ErrorOutput;
            return message;
        }

        /// <summary>
        /// 执行获取项目集下所有项目的权限组(项目组)的命令
        /// </summary>
        /// <param name="collectionUrl">项目URL,例如;http://10.2.11.35:8080/tfs/SureKAMSPM3 </param>
        /// <returns>返回正确输出流 + "#" + 错误输出流</returns>
        public static string ExcuteGetAuthortyGroup(string collectionUrl)
        {
            Process p = new Process();
            string cmdStr = ProduceCMD.GetAuthorityGroup(collectionUrl);
            p.StartInfo.FileName = "C:\\Program Files (x86)\\Microsoft Visual Studio 10.0\\Common7\\IDE\\TFSSecurity.exe";

            //执行命令及参数
            p.StartInfo.Arguments = cmdStr;
            p.StartInfo.UseShellExecute = false; //关闭Shell的使用
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = true;//设置不显示窗口
            p.Start();//启动进程

            string standardOutput = p.StandardOutput.ReadToEnd();
            string ErrorOutput = p.StandardError.ReadToEnd();
            string message = standardOutput + "#" + ErrorOutput;
            return message;
        }

评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

邹琼俊

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

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

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

打赏作者

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

抵扣说明:

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

余额充值