Is there any tool that can use C # code for powershell

I was wondering if there is an online tool that can convert C # code to powershell cmdlet code. I have the following code that I need in order to have it. I do not have a visual studio to turn it into 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(); } } } } } 
+4
source share
4 answers

The quickest way to do this is to write the PowerShell code itself. Below is an example of how the code will look in PowerShell, I would say that most C # developers should be able to understand the concepts of converting C # code to PowerShell in a very short time.

Functions can be a little weird at the beginning, as the usual PS syntax

 myFunction Parameter1 Parameter2 

You also really need to install PowerShell 3.0 and use the Windows PowerShell ISE tool for code development. In any case, you won’t need more than 1-2 hours for your C # code to work 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." + $_) } 
+6
source

He comments like the ones above that distract people from SO in the crowd. The OP question was unambiguous and reflected the real need.

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

As already discussed, with PS 2 you can either run C # (or most other languages) in a line, or refer to a well-formed external file. I had mixed success with this, and I don't believe the OP really came after.

If you really want to convert code (especially compiled assemblies), then a decompiler such as Reflector can do this, and - with the PowerShell add-on - can also convert it on the fly.

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

If you want your input and output to be done in the PS console, you still have to do some obvious re-recordings. But this method turned out to be incredibly useful to me.

+6
source

I doubt there is something like this remotely, however Visual Studio does not need to compile C # code. You can compile exe without VS. The compiler (csc.exe) and msbuild are included as part of the framework. They are located in C:\Windows\Microsoft.NET\Framework\{version} .

If you really want to call it from powershell, take a look at the Add-Type cmdlet. You provide it with the source code, and it will compile the source code on the fly, and then load the assembly into your session.

+1
source

Not sure about online tools, but download the free Visual Studio Express and follow in this tutorial you need to create a cmdlet in an instant

0
source

Source: https://habr.com/ru/post/1483835/


All Articles