如何返回从C#的PowerShell命令PowerShell中值或对象?

问题描述 投票:2回答:1

我使用的是C#库(.NET 4)与System.Management.Automation DLL文件来实现对PowerShell的(2.0)的一些命令。

我需要在PowerShell脚本中获取某个命令的值。

[Cmdlet(VerbsCommon.Get, "MyCommand")]
public class GetMyCommand : Cmdlet
{
    protected override void ProcessRecord()
    {
        // How do I return a value to PowerShell?
    }
}

我想获得一个价值的方式是这样的:

$value = Get-MyCommand
powershell powershell-v2.0 powershell-module
1个回答
2
投票

您必须使用CmdLet.WriteObject方法。

Here is从@RomanKuzmin一个很好的解释。

© www.soinside.com 2019 - 2024. All rights reserved.