I have the following variable that is used in many of my scripts for logging:
$uDate = get-date -format "ddd MM/dd/yyyy HH:mm:ss"
Only problem is that the date is never refreshed and only shows the date/time when the variable was declared.
This should not be done with a variable. Variables should store data, not take actions. So the right way to do this is to create a function that returns the current date in the format you want.
But... if you want to get really hack-tastic you can do this by setting a breakpoint on all reads to your variable. The -Action
of the breakpoint will reset the value of the variable to the current time.
$rightNow = Get-Date
$null =
Set-PSBreakpoint -Variable rightNow -Mode Read -Action {
Set-Variable -Scope 1 -Name rightNow -Value (Get-Date)
}
Testing...
PS > $rightnow
Monday, August 20, 2012 11:46:04 AM
PS > $rightnow
Monday, August 20, 2012 11:46:09 AM