获取远程主机列表的时间

2024-03-28 10:19:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要从VPN连接的远程主机列表中获取时间。你知道吗

这个列表位于一个txt文件中,我想在另一个包含IP和远程机器上注册的时间的文件中输出。你知道吗

我用以下windows powershell代码解决了:

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

Tags: 文件代码iniptxt机器列表远程
1条回答
网友
1楼 · 发布于 2024-03-28 10:19:24

差不多吧。你会得到当地时间。你知道吗

[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

相关问题 更多 >