Script that checks registry settings for Time Server configuration for hundreds of Windows member servers.

1. Must pull all servers from a .txt file (already have this)
2. Check time server configuration.  IE:  "computer","NTPServer","NTP_Type"
3. Export results to .csv, html or .xlsx

Thanks!
mjm21Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mauro CazabonnetSenior .NET Software EngineerCommented:
This should get ya started
$outfile = "C:\temp\output.txt"
$null | Out-File $outfile

$Servers = "Server1","Server2"

function Get-TimeServer {
<#
	.Synopsis
	Gets the time server as configured on a computer.
	.DESCRIPTION
	Gets the time server as configured on a computer.
	The default is localhost but can be used for remote computers.
	.EXAMPLE
	Get-TimeServer -ComputerName "Server1"
	.EXAMPLE
	Get-TimeServer -ComputerName "Server1","Server2"
	.EXAMPLE
	Get-TimeServer -Computer "Server1","Server2"
	.EXAMPLE
	Get-TimeServer "Server1","Server2"
	.NOTES
	Written by Jeff Wouters.
#>
	[CmdletBinding(SupportsShouldProcess=$true)]
	param ( 
		[parameter(mandatory=$true,position=0)][alias("computer")][array]$ComputerName="localhost"
	)
	begin {
		$HKLM = 2147483650
	}
	process {
        $Output = New-Object -TypeName psobject
		$Output | Add-Member -MemberType 'NoteProperty' -Name 'ComputerName' -Value "Computer"
        $Output | Add-Member -MemberType 'NoteProperty' -Name 'TimeServer' -Value "WMI Error"
	    $Output | Add-Member -MemberType 'NoteProperty' -Name 'Type' -Value "WMI Error"
		foreach ($Computer in $ComputerName) {
			$TestConnection = Test-Connection -ComputerName $Computer -Quiet -Count 1
			if ($TestConnection) {				
				try {
                    $Output.ComputerName = $Computer
                    $reg = [wmiclass]"\\$Computer\root\default:StdRegprov"
				    $key = "SYSTEM\CurrentControlSet\Services\W32Time\Parameters"
				    $servervalue = "NtpServer"
				    $server = $reg.GetStringValue($HKLM, $key, $servervalue)
				    $ServerVar = $server.sValue -split ","
				    $Output.TimeServer = $ServerVar[0]
    				$typevalue = "Type"
                    $type = $reg.GetStringValue($HKLM, $key, $typevalue)
                    $Output.Type = $Type.sValue	
                    $Output.Computername + "," + $Output.TimeServer + "," + $Output.Type | Out-File $outfile -Append
                } catch {
                }
			} else {
			}
            
		}
        
	}
}

"ComputerName," + "TimeServer," + "Type" | Out-File $outfile -Append 

Get-TimeServer -ComputerName $Servers 

Open in new window

mjm21Author Commented:
Thanks!  Instead of getting content this way:  Get-TimeServer -Computer "Server1","Server2"
I would like to get content from a text file.  There are hundreds of servers that need to get checked.  I have to text file already setup.
Mauro CazabonnetSenior .NET Software EngineerCommented:
I'll post something shortly
M
SD-WAN: Making It Work for You

As bandwidth requirements and Internet costs grow, businesses naturally want to manage budgets by reducing reliance on their most expensive connection types. Learn more about how to make SD-WAN work for your business in our on-demand webinar!

Mauro CazabonnetSenior .NET Software EngineerCommented:
Updated the code a bit

Sample server file list in csv format

ServerName
Server1
Server2
Server3
etc..

$outfile = "C:\temp\output.txt"
$null | Out-File $outfile

$ServerFile = "C:\Temp\ServerList.csv"

#Build Header
"ComputerName," + "TimeServer," + "Type" | Out-File $outfile -Append 

$HKLM = 2147483650
$Output = New-Object -TypeName psobject
$Output | Add-Member -MemberType 'NoteProperty' -Name 'ComputerName' -Value "Computer"
$Output | Add-Member -MemberType 'NoteProperty' -Name 'TimeServer' -Value "WMI Error"
$Output | Add-Member -MemberType 'NoteProperty' -Name 'Type' -Value "WMI Error" 
Import-Csv $ServerFile | ForEach {
     $Server = $_.ServerName
     $TestConnection = Test-Connection -ComputerName $Server -Quiet -Count 1
     if ($TestConnection) {				
	   try {
        $Output.ComputerName = $Server
        $reg = [wmiclass]"\\$Server\root\default:StdRegprov"
		$key = "SYSTEM\CurrentControlSet\Services\W32Time\Parameters"
		$servervalue = "NtpServer"
		$server = $reg.GetStringValue($HKLM, $key, $servervalue)
		$ServerVar = $server.sValue -split ","
		$Output.TimeServer = $ServerVar[0]
    	$typevalue = "Type"
        $type = $reg.GetStringValue($HKLM, $key, $typevalue)
        $Output.Type = $Type.sValue	
        $Output.Computername + "," + $Output.TimeServer + "," + $Output.Type | Out-File $outfile -Append
       }
        catch {
            "Error processing " + $Server | Out-File $outfile -Append 
        }         
     }
     else {
        "Failed test connection to server " + $Server | Out-File $outfile -Append 
     }
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mjm21Author Commented:
Thanks!  Testing shortly!
mjm21Author Commented:
Worked.  Thanks!  Appreciate it!
Mauro CazabonnetSenior .NET Software EngineerCommented:
Sure np
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.