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

Hello

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

$com=read-host "Enter Computer name here"
Get-WmiObject -Class Win32_NetworkLoginProfile -ComputerName $com | 
Sort-Object -Property LastLogon -Descending | 
Select-Object -Property * -First 1 | 
Where-Object {$_.LastLogon -match "(\d{14})"} | 
Foreach-Object { New-Object PSObject -Property @{ Name=$_.Name;LastLogon=[datetime]::ParseExact($matches[0], "yyyyMMddHHmmss", $null)}}

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

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

$ComputerLilst = Import-CSV -Path c:\temp\computerlist.csv
Foreach($Computer in $ComputerLilst.Name){
 Get-CimInstance -ComputerName $Computer -Class win32_bios
}

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

$AllJob | Wait-Job # waits for all jobs to be completed or failed
$Output = $AllJob | Receive-Job

# you can add more code to check job status and then decide to read or throw error

#2

$AllJob = Invoke-Command -ComputerName $ComputerLilst.Name -ScriptBlock {
  Get-CimInstance -Class win32_bios
} -AsJob -ThrottleLimit 200

# Throttlelimit will allow to limit max parallel remote executions at a time, we can consider it as batching

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

 

# Read Computer Names
$computers = Import-Csv Allcomputers.csv | Select -Expand Computername
$output = foreach ($com in $computers) {
 # $com will now be the current computername as the loop iterates
 # enter your code below without setting $com
}
$output | Export-Csv output.csv -NoType

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:

Get-CimInstance -ClassName Win32_NetworkLoginProfile -Property Caption, FullName, Description, LastLogon -Filter 'LastLogon IS NOT NULL' | 
Select Caption, 
       FullName, 
       Description, 
       LastLogon

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:

Caption FullName    Description                                                       LastLogon            
------- --------    -----------                                                       ---------            
sim   Rob Simmers Network login profile settings for Rob Simmers on DESKTOP123 9/11/2020 12:30:06 PM

Sweet

Thanks

I’ll look further into it. It’s no problem it would take a while.