0

All,

I'm trying to embed a powershell file into a c# program and then want to be able to access it as a ps1 file to either run it using process.start or using a pipeline.

The powershell is fairly complicated and reading it as a string etc.. doesn't seem to work as viable option.. :(

I usually execute the script in powershell using

.\svccontroller.ps1 -ComputerName localhost -ServiceName SQL* -Action Start -i

In c# , i want to be able to do the same thing without hard coding a path... Even ignoring the arguments, if i just want to run .\svccontroller.ps1 , is it possible ?

I want the file to be embedded in the build and accessible as a ps1 file

Is this possible? Am i missing something basic?

1
  • 1
    Is there really no better way of doing...whatever it is? I can't think of much of anything that really requires calling an external script. Commented Feb 19, 2014 at 20:14

2 Answers 2

3

I have written a script that creates an EXE that has an embedded script. The EXE unpacks the script and executes. Take a look at the source for ideas on how to extract the script (lines 149-155). Running the script, once it is extracted, is very easy using the System.Management.Automation.PowerShell class.

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

Comments

0

Is this possible? Am i missing something basic?

Yes. It could be personal preference, but I prefer to keep things simple. That being said, the best way to go about this is to use either powershell or c#, and not mix the two. You didn't supply particulars, but know that powershell is essentially a shell for .NET, so everything you can do in powershell you can do in C#. Yes, it may require some changes, but you will thank yourself for years to come.

3 Comments

There are good reasons for mixing C# and PowerShell. The question here wasn't if it was a good idea or not, though, it was about how to embed the script as a resource and access it in code.
@MikeShepard On the one hand you are right, but to me the OPs question also kind of begs the question of the XY problem meta.stackexchange.com/questions/66377/what-is-the-xy-problem
I guess it's the XY problem if you consider using C# and PowerShell together to be a problem. I don't.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.