Requesting help getting computer name to show up in script

Welcome Forums General PowerShell Q&A Requesting help getting computer name to show up in script

This topic contains 3 replies, has 2 voices, and was last updated by

 
Participant
4 years, 6 months ago.

  • Author
    Posts
  • #14195

    Participant
    Points: 0
    Rank: Member

    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? 🙂

  • #14196

    Member
    Points: 0
    Rank: Member

    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.

  • #14197

    Participant
    Points: 0
    Rank: Member

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

  • #14312

    Participant
    Points: 0
    Rank: Member

    Works great, thanks!

The topic ‘Requesting help getting computer name to show up in script’ is closed to new replies.