Asked by:
How do I convert C# code to PowerShell script?

Question
-
Completely new to powershell scripting here. But how do i convert this block of c# code to a powershell script? Is there a tool out there that newbies like myself can use?
public void BuildReport(string objectId, bool includesWebs, bool includesLists, bool includesLibraries, bool includesItems) { private bool m_includeWebs = includeWebs; private bool m_includeLists = includeLists; private bool m_includeLibrariess = includeLibrariess; private bool m_includeItems = includeItems; try { using (new SPMonitoredScope(this.GetType().FullName + CommonResources.Colon + new StackFrame(0).GetMethod().Name)) { using (SPSite site = new SPSite(m_siteUrl)) { if (ExpandADGroups || ShowGroupDescriptions) { InitADGroups(site); } initializeReport(site); m_serverUrl = GetServerUrl(site); InitReportColumns(site); AppendReportInfo(site, objectId); AppendHeaderRow(); switch (m_scope) { case ReportScope.Site: BuildSiteReport(site); break; case ReportScope.Subsite: BuildSiteReport(site); break; case ReportScope.Library: BuildSiteReport(site, objectId); break; case ReportScope.List: BuildSiteReport(site, objectId); break; } } } } catch (Exception ex) { } }
I really appreciate the help...
- Edited by Spawn10 Thursday, March 20, 2014 5:47 PM
- Moved by Bill_Stewart Saturday, July 5, 2014 8:06 PM This is not "translate C# code to script for me" forum
Thursday, March 20, 2014 5:44 PM
All replies
-
You could learn PowerShell and Net and C#. From there you could learn to convert C# code to PowerShell.
In the case of this code I would suggest just using the SharePoint library for PowerShell which has already coverted most of this code to PowerShell.
See the SDK for usage and class documentation:
http://www.microsoft.com/en-us/download/details.aspx?id=35585
OR
¯\_(ツ)_/¯
Thursday, March 20, 2014 7:01 PM -
This will bootstrap you into the DEveloper Dashboard and scope management/creation
http://graegert.de/blog/the-sharepoint-2010-developer-dashboard/
¯\_(ツ)_/¯
Thursday, March 20, 2014 7:05 PM -
Off the top of my head, here are the biggest things I keep in mind when converting C# to PowerShell:
- PowerShell has no "using" directive; you have to use the full namespace path to every class (with the exception that you can omit the "System." namespace from the beginning.)
- PowerShell also has no "using" statement for working with IDisposable objects. Typically, you'll want to use try/finally blocks to make sure the cleanup code is called.
- PowerShell can consume .NET types, but cannot create them (unless you embed C# code and use Add-Type), though you can technically simulate some class-like behavior with PSObjects and Add-Member to create ScriptProperties and ScriptMethods, etc.
- PowerShell can't use C#'s LINQ feature; you have to rewrite that logic in another way.
Aside from those, you can convert quite a bit of C# code to PowerShell almost verbatim, though there are usually ways to streamline things a bit in PowerShell: using -match, -replace or -split instead of System.Text.RegularExpression.Regex calls, etc.
Friday, March 21, 2014 2:30 AM - PowerShell has no "using" directive; you have to use the full namespace path to every class (with the exception that you can omit the "System." namespace from the beginning.)
-
Of course Linq is indirectly possible:
http://stackoverflow.com/questions/2869967/how-to-query-list-in-powershell
¯\_(ツ)_/¯
Friday, March 21, 2014 2:36 AM