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?

techmanc
  • 293
  • 1
  • 7
  • 19
  • 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. – Michael Hampton Feb 19 '14 at 20:14

2 Answers2

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.

Keith Hill
  • 194,368
  • 42
  • 353
  • 369
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.

MDMoore313
  • 3,233
  • 1
  • 23
  • 38
  • 7
    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. – Mike Shepard Feb 19 '14 at 20:34
  • 1
    @MikeShepard On the one hand you are right, but to me the OPs question also kind of begs the question of the XY problem http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem – Xantix Feb 19 '14 at 21:43
  • 2
    I guess it's the XY problem if you consider using C# and PowerShell together to be a problem. I don't. – Mike Shepard Feb 19 '14 at 22:29