首页 > 解决方案 > 获取远程主机列表的时间

问题描述

我需要从通过 VPN 连接的远程主机列表中获取时间。

该列表位于一个 txt 文件中,我希望将输出放在另一个文件中,其中包含 IP 和在远程机器上注册的时间。

我用这个windows powershell代码解决了:

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

标签: pythonwindowspowershelltimeremote-access

解决方案


类似的东西。你会得到当地时间。

[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

推荐阅读