Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any tool that can do c# code to powershell

I was wondering if there is an online tool that can convert c# code to powershell cmdlet code. I have following code that i need to have it powershell. I dont have visual studio to turn this into an exe or dll. any help or ideas would be great.

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();
                }
            }
        }
    }
}
like image 268
torres Avatar asked May 31 '13 16:05

torres


People also ask

Where can I write C code?

To write the source code of your first C program you need to open the Notepad++ text editor. The quickest way to do that in Windows 10 is to hit your Win key, type Notepad++ in the search window, and hit Enter.

Can you do C in Visual Studio?

Visual Studio Code is a lightweight, cross-platform development environment that runs on Windows, Mac, and Linux systems. The Microsoft C/C++ for Visual Studio Code extension supports IntelliSense, debugging, code formatting, auto-completion.

Is there an alternative to C?

The best alternative is Java. It's not free, so if you're looking for a free alternative, you could try C++ or Rust. Other great apps like C (programming language) are Go (Programming Language), C#, Lua and Perl.


2 Answers

It's comments like those above that are turning people away from SO in droves. The OP's question was unambiguous and displayed genuine need.

There are several ways to achieve this. Rewriting your entire C# code repository is not one of them.

As already discussed, as of PS 2 you are able to either run C# (or most any other language) inline, or refer to well-formed external file. I've had mixed success with this and I don't believe it's what the OP was really after.

If you genuinely want to convert code (particularly compiled assemblies) then a decompiler like Reflector is able to do this and - with the PowerShell addon - is also able to convert it on-the-fly.

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

If you want your input and output to take place within the PS console then you'd still have to perform some obvious re-writes. But this method has proved incredibly useful to me.

like image 27
Panoone Avatar answered Sep 18 '22 23:09

Panoone


The fastest way to do it is to write the PowerShell code yourself. Below is how the code will look in PowerShell, i would say that most C# developers should be able to grasp the concepts of converting C# code to PowerShell in a very short time.

Functions can be a little odd at the beginning, since the usual PS syntax is

myFunction Parameter1 Parameter2

Also you really should install PowerShell 3.0 and use the Windows PowerShell ISE tool to develop the code. Anyways it should not take you more than 1-2 hours to get your C# code running along in PowerShell.

[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." + $_)
}
like image 113
Sharken Avatar answered Sep 21 '22 23:09

Sharken