二进制Powershell模块 - 从Powershell主机获取全局变量的值

时间:2016-06-24 13:20:33

标签: powershell powershell-module

如何从用C#编写的二进制Powershell模块中的Powershell主机获取全局变量的值,例如$ConfirmPreference

1 个答案:

答案 0 :(得分:2)

可以使用PSCmdlet.GetVariableValue(string)方法:

using System.Management.Automation;

namespace MyModule
{

    [Cmdlet(VerbsDiagnostic.Test, "GetVariableValueMethod")]
    public class TestGetVariableValueMethod : PSCmdlet
    {
        protected override void ProcessRecord()
        {
            ConfirmImpact confirmPref =
                (ConfirmImpact)this.GetVariableValue("global:ConfirmPreference");
            WriteObject(confirmPref);
        }
    }
}

在Powershell中进行测试:

PS > Test-GetVariableValueMethod
High

PS > $ConfirmPreference = 'Low'

PS > Test-GetVariableValueMethod
Low
相关问题