Question:
Some Facts:
When you asign an object to a variable called $a
and then one of it’s propertys changes, the variable $a
gets updated.
But when I asign the value of an object’s property $object.property
(instead of the object itself) to the variable called $b
and then $object.property
changes, $b
doesn’t get updated. That means, the current value is stored in $object.property
, but $b
stays the way it is.
An example:
I asign an Window
object to a variable called $bochsWindow
. Then some propertys change because I move the window. But when I print out $bochsWindow
, you can see that it’s up to date – that means, all new values of the object’s propertys are also stored in $bochsWindow
.
But if try to store a property of $bochsWindow
in a variable called $posX
and then the property changes, $posX
doesn’t change.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
PS .> $bochsWindow = (GetProcess bochs | Get-Window) PS .> $bochsWindow ProcessId : 1536 ProcessName : bochs Position : {X=54,Y=32,Width=650,Height=576} IsMinimized : False IsMaximized : False WindowHandle : 3933134 Caption : Bochs for Windows - Display [[Moving Boch's Window By Hand]] PS .> $bochsWindow ProcessId : 1536 ProcessName : bochs Position : {X=0,Y=0,Width=650,Height=576} IsMinimized : False IsMaximized : False WindowHandle : 3933134 Caption : Bochs for Windows - Display PS .> (Get-Window -ProcessName bochs) ProcessId : 1536 ProcessName : bochs Position : {X=0,Y=0,Width=650,Height=576} IsMinimized : False IsMaximized : False WindowHandle : 3933134 Caption : Bochs for Windows - Display PS .> $posX = $bochsWindow.Position.X PS .> $posX 302 [[Moving Boch's Window By Hand]] PS .> $posX 302 PS .> $bochsWindow.Position.X 472 PS .> |
But what should I do if I want $posX
to stay up to date and always store the new value (472
) instead of 302
My Question:
I want to store a reference the an object’s property in a variable. That means, I want the variable to get updated every time the object’s property changes. How can I do this? Thanks.
Answer:
Not trivial way using PSBreakpoint
, but it’s the only that I know:
1 2 3 4 5 6 7 |
$global:bochsWindow = (GetProcess bochs | Get-Window) $act= @' $global:b = $bochsWindow.Position.X '@ $global:sb = [scriptblock]::Create($act) $global:b = Set-PSBreakpoint -Variable b -Mode Read -Action $global:sb |
In this way $b
is always updated when called.