C# PowerShell-从对象方法内写入进度
我有一个自定义的C#PowerShell Cmdlet,它是我编写的,用于输出对象C# PowerShell-从对象方法内写入进度,c#,powershell,C#,Powershell,我有一个自定义的C#PowerShell Cmdlet,它是我编写的,用于输出对象 [Cmdlet(VerbsCommon.Get, "CustomObj")] public class CustomObjGet : Cmdlet { protected override void ProcessRecord() { var instance = CustomObj.Get(); WriteObject(instance); } } 用法
[Cmdlet(VerbsCommon.Get, "CustomObj")]
public class CustomObjGet : Cmdlet
{
protected override void ProcessRecord()
{
var instance = CustomObj.Get();
WriteObject(instance);
}
}
用法:
$output = Get-CustomObj
$output.RestartServices()
返回的对象有一个方法:
public class CustomObj
{
public string Name { get; set; }
public static CustomObj Get()
{
var instance = new CustomObj() { Name = "Testing" };
return instance;
}
public void RestartServices ()
{
// Want to WriteProgress here...
}
}
用法:
$output = Get-CustomObj
$output.RestartServices()
目前,该方法无法像您在Cmdlet本身的ProcessRecord()方法中那样访问Cmdlet WriteProgress函数
我想从该方法中执行PowerShell WriteProgress。有什么办法吗?对不起,误读了这个问题。这似乎在我有限的测试中起作用:
public void RestartServices()
{
//Write
// Want to WriteProgress here...
for (int i = 0; i <= 100; i += 10)
{
Console.WriteLine("i is " + i);
UpdateProgress(i);
Thread.Sleep(500);
}
}
private void UpdateProgress(int percentComplete)
{
var runspace = Runspace.DefaultRunspace;
var pipeline = runspace.CreateNestedPipeline("Write-Progress -Act foo -status bar -percentComplete " + percentComplete, false);
pipeline.Invoke();
}
public void RestartServices()
{
//写
//想在这里写进度。。。
对于(int i=0;i),他希望从返回对象内部执行进度操作,而不是从Cmdlet内部。此外,WriteProgress位于Cmdlet上,而不是PSCmdlet上。@latkin更正,我希望从返回对象中的方法而不是Cmdlet本身访问它。