Display current time with time zone in PowerShell

前端 未结 7 812
盖世英雄少女心
盖世英雄少女心 2021-02-14 02:06

I\'m trying to display the local time on my system with the TimeZone. How can I display time in this format the simplest way possible on any system?:

Time: 8:00:34 AM ES

7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-14 02:51

    I just combined several scripts and finally was able to run the script in my domain controller.

    The script provides the output of time and timezone for all the machines connected under the domain. We had a major issue with our application servers and used this script to cross check the time and timezone.

    # The below scripts provides the time and time zone for the connected machines in a domain
    # Appends the output to a text file with the time stamp
    # Checks if the host is reachable or not via a ping command
    
    Start-Transcript -path C:\output.txt -append
    $ldapSearcher = New-Object directoryservices.directorysearcher;
    $ldapSearcher.filter = "(objectclass=computer)";
    $computers = $ldapSearcher.findall();
    
    foreach ($computer in $computers)
    {
        $compname = $computer.properties["name"]
        $ping = gwmi win32_pingstatus -f "Address = '$compname'"
        $compname
        if ($ping.statuscode -eq 0)
        {
            try
            {
                $ErrorActionPreference = "Stop"
                Write-Host “Attempting to determine timezone information for $compname…”
                $Timezone = Get-WMIObject -class Win32_TimeZone -ComputerName $compname
    
                $remoteOSInfo = gwmi win32_OperatingSystem -computername $compname
                [datetime]$remoteDateTime = $remoteOSInfo.convertToDatetime($remoteOSInfo.LocalDateTime)
    
                if ($Timezone)
                {
                    foreach ($item in $Timezone)
                    {
                        $TZDescription  = $Timezone.Description
                        $TZDaylightTime = $Timezone.DaylightName
                        $TZStandardTime = $Timezone.StandardName
                        $TZStandardTime = $Timezone.StandardTime
                    }
                    Write-Host "Timezone is set to $TZDescription`nTime and Date is $remoteDateTime`n**********************`n"
                }
                else
                {
                    Write-Host ("Something went wrong")
                }
             }
             catch
             {
                 Write-Host ("You have insufficient rights to query the computer or the RPC server is not available.")
             }
             finally
             {
                 $ErrorActionPreference = "Continue"
             }
        }
        else
        {
            Write-Host ("Host $compname is not reachable from ping `n")
        }
    }
    
    Stop-Transcript
    

提交回复
热议问题