PowerShell Script with ASP.NET and Console Application

I have two projects that need to incorporate Windows PowerShell, Exchange Management Shell and ActiveRoles Management Shell for AD (by Quest) in a ASP.NET and a Console Application recently, so I started to look for information about PowerShell scripting with .NET and below are two important articles in my opinion:

  1. HOWTO: Using PowerShell in ASP.NET (.NET Framework 2.0)

The first article explains the most challenging issues in hosting PowerShell scripting with ASP.NET - Security context and permission and describe three common optioins. The second article describes the logic and tips in writing PowerShell in .NET managed code application.

I wrote a small class to execute the PowerShell script, by passing in various script text on the fly. Below is a small code snippet:

  private const string PS_SNAP_IN_MICROSOFT = "Microsoft.Exchange.Management.PowerShell.Admin";
  private const string PS_SNAP_IN_QUEST = "Quest.ActiveRoles.AdManagement";

            RunspaceConfiguration rsConfig = RunspaceConfiguration.Create();
            PSSnapInException snapInException = null;
            rsConfig.AddPSSnapIn(PS_SNAP_IN_MICROSOFT, out snapInException); // Add Exchange Mgmt SnapIn
            rsConfig.AddPSSnapIn(PS_SNAP_IN_QUEST, out snapInException);  // Add Quest Mgmt SnapIn
           
            Runspace myRunspace = RunspaceFactory.CreateRunspace(rsConfig);
            myRunspace.Open();

            Pipeline pipeLine = myRunspace.CreatePipeline();
            pipeLine.Commands.AddScript(scriptText);  // Pass in the script text on the fly
            pipeLine.Commands.Add("Out-String"); 

            Collection<PSObject> cmdResults = pipeLine.Invoke();

            myRunspace.Close();

After preparing these PowerShell scripting in .NET app, I'd suggest to stick with PowerShell because it's handy and flexible. I'd prefer it over DirectoryServices or CDOEXM commands. :) Read the useful articles mentioned above in sequence then you are ready to start coding!

Regards,
Colt

Published Monday, August 17, 2009 11:04 PM by Colt
Filed under: ,

Comments

# re: PowerShell Script with ASP.NET and Console Application

awesome to the point and exactly what i needed thanks!

Friday, January 8, 2010 5:57 PM by carter cole

# re: PowerShell Script with ASP.NET and Console Application

Powershell great article thanks and Asp.net

Wednesday, April 13, 2011 1:46 PM by jigolo

# re: PowerShell Script with ASP.NET and Console Application

every aspect of your site is really very well designed and carefully crafted articles have been written in regards wish you continued success..

Saturday, May 7, 2011 7:21 PM by hava perdesi

# re: PowerShell Script with ASP.NET and Console Application

Everything is explained in detail relevant to the subject site is within the existing social networking sites I came across a very impressive skins and best regards I wish you continued success with your site, I found the opportunity to examine in detail...

Thursday, May 12, 2011 12:02 PM by hava perdeleri

# re: PowerShell Script with ASP.NET and Console Application

Everything is explained in detail relevant to the subject site is within the existing social networking sites I came across a very impressive skins and best regards I wish you continued success with your site, I found the opportunity to examine in detail...

always good fun to listen to the radio, but also makes the radio is beautiful music played in

Tuesday, May 31, 2011 6:48 AM by hava perdesi

# re: PowerShell Script with ASP.NET and Console Application

Thanks very much always

Sunday, June 5, 2011 9:56 AM by hava perdeleri

# re: PowerShell Script with ASP.NET and Console Application

When you first log into Facebook you are taken to a home page where you can see recent status updates of your Friends. Keep an eye on the status updates and comment on them when appropriate.

Saturday, July 9, 2011 9:55 AM by ilahi dinle

# re: PowerShell Script with ASP.NET and Console Application

powershell script wonderful web page home page where you can see recent status updates of your Friends.

Monday, October 3, 2011 3:27 AM by web tasarim

# re: PowerShell Script with ASP.NET and Console Application

public lectures and events featuring world renowned figures since January 2012. You can listen to them or download them here

Thursday, January 12, 2012 8:54 AM by izmir rent a car

# re: PowerShell Script with ASP.NET and Console Application

various script text on the fly. Below is a small code snippet

Monday, June 11, 2012 7:52 AM by travesti

# re: PowerShell Script with ASP.NET and Console Application

When some one searches for his vital thing, thus he/she needs to be available that in detail, therefore that thing is maintained over here.

Monday, November 26, 2012 2:43 PM by Hannah

# re: PowerShell Script with ASP.NET and Console Application

I truly love your site.. Pleasant colors &

theme. Did you create this site yourself? Please reply back as I'm wanting to create my very own blog and would love to find out where you got this from or exactly what the theme is named. Many thanks!

Thursday, March 14, 2013 9:44 PM by Flanders

# re: PowerShell Script with ASP.NET and Console Application

You can certainly see your enthusiasm within the work you write.

The sector hopes for more passionate writers like you who aren't afraid to say how they believe. Always follow your heart.

Tuesday, April 16, 2013 12:06 AM by Samples