0

I have some powershell scripts that I want to call from C#.

From the commandline I would do this:

Import-Module c:\provisioning\newModule.psm1 –Force
Get-MTMailbox -customerID custid0001

This works and gives the correct result.

I want to call my scripts from C#:

InitialSessionState initial = InitialSessionState.CreateDefault();
initial.ImportPSModule(new string[] { "C:\\provisioning\\newModule.psm1" });
RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
Runspace runspace = RunspaceFactory.CreateRunspace(initial);
runspace.Open();

PowerShell ps = PowerShell.Create();
ps.Runspace = runspace;
ps.AddCommand("Get-MTMailbox").AddArgument("-customerID custid0001");
// Also tried this. Still ignored.
//ps.AddParameter("-customerID", "custid0001");
ps.Invoke();
Collection<PSObject> results = ps.Invoke();
foreach (PSObject psObject in results)
{
    Console.Write("Result: "+psObject.ToString());
    if (psObject.BaseObject is System.Collections.Hashtable)
    {
        Hashtable ht = (Hashtable) psObject.BaseObject;
        foreach (DictionaryEntry keypair in ht)
        {
            Console.WriteLine(keypair.Key+" "+keypair.Value); 
        }

    }
}

The command runs, but my arguments are always ignored.

I'm very new to C# so maybe I don't know what to search for.

What's the correct way to pass arguments to a cmdlet in C#?

Megasaur
  • 626
  • 8
  • 20

1 Answers1

0

try with this:

ps.AddParameter("customerID", "custid0001");

You do not need the - to specify the argument name

CB.
  • 58,865
  • 9
  • 159
  • 159