Get Current Time For All Domain Workstations

Home Forums Scripting PowerShell Get Current Time For All Domain Workstations

This topic contains 5 replies, has 5 voices, and was last updated by  Anonymous 4 months, 3 weeks ago.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts

  • shadragon
    Member
    #167629

    Is there a way to use a PS script to get the current times of all Workstations in the domain or a specific OU?

    To be clear, I don’t want to sync the times, I want to see what times are set on all workstations at the time of the script execution. A simple text list will do, workstation and time.

    We use AD for time sync and I am convinced we have some issues with time sync as I’ve found several workstations that are up to three minutes out of sync with other workstations in the same OU. In short, I need to see how large the problem is.

    Cheers.


    Anonymous
    #372111

    The simplest way I think would be to read in a text file with the PC names in it, then for each name run ‘invoke-command’ to call the built-in cmd prompt ‘w32tm /query /status’, and save the results for each query to a csv file. You’ll get more info that you originally want, but with the csv opened in Excel, you can manipulate the data just about any way you need.


    wullieb1
    Moderator
    #245796

    The following will get you the time

    (get-date).ToString(‘T’)[/CODE]

    However its pretty pointless if you don’t have the computer name

    Maybe something like this

    [CODE]$CompTime = $env:computername + ” ” + (get-date).ToString(‘T’)
    write-host $CompTime[/CODE]

    Output is this

    [CODE]WULLIE-RAZER 12:51:44 PM[/CODE][CODE](get-date).ToString(‘T’)[/CODE]

    However its pretty pointless if you don’t have the computer name

    Maybe something like this

    $CompTime = $env:computername + ” ” + (get-date).ToString(‘T’)
    write-host $CompTime[/CODE]

    Output is this

    [CODE]WULLIE-RAZER 12:51:44 PM[/CODE][CODE]$CompTime = $env:computername + ” ” + (get-date).ToString(‘T’)
    write-host $CompTime[/CODE]

    Output is this

    WULLIE-RAZER 12:51:44 PM[/CODE][CODE]WULLIE-RAZER 12:51:44 PM[/CODE]


    Anonymous
    #369416

    Thanks wullieb1. I took you code and added it to a For-Each script. When I run it I get one line with just the date. Code here, what am I missing? Thanks for the feedback guys,

    $computers = Get-Content -Path C:TempVM_ListsComputers.txt
    $txtfile = “C:TempVM_Listscomptime.txt”
    ForEach($computer in $computers)
    {
    $CompTime = $env:computers + ” ” + (get-date).ToString(‘T’) | Out-File $txtfile
    }[/CODE][CODE]$computers = Get-Content -Path C:TempVM_ListsComputers.txt
    $txtfile = “C:TempVM_Listscomptime.txt”
    ForEach($computer in $computers)
    {
    $CompTime = $env:computers + ” ” + (get-date).ToString(‘T’) | Out-File $txtfile
    }[/CODE]


    joeqwerty
    Moderator
    #304635

    Prior to Windows Server 2016, the Windows Time service was designed to keep time in a domain in sync within 5 minutes. If you have a 3 minute time difference then that isn’t a problem you’re going to find a solution for, nor is it withing the bounds of what Microsoft would consider a problem.


    wullieb1
    Moderator
    #245798
    shadragon;n517655 wrote:
    Thanks wullieb1. I took you code and added it to a For-Each script. When I run it I get one line with just the date. Code here, what am I missing? Thanks for the feedback guys,

    $computers = Get-Content -Path C:TempVM_ListsComputers.txt
    $txtfile = “C:TempVM_Listscomptime.txt”
    ForEach($computer in $computers)
    {
    $CompTime = $env:computers + ” ” + (get-date).ToString(‘T’) | Out-File $txtfile
    }[/CODE][/QUOTE]

    You’re pretty much overwriting the file each time you go through the loop.

    You need you append the information to the file.

    [CODE]$computers = Get-Content -Path C:TempVM_ListsComputers.txt
    $txtfile = “C:TempVM_Listscomptime.txt”
    ForEach($computer in $computers)
    {
    $CompTime = $env:computers + ” ” + (get-date).ToString(‘T’) | Out-File -append $txtfile
    }[/CODE][CODE]$computers = Get-Content -Path C:TempVM_ListsComputers.txt
    $txtfile = “C:TempVM_Listscomptime.txt”
    ForEach($computer in $computers)
    {
    $CompTime = $env:computers + ” ” + (get-date).ToString(‘T’) | Out-File $txtfile
    }[/CODE]

    You’re pretty much overwriting the file each time you go through the loop.

    You need you append the information to the file.

    $computers = Get-Content -Path C:TempVM_ListsComputers.txt
    $txtfile = “C:TempVM_Listscomptime.txt”
    ForEach($computer in $computers)
    {
    $CompTime = $env:computers + ” ” + (get-date).ToString(‘T’) | Out-File -append $txtfile
    }[/CODE][CODE]$computers = Get-Content -Path C:TempVM_ListsComputers.txt
    $txtfile = “C:TempVM_Listscomptime.txt”
    ForEach($computer in $computers)
    {
    $CompTime = $env:computers + ” ” + (get-date).ToString(‘T’) | Out-File -append $txtfile
    }[/CODE]

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.