【问题标题】:Get the time of a list of remote hosts获取远程主机列表的时间
【发布时间】:2019-12-03 01:45:39
【问题描述】:
我需要从通过 VPN 连接的远程主机列表中获取时间。
该列表位于一个 txt 文件中,我希望将输出保存在另一个文件中,其中包含远程机器上注册的 IP 和时间。
我用这个windows powershell的代码解决了:
foreach($line in [System.IO.File]::ReadLines("percorso_del_file.txt"))
{
net time \\$line
}
【问题讨论】:
标签:
python
windows
powershell
time
remote-access
【解决方案1】:
类似的东西。您将获得当地时间。
[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