New-TiedVariable by Joel Bennett 23 months ago
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/2750"></script>download | new post
A function for creating tied variables using Robert Robelo’s idea to create breakpoints that update the variable values.
- function New-TiedVariable {
- #.Synopsis
- # Creates a ReadOnly variable that recalculates it's value each time it's read
- #.Description
- # Create a variable tied to a scriptblock by using a breakpoint ;-)
- #.Notes
- # New-TiedVariable uses breakpoints to trigger the automatic recalculation of the variables, so the use of a command like Get-PSBreakPoint | Remove-PSBreakPoint will break any variables created using New-TiedVariable
- param(
- # The name of the variable to create
- [String]$Name,
- # The scriptblock to use to calculate the value each time
- [ScriptBlock]$Value
- )
- Set-Variable $Name -Value (.$Value) -Option ReadOnly, AllScope -Scope Global -Force
- $null = Set-PSBreakpoint -Variable $Name -Mode Read -Action {
- Set-Variable $Name (. $Value) -Option ReadOnly, AllScope -Scope Global -Force
- }.GetNewClosure()
- }
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.
PowerShell Code Repository