Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 514 Vote(s) - 3.43 Average
  • 1
  • 2
  • 3
  • 4
  • 5
powershell: how to write-host value from [ref] variable

#1

I'm new to Powershell and I'm trying to work out how to print the value of a [ref] variable from within a function.

Here is my test code:



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"


The output I get from this test is as follows. You'll notice that I don't get the value of $obj1 as I was hoping. I also tried passing in $obj1.value in the call to write-host but that generated the same response.

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

Reply

#2
You would have probably tried:

write-host "the new value is $obj1.value"

and got corresponding output of

the new value is System.Management.Automation.PSReference.value

I think you did not notice the `.value` in the end of the output.

In strings you have to do something like this while accessing properties:

write-host "the new value is $($obj1.value)"

Or use string format, like this:

write-host ("the new value is {0}" -f $obj1.value)

Or assign value outside like `$value = $obj1.value` and use in string.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through