c# - 是否有任何工具可以对 powershell 执行 c# 代码

标签 c# sharepoint powershell sharepoint-2010 powershell-cmdlet

我想知道是否有一个在线工具可以将 c# 代码转换为 powershell cmdlet 代码。我有以下代码,我需要它 powershell。我没有 visual studio 可以将其转换为 exe 或 dll。任何帮助或想法都会很棒。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace CopyUsersBetweenGroupsInSharepointByRR
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("This tool will copy the users from one group to another group");
            Console.WriteLine("Please enter the URL of the site where your groups are available");
            String siteUrl = Console.ReadLine();
            using (SPSite site = new SPSite(siteUrl))
            {
                try
                {
                    SPWeb web = site.OpenWeb();
                    Console.WriteLine("Please enter the name of the source group");
                    String sourceGroupName = Console.ReadLine();
                    Console.WriteLine("Please enter the name of the destination group");
                    String destinationGroupName = Console.ReadLine();
                    SPGroup sourceGroup = web.Groups[sourceGroupName];
                    SPGroup destinationGroup = web.Groups[destinationGroupName];
                    SPUserCollection sourceUsers = sourceGroup.Users;
                    SPUserInfo[] sourceUserInfoArray = new SPUserInfo[sourceUsers.Count];
                    for (int i = 0; i < sourceUsers.Count; i++)
                    {
                        sourceUserInfoArray[i] = new SPUserInfo();
                        sourceUserInfoArray[i].LoginName = sourceUsers[i].LoginName;
                        sourceUserInfoArray[i].Name = sourceUsers[i].Name;
                    }
                    destinationGroup.Users.AddCollection(sourceUserInfoArray);
                    destinationGroup.Update();
                    web.Update();
                    Console.WriteLine("Operation Completed Successfully");
                    Console.ReadLine();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    Console.ReadLine();
                }
            }
        }
    }
}

最佳答案

正是像上面那些评论让人们成群结队地远离 SO。 OP 的问题毫不含糊,显示出真正的需要。

有几种方法可以实现这一点。重写整个 C# 代码存储库不是其中之一。

如前所述,从 PS 2 开始,您可以内联运行 C#(或大多数其他语言),或引用格式良好的外部文件。我在这方面取得了不同程度的成功,但我不相信这是 OP 真正想要的。

如果您真的想转换代码(特别是编译后的程序集),那么像 Reflector 这样的反编译器就可以做到这一点,并且使用 PowerShell 插件也可以即时转换它。

http://blog.lekman.com/2011/10/converting-c-to-powershell.html

如果您希望在 PS 控制台中进行输入和输出,那么您仍然需要进行一些明显的重写。但事实证明,这种方法对我非常有用。

关于c# - 是否有任何工具可以对 powershell 执行 c# 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16862051/

相关文章:

c# - 如何修复系统数据 pdb 未加载

c# - 用 Angular 2 应用程序替换现有的 MVC 项目

c# - 为什么没有读取 log4net 配置文件

ASP.NET MVP 与 ASP.NET MVC

c# - 使用 ASP.NET 或 Sharepoint

powershell - 只需获取 Powershell Object [collection?] 的类型(不是方法等)(也是 : what's going on here? )

powershell - 无法通过Powershell将星号字符传递给外部命令

c# - 排列/测量问题 - WPF 中的布局是否损坏?

c# - JavaScript Object.create 和 IE8

powershell - 如何从函数语句中退出 powershell 函数