我写了很多cmdlet。我目前正在做的一个,我想调用我的其他cmdlet。我尝试这样做:
//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();
}但是,当我从powershell运行“调用cmdlet”时,什么也没有发生。没有错误,我在其他cmdlet中编写的代码都没有运行。在Cmdlet1和Cmdlet2中有许多WriteObject调用,如果cmdlet实际正在运行,我应该可以看到这些调用吗?尽管我应该提到,这显然不是我唯一检查它们是否被成功调用的东西。
发布于 2013-06-11 23:42:23
您使用WriteObject编写的对象将通过Invoke()方法调用返回。迭代该调用的结果以从cmdlet调用中获取各个对象,例如:
foreach (var result in _cmdlet1.Invoke())
{
...
}有关更多详细信息,请参阅此MSDN topic。
https://stackoverflow.com/questions/17045204
复制相似问题