Question

I have a number of cmdlets written. The one I'm currently working on I'd like to call my other cmdlets. I'm attempting this like so:

//Calling cmdlet
protected override void ProcessRecord()
{
    Cmdlet1 _cmdlet1 = new Cmdlet1();
    _cmdlet1.configFilePath = this.configFilePath; //set a few parameters the cmdlet will need
    _cmdlet1.useConfigFile = true; //and one more
    _cmdlet1.Invoke();

    Cmdlet2 _cmdlet2 = new Cmdlet2();
    _cmdlet2.configFilePath = this.configFilePath; //set a few parameters again
    _cmdlet2.useConfigFile = true; //one more
    _cmdlet2.Invoke();
}

However, when I run "Calling cmdlet" from powershell, nothing happens. There are no errors, none of the code I've written in my other cmdlets is run. There's a number of WriteObject calls in Cmdlet1 and Cmdlet2, shouldn't I be able to see those if the cmdlets are actually being run? Though I should mention that that's obviously not the only thing I'm checking to verify that they're being successfully called.

Was it helpful?

Solution

The objects you are writing with WriteObject are being returned via the Invoke() method call. Iterate over the results of that call to get the individual objects from the cmdlet invocation e.g.:

foreach (var result in _cmdlet1.Invoke())
{
    ...
}

See this MSDN topic for more details.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top