私は Powershell を初めて使用し、関数内から [ref] 変数の値を出力する方法を見つけようとしています。
ここに私のテストコードがあります:
function testref([ref]$obj1) {
$obj1.value = $obj1.value + 5
write-host "the new value is $obj1"
$obj1 | get-member
}
$foo = 0
"foo starts with $foo"
testref([ref]$foo)
"foo ends with $foo"
このテストから得られる出力は次のとおりです。$obj1 の値が期待どおりに取得されていないことに気付くでしょう。また、write-host への呼び出しで $obj1.value を渡そうとしましたが、同じ応答が生成されました。
PS > .\testref.ps1
foo starts with 0
the new value is System.Management.Automation.PSReference
TypeName: System.Management.Automation.PSReference
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
Value Property System.Object Value {get;set;}
foo ends with 5