Reputation:
I have made a PowerShell script, which is running perfectly fine and generating a text file when I run it standalone. I wanted to automate that whenever my ASP.NET page loads I invoke a process from C# that calls my PowerShell script and executes that leading to a text file being generated.
The problem is the script is being called, but not excuted. Giving some error about permissions, etc.
Upvotes: 3
Views: 7932
Reputation: 546
I would leverage the System.Management.Automation namespace and create a PowerShell console object. You can pass commands or .ps1 files - both with arguments - to it for execution. Much cleaner than a separate shell that will have to then call the powershell.exe.
Other things to ensure are appropriate are the identity of the application pool. Ensure it has the level of rights required to perform the PowerShell commands and/or scripts. Next, make sure you have you Set-ExecutionPolicy appropriately if you're going to execute script files.
Here's the code executing commands submitted by a TextBox web form as if it were a a PowerShell console using these objects - should illustrate the approach:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Management.Automation;
using System.Text;
namespace PowerShellExecution
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ExecuteCode_Click(object sender, EventArgs e)
{
// Clean the Result TextBox
ResultBox.Text = string.Empty;
// Initialize PowerShell engine
var shell = PowerShell.Create();
// Add the script to the PowerShell object
shell.Commands.AddScript(Input.Text);
// Execute the script
var results = shell.Invoke();
// display results, with BaseObject converted to string
// Note : use |out-string for console-like output
if (results.Count > 0)
{
// We use a string builder ton create our result text
var builder = new StringBuilder();
foreach (var psObject in results)
{
// Convert the Base Object to a string and append it to the string builder.
// Add \r\n for line breaks
builder.Append(psObject.BaseObject.ToString() + "\r\n");
}
// Encode the string in HTML (prevent security issue with 'dangerous' caracters like < >
ResultBox.Text = Server.HtmlEncode(builder.ToString());
}
}
}
}
To do this when a page loads, you'll want to create a function that meets your needs and call it from the "Page_Load" function.
Here's is a write up for you that covers how to create a page from start to finish with Visual Studio and get this done, http://grokgarble.com/blog/?p=142.
Please up-vote and mark appropriately if helpful.
Thanks, Jeff
Upvotes: 0
Reputation: 15011
Another option you should look into is running the script within your code directly, which should eliminate the need for the text file all together.
http://msdn.microsoft.com/en-us/library/ms714661(VS.85).aspx
http://devcentral.f5.com/weblogs/Joe/archive/2009/01/16/powershell-abcs---r-is-for-runspace.aspx
http://bartdesmet.net/blogs/bart/archive/2007/02/05/a-first-introduction-to-windows-powershell-runspaces.aspx (see "Hosting a Runspace" at the end)
Upvotes: 1
Reputation: 39510
Depending on how you're running your ASP.NET script (VS development server, or IIS), then you'll need to take account of the fact that the 'user' who's running the script is probably not you, and may not have the same access to the same things as you do.
You may well need to get further into it than "some error about permission etc", because that error is probably the very essence of your problem.
You may find that ProcMon (from www.sysinternals.com) is a useful tool for finding what the permissions problem is.
Upvotes: 4
Reputation: 59
To Run powershell using C# program, you could use Process.Start() with "C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe", and provide appropriate powershell file argument.
For more information, please run
C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -?
Regarding permission issue, please read Running Scripts From Within Windows PowerShell, and make sure powershell has a correct ExecutionPolicy.
Further Information: http://www.microsoft.com/technet/scriptcenter/resources/qanda/sept06/hey0926.mspx
Upvotes: 5