有没有任何工具可以对powershell执行c#代码

tor*_*res 5 c# powershell cmdlets sharepoint sharepoint-2010

我想知道是否有一个可以将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();
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Sha*_*ken 6

最快的方法是自己编写PowerShell代码.下面是代码在PowerShell中的外观,我想说大多数C#开发人员应该能够在很短的时间内掌握将C#代码转换为PowerShell的概念.

函数在开始时可能有点奇怪,因为通常的PS语法是

myFunction Parameter1 Parameter2
Run Code Online (Sandbox Code Playgroud)

您还应该安装PowerShell 3.0并使用Windows PowerShell ISE工具来开发代码.无论如何,在PowerShell中运行C#代码不应超过1-2个小时.

[System.Reflection.Assembly]::LoadWithPartialName(”Microsoft.SharePoint”) 
Write-Host "This tool will copy the users from one group to another group"
Write-Host "Please enter the URL of the site where your groups are available"
[string] $siteUrl = [Console]::ReadLine()

$site = new-object Microsoft.SharePoint.SPSite($siteUrl) 
try
{
  $web = $site.OpenWeb()
  Write-Host "Please enter the name of the source group"
  [string] $sourceGroupName = [Console]::ReadLine()
  Write-Host "Please enter the name of the destination group"
  [string] $destinationGroupName = [Console]::ReadLine()
  $sourceUsers = $web.Groups[$sourceGroupName]

  (and so on)
}
catch
{
  Write-Error ("Failed to copy sharepoint users." + $_)
}
Run Code Online (Sandbox Code Playgroud)


小智 6

像上面的那些评论正在成群结队地让人们远离SO.OP的问题是明确的,并显示出真正的需要.

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

如前所述,从PS 2开始,您既可以内联运行C#(或大多数其他语言),也可以参考格式正确的外部文件.我已经取得了不同程度的成功,我不相信这是OP真正追求的.

如果你真的想要转换代码(特别是编译的程序集),那么像Reflector这样的反编译器能够做到这一点,并且 - 使用PowerShell插件 - 也能够即时转换它.

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

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