locked
Powershell timestamp RRS feed

  • Question

  • Hi,

    I am trying to add a timestamp as an additional column to the below command:

    Get-ClusterGroup | ? {$_.GroupType -eq 'VirtualMachine'} | Out-File -Append c:\test.txt

    So far I can only get the timestamp at the beginning of each output:

    function Get-Timestamp {
        return "[{0:MM/dd/yy} {0:HH:mm:ss}]" -f (Get-Date)
    }
    
    filter timestamp {"$(Get-Date -Format G): $_"}
    $result = Get-ClusterGroup | ? {$_.GroupType -eq 'VirtualMachine'}
    $result2 = $(Get-TimeStamp) |
    Write-Output $result2 $result  |Out-File -Append c:\"cluster_audit"\test.txt

    What I would like is 

    Name   OwnerNode   State   Timestamp

    ---------------------------------------------

    Can anyone suggest anything?

    Thanks in advance,

    Matt

    

    Thursday, January 31, 2019 10:31 AM

Answers

  • You can use a calculated property to add an attribute the the output
    Get-ClusterGroup | 
        Where-Object {$_.GroupType -eq 'VirtualMachine'} |
            Select-Object -Property Name,OwnerNode,State,@{Name='TimeStamp';Expression={$(Get-Date -Format 'MM/dd/yy HH:mm:ss')}}



    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''


    • Edited by BOfH-666 Thursday, January 31, 2019 10:37 AM
    • Proposed as answer by Proed Thursday, January 31, 2019 10:45 AM
    • Marked as answer by Mattps2 Thursday, January 31, 2019 12:38 PM
    Thursday, January 31, 2019 10:36 AM

All replies

  • You can use a calculated property to add an attribute the the output
    Get-ClusterGroup | 
        Where-Object {$_.GroupType -eq 'VirtualMachine'} |
            Select-Object -Property Name,OwnerNode,State,@{Name='TimeStamp';Expression={$(Get-Date -Format 'MM/dd/yy HH:mm:ss')}}



    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''


    • Edited by BOfH-666 Thursday, January 31, 2019 10:37 AM
    • Proposed as answer by Proed Thursday, January 31, 2019 10:45 AM
    • Marked as answer by Mattps2 Thursday, January 31, 2019 12:38 PM
    Thursday, January 31, 2019 10:36 AM
  • you can also create a PowerShell custom object to add the info together. 

    see https://stackoverflow.com/questions/43893931/how-to-create-pscustomobject-with-properties-and-methods 

    $Obj = [pscustomobject]@{
        A
    = @(5,6,7)
        B
    = 9
       
    Len_A = {return $this.A.count;}
       
    Sum_A = {return (SumOf $this.A);}
    }


    Klaus

    Thursday, January 31, 2019 11:45 AM