How to create a dynamic variable in Powershell, sucha as date/time etc

后端 未结 5 942
太阳男子
太阳男子 2020-12-22 08:20

Hi i am not exactly sure if my wording is right but i need a variable which contains current date/time whenever i write data to log ; how can i do that with

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-22 08:38

    Using PsBreakPoint:

    $act= @'
    $global:now = (get-date).datetime
    '@
    $global:sb = [scriptblock]::Create($act)
    $now = Set-PSBreakpoint -Variable now -Mode Read -Action $global:sb
    

    calling $now returns current updated datetime value

    One liner:

    $now = Set-PSBreakpoint -Variable now -Mode Read -Action { $global:now = (get-date).datetime }
    

提交回复
热议问题