none
How to make timestamp prefix in the white-host output? RRS feed

  • Question

  • Hi all

    Im novice at ps so i seek your help/advice on following:

    I have write a little script to install bginfo and at every step output to console.

    I want to print the timestamp on every line. Right now it look like this:

    PS C:\Windows\system32> S:\Dev\BgInfo\BgInfo.ps1
    Default config: 'default.bgi'
    Downloading BGInfo.zip to C:\Users\Dev\AppData\Local\Temp
    Extracting BGInfo to: c:\BGinfo
    Copy config: 'default.bgi' to 'c:\BGinfo\' folder
    Executing: c:\BGinfo\Bginfo.exe c:\BGinfo\default.bgi /timer:0 /accepteula /silent
    Time taken: 3 second(s)

    I want something like this:

    PS C:\Windows\system32> S:\Dev\BgInfo\BgInfo.ps1
    22-02-16 08:00:00 # Default config: 'default.bgi'
    22-02-16 08:00:01 # Downloading BGInfo.zip to C:\Users\Dev\AppData\Local\Temp
    22-02-16 08:00:01 # Extracting BGInfo to: c:\BGinfo
    22-02-16 08:00:02 # Copy config: 'default.bgi' to 'c:\BGinfo\' folder
    22-02-16 08:00:03 # Executing: c:\BGinfo\Bginfo.exe c:\BGinfo\default.bgi /timer:0 /accepteula /silent
    22-02-16 08:00:03 # Time taken: 3 second(s)
    How do i get this done?


    IT Adm

    Monday, February 22, 2016 8:13 AM

Answers

  • Hi LnQ,

    you can add a timestamp to a string like this:

    "$(Get-Date -Format "dd-MM-yy HH:mm:ss") Example Protocol Entry"

    Cheers,
    Fred


    There's no place like 127.0.0.1

    • Marked as answer by LongQuo Monday, February 22, 2016 9:35 AM
    Monday, February 22, 2016 8:56 AM

All replies

  • Hi LnQ,

    you can add a timestamp to a string like this:

    "$(Get-Date -Format "dd-MM-yy HH:mm:ss") Example Protocol Entry"

    Cheers,
    Fred


    There's no place like 127.0.0.1

    • Marked as answer by LongQuo Monday, February 22, 2016 9:35 AM
    Monday, February 22, 2016 8:56 AM
  • Thanks Fred,

    I have taken your sample and come up with this function:

    function sMessage
    {
    param([string]$sMessage)
    Write-Output "$(Get-Date -Format "dd-MM-yy HH:mm:ss") # $sMessage"
    }

    to save some typing :D

    sMessage "Hello"

    will output:

    22-02-16 10:34:46 # Hello
    



    #LnQ¯\_(ツ)_/¯

    Monday, February 22, 2016 9:38 AM