PoshCode Logo PowerShell Code Repository

SNTP by a Monkey 10 months ago
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/2604"></script>download | new post

Get and Set functions for SNTP

  1.  
  2. <#
  3. .SYNOPSIS
  4. Gets the SNTP servers a machine is configured to use
  5.  
  6. .DESCRIPTION
  7. Gets the SNTP servers a machine is configured to use
  8.  
  9. .PARAMETER Server
  10. The machine to get the SNTP Servers for
  11.  
  12. .EXAMPLE
  13. PS C:\> Get-SNTPServer -Server MachineName
  14. This is return the SNTP servers for MachienName
  15.  
  16. .EXAMPLE
  17. PS C:\> Get-SNTPServer -server Machine1 | Set-SNTPServer -server Machine2
  18. This will get the sntp server for Machine1 and set Machine2 to the same
  19.  
  20. .EXAMPLE
  21. PS C:\> Get-Content servers.txt | Get-SNTPServer
  22.  
  23. .NOTES
  24.     NAME        :  Get-SNTPServer
  25.     VERSION     :  1.0
  26.     LAST UPDATED:  04/04/2011
  27.     AUTHOR      :  a Monkey
  28.  
  29. .LINK
  30. http://amonkeysden.tumblr.com/post/4393059808/sntp-module
  31.  
  32. #>
  33. function Get-SNTPServer
  34. {
  35.     [cmdletbinding()]
  36.     Param(
  37.         [Parameter(
  38.             Mandatory = $True,
  39.             Position = 0,
  40.             ParameterSetName = '',
  41.             ValueFromPipeline = $True)]
  42.         [string]$Server
  43.     )
  44.     [string] $z = net time \\$server /QUERYSNTP
  45.     if (($z.contains("The current SNTP value is:"))) {
  46.         #$out = "{0}: {1}" -f $Server,$z.split(" ")[5]
  47.         #Write-Output $out
  48.         Write-Output $z.split(" ")[5]
  49.     } else {
  50.         Write-Error "$z"
  51.     }
  52. }
  53.  
  54. <#
  55. .SYNOPSIS
  56. Sets the SNTP servers a machine is configured to use
  57.  
  58. .DESCRIPTION
  59. Sets the SNTP servers a machine is configured to use
  60.  
  61. .PARAMETER Servers
  62. The machines to set the SNTP Servers for
  63.  
  64. .PARAMETER $SNTPServerList
  65. The machine to get the SNTP Servers for
  66.  
  67. .EXAMPLE
  68. PS C:\> Set-SNTPServer -Server MachineName -SNTPServerList "dc1,dc2"
  69. This will set the SNTP servers to DC1 and DC2 for MachienName
  70.  
  71. .EXAMPLE
  72. PS C:\> Get-SNTPServer -server Machine1 | Set-SNTPServer -server Machine2
  73. This will get the sntp server for Machine1 and set Machine2 to the same
  74.  
  75. .EXAMPLE
  76. PS C:\> Get-Content servers.txt | Set-SNTPServer -SNTPServerList "dc1,dc2"
  77.  
  78. .NOTES
  79.     NAME        :  Set-SNTPServer
  80.     VERSION     :  1.0
  81.     LAST UPDATED:  04/04/2011
  82.     AUTHOR      :  a Monkey
  83.  
  84. .LINK
  85. http://amonkeysden.tumblr.com/post/4393059808/sntp-module
  86.  
  87. #>
  88. function Set-SNTPServer
  89. {
  90.     [cmdletbinding()]
  91.     Param(
  92.         [Parameter(
  93.             Mandatory = $True,
  94.             Position = 0,
  95.             ParameterSetName = '',
  96.             ValueFromPipeline = $True)]
  97.         [string]$Server,
  98.         [Parameter(
  99.             Mandatory = $True,
  100.             Position = 1,
  101.             ParameterSetName = '',
  102.             ValueFromPipeline = $True)]
  103.         [string]$SNTPServerList
  104.     )
  105.     foreach ($s in $Server.Split(","))
  106.     {
  107.         [string] $z = net time \\$s /SETSNTP:$SNTPServerList
  108.         if (($z.contains("The command completed successfully."))) {
  109.             Write-Output $z
  110.         } else {
  111.             Write-Error $z
  112.         }
  113.     }
  114. }
  115.  
  116.  
  117. # export the functions to be used
  118. Export-ModuleMember Set-SNTPServer
  119. Export-ModuleMember Get-SNTPServer

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me