简体   繁体   中英

Get the time of a list of remote hosts

I need to get the time from a list of remote hosts connected by a VPN.

The list is located in a txt file and i would like to have the output in another file containing the IPs and the time registered on the remote machines.

I resolved with this code of windows powershell:

foreach($line in [System.IO.File]::ReadLines("percorso_del_file.txt"))
{ 
       net time \\$line
}

Something like that. You will get local time.

[System.IO.File]::ReadLines("percorso_del_file.txt") | 
    Foreach-Object { 
        $local:computer = $_
        try {
            $local:wmi_os = Get-WmiObject -Class 'win32_operatingsystem' -Property @('localdatetime','__SERVER') -ComputerName $local:computer -ErrorAction Stop
            $local:o = New-Object -TypeName 'PSObject' -Property @{
               ComputerName = $local:wmi_os.'__SERVER';
               Time = $local:wmi_os.ConvertToDateTime($local:wmi_os.localdatetime)
            }
        } catch {
            $local:o = New-Object -TypeName 'PSObject' -Property @{
                ComputerName = $local:computer
                Time = [DateTime]::MinValue
            }
        }
        return $local:o
    } | 
    Export-Csv -LiteralPath 'c:\report.csv' -Encoding UTF32 -Delimiter "`t" -NoTypeInformation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM