Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何创建powershell cmdlet(C#)输出参数_C#_Powershell - Fatal编程技术网

如何创建powershell cmdlet(C#)输出参数

如何创建powershell cmdlet(C#)输出参数,c#,powershell,C#,Powershell,常见参数有几个例子,如ErrorVariable、InformationVariable get-item foad: -ev foo $foo “get item”将创建$foo并将其值设置为ErrorRecord的实例。 如何创建自己的参数来执行类似的操作 基本上,我正在创建一个cmdlet,该cmdlet使用WriteObject()将数据写入管道,但我还有一些额外的信息,我希望允许用户访问这些信息(基本上是带外数据),这些信息不是管道的一部分 C#中的out参数示例: 我正在寻找如何将

常见参数有几个例子,如ErrorVariable、InformationVariable

get-item foad: -ev foo
$foo
“get item”将创建$foo并将其值设置为ErrorRecord的实例。 如何创建自己的参数来执行类似的操作

基本上,我正在创建一个cmdlet,该cmdlet使用WriteObject()将数据写入管道,但我还有一些额外的信息,我希望允许用户访问这些信息(基本上是带外数据),这些信息不是管道的一部分

C#中的out参数示例:

我正在寻找如何将“GetMyStuff()”转换为powershell cmdlet

[Cmdlet(VerbsCommon.Get, "MyStuff")]
public class ExampleCmdLet : PSCmdlet
{
    [Parameter(Mandatory = false)] int param1;
    [Parameter(Mandatory = false)] string someVar; // How to make this [out] ?
    protected override void ProcessRecord()            
    {
        someVar = "My out-of-band result";
        WriteObject(param1 + 1);
    }
}
您希望设置的是PowerShell变量,而不是.NET变量

访问PowerShell变量需要通过调用方的会话状态访问它们

在派生cmdlet中,您可以通过
this.SessionState.PSVariable.set(,)
设置变量:

上述收益率:

667  # Output from the cmdlet, via WriteObject()
42   # Value of $targetVariable, set by Get-MyStuff
# Compile a Get-MyStuff cmdlet and import it into the current session.
Add-Type -TypeDefinition @'
using System.Management.Automation;

[Cmdlet(VerbsCommon.Get, "MyStuff")]
public class ExampleCmdLet : PSCmdlet
{
    [Parameter()] public int Param1 { get; set; }
    [Parameter()] public string SomeVar { get; set; }

    protected override void ProcessRecord()
    {

        // Assign to the $SomeVar variable in the caller's context.
        this.SessionState.PSVariable.Set(SomeVar, 42);

        WriteObject(Param1 + 1);
    }

}
'@ -PassThru | % Assembly | Import-Module                                                                           #'

# Call Get-MyStuff and pass the *name* of the 
# variable to assign to, "targetVariable", which sets
# $targetVariable:
Get-MyStuff -Param1 666 -SomeVar targetVariable
# Output the value of $targetVariable
$targetVariable
667  # Output from the cmdlet, via WriteObject()
42   # Value of $targetVariable, set by Get-MyStuff