1
$ConfirmPreference
などのグローバル変数の値を、C#で書かれたバイナリPowershellモジュール内のPowershellホストから取得するにはどうすればよいですか?バイナリPowershellモジュール - Powershellホストからグローバル変数の値を取得
$ConfirmPreference
などのグローバル変数の値を、C#で書かれたバイナリPowershellモジュール内のPowershellホストから取得するにはどうすればよいですか?バイナリPowershellモジュール - Powershellホストからグローバル変数の値を取得
PSCmdlet.GetVariableValue(string)
方法がこのために使用することができる。PowerShell内
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);
}
}
}
テスト:同じ `PSHost`が複数の` Runspace`を有することができる
PS > Test-GetVariableValueMethod
High
PS > $ConfirmPreference = 'Low'
PS > Test-GetVariableValueMethod
Low
グローバル変数は、 'Runspace'に特異的ですs。 – PetSerAl