Quantcast
Channel: Hey, Scripting Guy! Blog
Viewing all articles
Browse latest Browse all 3333

PowerTip: Create Self-Updating Variable

$
0
0

Summary: Learn how to create a self-updating variable.

Hey, Scripting Guy! Question How can I create a variable that will automatically update when I call it?

Hey, Scripting Guy! Answer Use Set-PSBreakPoint to set a variable that will always have the current date and time (or only time):

Set-PSBreakpoint -Variable Time -Mode Read -Action {$Global:Time=(Get-Date).ToShortTimeString();$Global:Time}

$Time

Set-PSBreakpoint -Variable Date -Mode Read -Action {$Global:Date=Get-Date;$Global:Date}

$Date

Note  You need to call the variable to kick off the PSBreakPoint action script block the first time.
           After that, the information will be up-to-date.

PS C:\> $Date

Wednesday, October 29, 2014 10:01:28 PM

PS C:\> $Time

10:01 PM


Viewing all articles
Browse latest Browse all 3333

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>