0

I am really not asking for any specific lines of code here, but more along the lines of someone being able to explain to me the idea of it to help me have a better understanding. I am very new to coding with PowerShell scripts let alone calling them in a C# app. Would anyone be able to explain or even point me in the right direction so I can learn about what I would need to do here. I have a basic "Hello World" script that I would like to call from a C# Windows Service using VS2010. I have seen examples around the internet, but they are very brief and don't really teach the concept behind it.

Any help would be much appreciated! Thanks!

2
  • have you checked this: stackoverflow.com/questions/527513/… Commented Jun 29, 2013 at 0:59
  • I have seen it, but I honestly am not really sure how to incorporate my personal script into it. Commented Jun 30, 2013 at 1:58

2 Answers 2

1

Here is a pretty good discussion on how to call Powershell from C#: http://www.codeproject.com/Articles/18229/How-to-run-PowerShell-scripts-from-C

I suggest you start with the RunScript function and add it to a VS2010 console application. Main() would then invoke RunScript something like RunScript( "echo 'hello from PS';get-date; get-culture; ").

Windows services have quite a few differences from a normal console application. You can read about how to write a windows service on MSDN, but if you've never coded before, you have a steep learning curve in front of you.

There is a service from an old Resource Kit call srvany.exe (Google it) which would run any console app as a service. However in Windows Vista (and above) services were restricted from accessing the desktop, so srvany could only be successful in Vista and above if you EXE doesn't need keyboard input or need to write to the display. However your EXE could read/write files.

Sign up to request clarification or add additional context in comments.

3 Comments

I have seen that article before. But again, I don't really see how I would be able to incorporate my personal script into it. Like to me, it would make sense to be able to call my particular script from the file location. But I could also be wrong...
I feel like I would have to do something similar to this, but I am still trying to figure out how I would incorporate it into an actual windows service. stackoverflow.com/questions/10260597/…
You could replace "echo 'hello from PS';get-date; get-culture; " with you personal script. If your script is in a file, for example: c:\my\script.ps1, then you would replace that string with "c:\my\script.ps1". If your personal script is to copy c:\my\fromfile.txt to c:\my\tofile.txt, then you'd replace it with: "copy c:\my\fromfile.txt c:\my\tofile.txt" and so on. How to write a full Windows service is well beyond the scope of stackoverflow.com. Instead it would be a lot simpler executing your script via AT.EXE (google it) or Task Scheduler (control panel then administrative tools)
1

This is what I was able to get to work for me.

I basically followed the information that I found from this post, but changed a couple of things to get it to work for me: Problem with calling a powershell function from c#

For example, for the ps.AddCommand("BatAvg") I just added my own function in my .ps1 file ps.AddCommand("PS1FunctionName") and didn't add any parameters since it was not necessary. The answer was staring me in the face the whole time, I just didn't know where I needed to place it in my windows service. I was able to figure that out and now I am cruising!

Also, don't forget to reference using System.Management.Automation and using System.Management.Automation.Runspaces in your code (for all the future people that may look at this).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.