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: 0
      Points: 11
      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: 1309
      Points: 4,781
      Helping Hand
      Rank: Community Hero

      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 -AsJob parameter of Get-WmiObject cmdlet or use Invoke-Command with -AsJob then Get-WmiObject inside the scriptblock of Invoke-Command
      #1

      #2

    • #257192
      Participant
      Topics: 0
      Replies: 77
      Points: 343
      Helping Hand
      Rank: Contributor

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

       

    • #257198
      Participant
      Topics: 13
      Replies: 1753
      Points: 3,136
      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.