0

I am searching for a way on how to run a powershell script in C# in memory using the newest powershell engine found on the system. Currently I use the Microsoft Windows PowerShell Engine Core Assembly (System.Management.Automation) which works perfect for powershell scripts using the powershell version 2.0 engine.

My goal is the create a .NET 2.0 or 3.5 assembly which calls powershell scripts in memory using the newest powershell engine found on the system. This could be version 2 or 3.

These are my prerequisites:

  • My assembly should be targeted to .NET 2.0 or .NET 3.5
  • Always use the newest version of the powershell engine
  • Run script in memory (it is a no-go to store the PS script temporarily on the hard disc)

Any ideas?

3
  • Why do you need to use the latest PowerShell? Do you simply need to be able to run on the latest PowerShell, or do you want to target new features in the latest version? Commented Feb 24, 2014 at 14:37
  • When powershell v3 is installed on the machine, the script uses powershell 3 features. Commented Feb 24, 2014 at 15:15
  • 1
    That will be trickier as the C# code that uses newer 3.0 features will need to be compiled against the v3 version of System.Management.Automation.dll. I would compile the V1/2 stuff in one class library that reference SMA v1/2 and the V3/V4 stuff in another class library that references SMA v3. Then at runtime determine which is on the system use Assembly.LoadFrom to load the appropriate class library. You will also have the issue running the correct .NET runtime. That might require a small EXE stub to do this determination and then fire off one of two exes (one for .NET 2.0 and one for 4.0) Commented Feb 24, 2014 at 16:09

2 Answers 2

1

Create a RAM disk and store the script file there. Alternatively, if you don't want to store any script file at all, then use the AddCommand() method to add individual commands to the PowerShell object.

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

Comments

1

You can embed a script as an "embedded resource" inside a C# program and then extract it at runtime. Take a look at this script lines 149-155 on how to do that. Targeting .NET 2.0 thru 3.5 is easy since 2.0, 3.0 and 3.5 all run on the .NET 2.0 runtime - just target .NET 2.0.

However if you want to always target the latest version of the PowerShell engine that is going to be trickier because PowerShell V3 and V4 target .NET 4.0. If your assembly always targets .NET 2.0 (or even 3.5) you won't be able to use new features in the latest PowerShell engine because you would have to reference the V3 System.Management.Automation.dll assembly during compile and it requires .NET 4.0.

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.