1

I need to instantiate a new object and pass it to a command input like this:

using (var pipeline = runspace.CreatePipeline())
{
    // Create configuration object
    pipeline.Commands.AddScript(
        "$eap = New-EapConfiguration -UseWinlogonCredential");

    var cmd = new Command("Add-VpnConnection");
    // Set connection settings...
    cmd.Parameters.Add(new CommandParameter(
        "EapConfigXmlStream", "$eap.EapConfigXmlStream"));

    pipeline.Commands.Add(cmd);
    pipeline.Invoke();
}

Result:

Unable to convert "$eap.EapConfigXmlStream" in "System.Xml.XmlDocument" object type...

$eap.EapConfigXmlStream is interpreted as string, not as object reference

Any suggestion will be welcome


Edit:

@JohnB: This PS command works:

Set-VpnConnection -Name "MyVpnConnection"
    -EapConfigXmlStream (New-EapConfiguration -UseWinlogonCredential).EapConfigXmlStream

But this C# replacement does not works:

cmd.Parameters.Add(new CommandParameter("EapConfigXmlStream",
    "(New-EapConfiguration -UseWinlogonCredential).EapConfigXmlStream"));

(New-EapConfiguration -UseWinlogonCredential).EapConfigXmlStream interpreted as string

4
  • i have a feeling that the EapConfigXmlStream param is expecting an XmlDocument - at least that is what I seem to understand from reading learn.microsoft.com/en-us/powershell/module/vpnclient/… Commented Aug 8, 2019 at 7:36
  • 1
    @JohnB: See my edit Commented Aug 8, 2019 at 7:47
  • tried to prefix with [ref]? Commented Aug 8, 2019 at 8:04
  • @FalcoAlexander: Same error, interpreted as string Commented Aug 8, 2019 at 8:19

1 Answer 1

1

it should work like this. CommandParameter(String, Object) expects an object, but you pass a string "EapConfigXmlStream", "$eap.EapConfigXmlStream"

Pass a PSObject instead:

    Runspace runspace = RunspaceFactory.CreateRunspace();
    runspace.Open();
    Collection<PSObject> configXML;

    using (var pipeline = runspace.CreatePipeline())
    {
        pipeline.Commands.AddScript(
            "(New-EapConfiguration -UseWinlogonCredential).EapConfigXmlStream");
        configXML = pipeline.Invoke();
    }

    using (var pipeline = runspace.CreatePipeline())
    {
        var cmd = new Command("Add-VpnConnection");
        cmd.Parameters.Add(new CommandParameter(
            "EapConfigXmlStream", configXML[0]));
        pipeline.Commands.Add(cmd);
        pipeline.Invoke();
    }
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much for your help. I am trying it... Is it possible to keep the same pipeline and do not export object in C# context and import again in PS context ? Just create an object reference and use it (always stay in PS context)
not so sure, I did a quick test and it won't let me Invoke the pipeline twice. There will probably be a more elegant way.
Your solution works perfectly. I exported New-EapConfiguration instead and use cmd.Parameters.Add("EapConfigXmlStream", config.Properties["EapConfigXmlStream"].Value);

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.