3

Import-Module command work fine with powershell windows console but same command doesn't work on c# api. i'm using this project for execute powershell script: http://www.codeproject.com/Articles/18229/How-to-run-PowerShell-scripts-from-C

it execute many of them commands but it doesn't execute "Import-Module 'c:\vm\vm.psd1'" command. i try import microsoft modules but it doesn't work too. How can i execute "Import-Module" command with c# api?

Also add-pssnapin 'virtualmachinemanager' doesn't work too.

2 Answers 2

1

Try load module in this way:

PowerShell powershell = PowerShell.Create();
powerShell.Commands.AddCommand("Import-Module").AddParameter("Name", "c:\vm\vm.psd1'");

or

PowerShell powershell = PowerShell.Create();
powershell.Commands.AddCommand("Add-PsSnapIn").AddParameter("Name", "virtualmachinemanager");

With a pipeline try create an InitialSessionState

InitialSessionState iss = InitialSessionState.CreateDefault();
           iss.ImportPSModule(new string[] { @"C:\vm\vm.psd1"});
           Runspace runSpace = RunspaceFactory.CreateRunspace(iss);
           runSpace.Open();

then use your code with pipeline to run cmdlet from module loaded

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

7 Comments

pipeline.Commands.Add return void. i use such as Command cmd = new Command("Import-Module"); cmd.Parameters.Add(new CommandParameter("Name", @"C:\Program Files\Microsoft System Center 2012\Virtual Machine Manager\bin\psModules\virtualmachinemanager\virtualmachinemanager.psd1")); pipeline.Commands.Add(cmd); but it doesn't work too.
@Baris I'm sorry, it works on Powershell Class.. not in a pipeline object. Add other solution.
thanks for answer but how do i use PowerShell class? i use only pipeline for execute commands. i try use PowerShell class such as before run script: PowerShell ps = PowerShell.Create(); ps.Commands.AddCommand("Import-Module") .AddParameter("Name", @"C:\Program Files\Microsoft System Center 2012\Virtual Machine Manager\bin\psModules\virtualmachinemanager\virtualmachinemanager.psd1"); ps.Invoke(); but it doesn't work. how do i use it?
@Baris have you tried with InitialSessionState and pipeline as in my last example in answer? Should works!
Thanks i tired InitialSessionState but it doesn't work too. maybe i will should re-install system center virtual machine manager.
|
1

Try something like this for loading the snapin and executing your commands:

using System.Management.Automation.Runspaces;

//...

var rsConfig = RunspaceConfiguration.Create();
using (var myRunSpace = RunspaceFactory.CreateRunspace(rsConfig))
{
    PSSnapInException snapInException = null;
    var info = rsConfig.AddPSSnapIn("FULL.SNAPIN.NAME.HERE", out snapInException);

    myRunSpace.Open();
    using (var pipeLine = myRunSpace.CreatePipeline())
    {
        Command cmd = new Command("YOURCOMMAND");
        cmd.Parameters.Add("PARAM1", param1);
        cmd.Parameters.Add("PARAM2", param2);
        cmd.Parameters.Add("PARAM3", param3);

        pipeLine.Commands.Add(cmd);
        pipeLine.Invoke();
        if (pipeLine.Error != null && pipeLine.Error.Count > 0)
        {
            //check error
        }
    }
}

1 Comment

hi, thanks for answer. i try add snapin but i didn't add snapin anywise.

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.