Read computername from csv and list last logged on user in csv

Welcome Forums General PowerShell Q&A Read computername from csv and list last logged on user in csv

Viewing 3 reply threads
  • Author
    Posts
    • #257162
      Participant
      Topics: 1
      Replies: 1
      Points: 13
      Rank: Member

      Hello

      I found this neat little script which works with manual input.

      But I’d want some changed here.

      First I would like to read from a csv I have (AllComputers.csv). This csv has all 4000+ computers we have here. All listed under the name ‘Computername’ in the csv.

      Next I would love that it’s exported back to another csv with all computers listed and their last logged on user next to it.

       

      Thanks

    • #257186
      Senior Moderator
      Topics: 9
      Replies: 1408
      Points: 5,227
      Helping Hand
      Rank: Community MVP

      Well, the only change you need here is to take the computernames form CSV file using Import-CSV cmdlet.
      A small example below
      Lets say the header for computer name is Name

      But this is gonna kill you with time when targeting huge number of nodes. You can try using Invoke-Command with -AsJob then Get-CimInstance inside the scriptblock of Invoke-Command
      #1

      #2

      • This reply was modified 2 months ago by kvprasoon. Reason: Post correction
    • #257192
      Participant
      Topics: 1
      Replies: 85
      Points: 387
      Helping Hand
      Rank: Contributor

      With 4000 computers, this is going to take a long time.

       

    • #257198
      Participant
      Topics: 16
      Replies: 1842
      Points: 3,514
      Helping Hand
      Rank: Community Hero

      There are a ton of resources that show you how to read from a CSV, even in this forum. Get-WMIObject is deprecated, use Get-CimInstance. The logic in the code could use some work.

      • Get all network profiles
      • Sort profiles on lastlogon descending
      • Select the first item with all properties
      • Where the lastlogon matches 14 char digit
      • Loop thru each objects
      • Create a new object with parsed date

      When I test, the date does not require parsing:

      Note that NULL lastlogon date are filtered out in the actual query, so no need for the Where or the for loop or creating another object:

Viewing 3 reply threads
  • You must be logged in to reply to this topic.