【问题标题】:Is there any tool that can do c# code to powershell是否有任何工具可以将 c# 代码执行到 powershell
【发布时间】:2013-05-27 13:21:08
【问题描述】:

我想知道是否有可以将 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();
                }
            }
        }
    }
}

【问题讨论】:

  • 我想知道我的帖子有什么理由得到负面反馈。我只是想知道是否有另一种将 c# 转换为 powershell 的方法。由于突然合并,我们正处于严重的时间紧缩状态。对不起,如果我把帖子搞砸了。
  • 这个工具好像叫@Sharken :)
  • @torres 我没有投反对票,但既然你问了,我想你可能会从回复中受益。通常,SO 社区认为“翻译我的代码”类型的帖子不是好问题。他们往往对提问者表现出很少的努力,过于本地化,并且很难让其他人学习。我怀疑您使用“是否有工具可以进行此翻译”的措辞被视为将“翻译我的代码”变成问题的肤浅尝试。
  • 为什么会有这么多反对票?这个问题没问题。

标签: c# sharepoint powershell sharepoint-2010 cmdlets


【解决方案1】:

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

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

myFunction Parameter1 Parameter2

另外,您确实应该安装 PowerShell 3.0 并使用 Windows PowerShell ISE 工具来开发代码。 无论如何,让您的 C# 代码在 PowerShell 中运行不会超过 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." + $_)
}

【讨论】:

【解决方案2】:

像上面这样的 cmets 正在使人们成群结队地远离 SO。 OP 的问题很明确,显示出真正的需要。

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

如前所述,从 PS 2 开始,您既可以内联运行 C#(或大多数其他语言),也可以引用格式正确的外部文件。我在这方面取得了喜忧参半的成功,但我不相信这是 OP 真正追求的。

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

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

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

【讨论】:

    【解决方案3】:

    我怀疑是否有类似的东西,但是编译 c# 代码不需要 Visual Studio。您可以在没有 VS 的情况下编译 exe。编译器 (csc.exe) 和 msbuild 包含在框架中。它们位于C:\Windows\Microsoft.NET\Framework\{version}

    如果您真的想从 powershell 调用它,请查看 Add-Type cmdlet。您向它提供源代码,它会即时编译源代码,然后将程序集加载到您的会话中。

    【讨论】:

      【解决方案4】:

      不确定在线工具,但下载免费的 Visual Studio Express 并关注 this tutorial 应该让您立即创建一个 cmdlet

      【讨论】:

      • 链接已损坏。
      • 我认为您甚至无法再获得 Visual Studio Express。但是这个帖子已经快8岁了
      猜你喜欢
      • 2010-12-06
      • 2012-01-05
      • 1970-01-01
      • 1970-01-01
      • 2015-06-10
      • 2011-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多