Sono nuovo di Powershell e sto cercando di capire come stampare il valore di una variabile [ref] all'interno di una funzione.powershell: come scrivere il valore dell'host dalla variabile [ref]
Ecco il mio codice di prova:
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"
L'uscita che ricevo da questo test è la seguente. Noterai che non ottengo il valore di $ obj1 come speravo. Ho anche provato a passare $ obj1.value nella chiamata a scrivere-host ma questo ha generato la stessa risposta.
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
Sì, era così. Grazie! – Denis
@Denis - Accetta come risposta se risolve il tuo problema! – manojlds
Come faccio? – Denis