PoshCode Archive  Artifact [2fcd948ff3]

Artifact 2fcd948ff31671fcab36a73d460c926036a2c29543581d3098fe042f1327ebd3:

  • File SNTP.ps1 — part of check-in [eb1b591b74] at 2018-06-10 13:13:01 on branch trunk — Get and Set functions for SNTP (user: a Monkey size: 3277)

# encoding: ascii
# api: powershell
# title: SNTP
# description: Get and Set functions for SNTP
# version: 1.0
# type: script
# author: a Monkey
# license: CC0
# function: Get-SNTPServer
# x-poshcode-id: 2604
# x-archived: 2012-12-29T04:30:20
# x-published: 2012-04-06T10:31:00
#
#

<#
.SYNOPSIS
Gets the SNTP servers a machine is configured to use
 
.DESCRIPTION
Gets the SNTP servers a machine is configured to use
 
.PARAMETER Server
The machine to get the SNTP Servers for
 
.EXAMPLE
PS C:\> Get-SNTPServer -Server MachineName
This is return the SNTP servers for MachienName
 
.EXAMPLE
PS C:\> Get-SNTPServer -server Machine1 | Set-SNTPServer -server Machine2
This will get the sntp server for Machine1 and set Machine2 to the same

.EXAMPLE
PS C:\> Get-Content servers.txt | Get-SNTPServer 
 
.NOTES
    NAME        :  Get-SNTPServer
    VERSION     :  1.0
    LAST UPDATED:  04/04/2011
    AUTHOR      :  a Monkey
 
.LINK
http://amonkeysden.tumblr.com/post/4393059808/sntp-module
 
#>
function Get-SNTPServer
{
    [cmdletbinding()]
    Param(
        [Parameter(
            Mandatory = $True,
            Position = 0,
            ParameterSetName = '',
            ValueFromPipeline = $True)]
        [string]$Server
    )
    [string] $z = net time \\$server /QUERYSNTP
    if (($z.contains("The current SNTP value is:"))) {
        #$out = "{0}: {1}" -f $Server,$z.split(" ")[5]
        #Write-Output $out
        Write-Output $z.split(" ")[5]
    } else {
        Write-Error "$z"
    }
}
 
<#
.SYNOPSIS
Sets the SNTP servers a machine is configured to use
 
.DESCRIPTION
Sets the SNTP servers a machine is configured to use
 
.PARAMETER Servers
The machines to set the SNTP Servers for
 
.PARAMETER $SNTPServerList
The machine to get the SNTP Servers for
 
.EXAMPLE
PS C:\> Set-SNTPServer -Server MachineName -SNTPServerList "dc1,dc2"
This will set the SNTP servers to DC1 and DC2 for MachienName
 
.EXAMPLE
PS C:\> Get-SNTPServer -server Machine1 | Set-SNTPServer -server Machine2
This will get the sntp server for Machine1 and set Machine2 to the same

.EXAMPLE
PS C:\> Get-Content servers.txt | Set-SNTPServer -SNTPServerList "dc1,dc2"

.NOTES
    NAME        :  Set-SNTPServer
    VERSION     :  1.0
    LAST UPDATED:  04/04/2011
    AUTHOR      :  a Monkey
 
.LINK
http://amonkeysden.tumblr.com/post/4393059808/sntp-module

#>
function Set-SNTPServer
{
    [cmdletbinding()]
    Param(
        [Parameter(
            Mandatory = $True,
            Position = 0,
            ParameterSetName = '',
            ValueFromPipeline = $True)]
        [string]$Server,
        [Parameter(
            Mandatory = $True,
            Position = 1,
            ParameterSetName = '',
            ValueFromPipeline = $True)]
        [string]$SNTPServerList
    )
    foreach ($s in $Server.Split(",")) 
    {
        [string] $z = net time \\$s /SETSNTP:$SNTPServerList
        if (($z.contains("The command completed successfully."))) {
            Write-Output $z
        } else {
            Write-Error $z
        }
    }
}
 
 
# export the functions to be used
Export-ModuleMember Set-SNTPServer
Export-ModuleMember Get-SNTPServer