当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] PowerShell:使用 Get-Time 检索和监控所有计算机的系统时间

作者:精品下载站 日期:2024-12-14 07:30:39 浏览:15 分类:玩电脑

PowerShell:使用 Get-Time 检索和监控所有计算机的系统时间


您的 Active Directory 域中的时间同步是否有问题,或者您只对监视和确定服务器、工作站或域控制器的系统时间感兴趣?如果您的答案是肯定的,请继续阅读。在本文中,我将为您提供高级 PowerShell 功能,该功能将远程检索您选择的单台计算机或一组加入域的计算机的系统时间。

目标

我希望它尽可能简单:只需一个命令即可获取 Active Directory 域中的所有或某些计算机的系统时间。我们走吧。

本地系统时间

[玩转系统] PowerShell:使用 Get-Time 检索和监控所有计算机的系统时间

很简单的事情吧?让我们开始派对吧。

特定计算机的本地系统时间

[玩转系统] PowerShell:使用 Get-Time 检索和监控所有计算机的系统时间

所有域控制器的本地系统时间

[玩转系统] PowerShell:使用 Get-Time 检索和监控所有计算机的系统时间

现在让我们更进一步。

所有加入域的Windows服务器的本地系统时间

[玩转系统] PowerShell:使用 Get-Time 检索和监控所有计算机的系统时间

这就引出了主要部分,即函数本身。

获取时间

这部分仅包含您稍后需要的代码。如需在 PowerShell 会话中使用 Get-Time 的帮助,请转到下一部分。


function Get-Time {

# .SYNOPSIS
# Get-Time is an advanced Powershell function. It obtains the local system time of a scope of computers or the system time of particular remote computers.

# .DESCRIPTION
# Uses the buil-in function Get-Date. Define scope or computer name.

# .PARAMETER
# Scope
# Define the scope. Possible values: "AllServer", "DomainController", "Computer"

# .PARAMETER
# Computer
# Provide computer name of remote computer.

# .EXAMPLE
# Get-Time -Scope AllServer

# .NOTES
# Author: Patrick Gruenauer, MVP PowerShell
# Web: https://a-d.site

[CmdletBinding()]

param
(

[Parameter(Mandatory=$false, HelpMessage='Enter the following values: AllServer, DomainController, Computer')]
[ValidateSet("AllServer", "DomainController", "Computer")]
$Scope,

[parameter(Mandatory=$false)]
$Computer="$env:Computername"

)
$server=(Get-ADComputer -Filter 'operatingsystem -like "*server*"-and enabled -eq "true"').Name
$dc=Get-ADDomainController -Filter * | Select-Object -ExpandProperty Name

$result=@()

switch ($Scope)

{

'AllServer' {

foreach ($s in $server) {
$t=Invoke-Command -ComputerName $s {Get-Date -Displayhint time | Select-Object -ExpandProperty DateTime} -ErrorAction SilentlyContinue

$result +=New-Object -TypeName PSCustomObject -Property ([ordered]@{
'Server'= $s
'Time' = $t

})}

}

'DomainController' {

foreach ($d in $dc) {
$t=Invoke-Command -ComputerName $d {Get-Date -Displayhint time | Select-Object -ExpandProperty DateTime} -ErrorAction SilentlyContinue

$result +=New-Object -TypeName PSCustomObject -Property ([ordered]@{
'Server'= $d
'Time' = $t

})}

}

}

If ($Computer -ne "$env:computername") {

Try {

$t=Invoke-Command -ComputerName $Computer {Get-Date -Displayhint time | Select-Object -ExpandProperty DateTime} -ErrorAction Stop

$result +=New-Object -TypeName PSObject -Property ([ordered]@{
'Computer'= $Computer
'Time' = $t

})

}

Catch {

$result+=New-Object -TypeName PSObject -Property ([ordered]@{
'Computer'=$Computer
'Time'='Computer could not be reached'

})

}

}

If (($computer -eq "$env:computername") -and ($scope -eq $null)) {
Get-Date -Displayhint time | Select-Object -ExpandProperty DateTime

}

Write-Output $result
}

如何使用它

将上述代码复制到 PowerShell ISE (ise.exe) 中并运行该代码。然后输入命令并享受它的乐趣。

如果您想让该函数永久可用,以便每次启动 PowerShell 时该函数都可用,则必须在 C:\Program Files\WindowsPowerShell\Modules 中创建一个文件夹。将文件夹命名为Get-Time。然后将代码另存为该文件夹中的 .psm1 文件。下面的屏幕截图将为您提供帮助。

[玩转系统] PowerShell:使用 Get-Time 检索和监控所有计算机的系统时间

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯