Requesting help getting computer name to show up in script

I’m trying to write a script to check how long it’s been since a process was started on a group of 20ish remote machines. I can get the script to output the date correctly, but I’m stuck trying to get it to also tell me the name of the machine that corresponds to the date.

Here’s what I have so far:

$ErrorActionPreference = “silentlycontinue”
$computers = Get-Content -path C:\users\mprill\desktop\scripts\dispatchWorkstations.txt # Loads the computers variable with the machines listed in the text file
$output = foreach ($computer in $computers)
{
write-host Checking $computer
$DateFix = Get-WmiObject -class win32_process -ComputerName $computer | where name -eq ‘BrokerService.exe’ | select pscomputername,creationdate
[management.managementDateTimeConverter]::ToDateTime($DateFix.CreationDate)
}

write-host Done!
$output | select pscomputername,date | export-csv -path c:\users\mprill\desktop\scripts\dispatchWorkstationBrokerRestartTime.csv

You can see I’ve tried adding in pscomputername in a couple spots to try to find out where I’m missing it, but all it does it output a column of dates into the .csv.

Can anyone please tell me what I’m missing or point me in a direction? :slight_smile:

Try this:

Get-WmiObject -Class Win32_Process -ComputerName $computers -Filter 'Name="BrokerService.exe"' |
Select-Object -Property PSComputerName, @{Name = 'StartTime'; Expression = { [System.Management.ManagementDateTimeConverter]::ToDateTime($_.CreationDate) }} |
export-csv -path c:\users\mprill\desktop\scripts\dispatchWorkstationBrokerRestartTime.csv

Your problem was that you were only outputting DateTime objects (the result from the ToDateTime() method) in your loop, and discarding the WMI objects.

Thanks I’ll give that a shot, I assumed I was missing something basic, gotta go back and watch Don’s videos :slight_smile:

Works great, thanks!