views:

1502

answers:

3

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.

+2  A: 

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

mk
Better link for Process.Start() class: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.start.aspx
eddiegroves
+1  A: 

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.

Will Dean
A: 

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)

JasonMArcher