none
Create an event in powershell to run when a variable is changed RRS feed

  • Question

  • Evening Everyone,

    I have been searching the web to see if its possible to determine when a variable changes and if a changes is made invoke a function. I have an xml file i load in my profile that other functions can update. I want to save the changes to that xml when the variables changed without having to add it to every function i write. Is this possible? I have been reading over Register-ObjectEvent but im having no luck with getting it to work, and all the examples i see are for objects that already have events. The variable i created is [XML].

    Friday, May 27, 2016 11:29 PM

Answers

  • I figured this out, XML has an event for when a node is changed. below is the example i found.

    $x=[XML]”<a><b>TEST</b></a>”
    
    $x.Add_NodeChanged({ write-host “NODE CHANGED“ -ForegroundColor Cyan})
    $x.a.b  = “New Value”

    • Marked as answer by Zach Shupp Friday, May 27, 2016 11:49 PM
    Friday, May 27, 2016 11:49 PM

All replies

  • I figured this out, XML has an event for when a node is changed. below is the example i found.

    $x=[XML]”<a><b>TEST</b></a>”
    
    $x.Add_NodeChanged({ write-host “NODE CHANGED“ -ForegroundColor Cyan})
    $x.a.b  = “New Value”

    • Marked as answer by Zach Shupp Friday, May 27, 2016 11:49 PM
    Friday, May 27, 2016 11:49 PM
  • Nice find.  Should be useful.

    \_(ツ)_/

    Saturday, May 28, 2016 12:00 AM
  • This is great finding. I had a use case some days back. I was changing a value in web config file , and to verify it i was reading it again.

    This should help me.

    Saturday, May 28, 2016 2:12 AM