locked
How to add computer name column into the scripts RRS feed

  • Question

  • How to add computer name column into the below scripts  

    https://gallery.technet.microsoft.com/scriptcenter/Find-Users-who-Logged-in-07dbe5f6#content

    When i'm trying to get the logs from multiple computer it is very difficult to identify the logs from which server.

    I'm using below command to fetch the data and i'm getting only user and timestamp details. It would be great if i get the result with computer name. 

    Import-Csv .\computers.csv| %{Get-ULogged -After 02/02/2018 -Before 02/13/2018 -ComputerName $_.ComputerName }


    Wednesday, February 14, 2018 9:29 AM

Answers

  • E={$_.Computername}. here $_ is the current item in the pipeline, that is the output of Get-ULogged, which anyways does not have a computername property. Hence it is empty.

    Below code will do what you want

    Import-Csv .\computers.csv | 
      Foreach-Object -Process {
    $ComputerName = $_.ComputerName
    Get-ULogged -After 02/02/2018 -Before 02/13/2018 -ComputerName $ComputerName| select *,@{L="Computer";E={$ComputerName}}
    } 
    


    Regards kvprasoon


    Wednesday, February 14, 2018 7:08 PM

All replies

  • Have you tried
    Import-Csv .\computers.csv | 
      %{Get-ULogged -After 02/02/2018 -Before 02/13/2018 -ComputerName $_.ComputerName | select *,@{L="Computer";E={$_}} } 


    • Edited by hpotsirhc Wednesday, February 14, 2018 9:45 AM
    Wednesday, February 14, 2018 9:45 AM
  • Under computer tab, i'm getting output combination of User+loggedInAt

    User                                    LoggedInAt                              Computer
    ----                                    ----------                                   --------
    User: abc\user              2/12/2018 11:44:03 PM                   @{User=User: abc\user;
                                                                                            LoggedInAt=2/12/2018 11:44:03 PM}

    If i am using "$env:computername" then getting the same hostname from where im running the script.
    Import-Csv .\computers.csv | 
      %{Get-ULogged -After 02/02/2018 -Before 02/13/2018 -ComputerName $_.ComputerName | select *,@{L="Computer";E={$env:computername}} } 



    • Edited by Partha1012 Wednesday, February 14, 2018 11:29 AM
    Wednesday, February 14, 2018 9:53 AM
  • Sorry, my mistake. But it is quite simple now:

    Import-Csv .\computers.csv | 
      %{Get-ULogged -After 02/02/2018 -Before 02/13/2018 -ComputerName $_.ComputerName | select *,@{L="Computer";E={$_.ComputerName}} }

    Wednesday, February 14, 2018 2:45 PM
  • Sorry, now the computer column output is empty
    Wednesday, February 14, 2018 4:03 PM
  • E={$_.Computername}. here $_ is the current item in the pipeline, that is the output of Get-ULogged, which anyways does not have a computername property. Hence it is empty.

    Below code will do what you want

    Import-Csv .\computers.csv | 
      Foreach-Object -Process {
    $ComputerName = $_.ComputerName
    Get-ULogged -After 02/02/2018 -Before 02/13/2018 -ComputerName $ComputerName| select *,@{L="Computer";E={$ComputerName}}
    } 
    


    Regards kvprasoon


    Wednesday, February 14, 2018 7:08 PM