Well, PowerShell is Object based, but you can do lots of things with strings.
The command you are using is a string adn hard to just do string maniplulation
on it.
Though not exactly what you are trying to do here, but…
Folks have alerady done wotk to help here. For example, see…
‘jeffwouters.nl/index.php/2013/09/powershell-function-to-get-time-server-ntp-settings-from-clients’
‘gallery.technet.microsoft.com/scriptcenter/Check-timesource-W32TM-of-3d80b441/view/Discussions’
All that being said, you can just extract the parts you need.
Use the localhost computer list to simple parse Win32tm output
‘localhost’,$env:COMPUTERNAME `
| % { w32tm /query /computer:$env:COMPUTERNAME /status }
Results
Leap Indicator: 0(no warning)
Stratum: 1 (primary reference - syncd by radio clock)
Precision: -6 (15.625ms per tick)
Root Delay: 0.0000000s
Root Dispersion: 10.0000000s
ReferenceId: 0x4C4F434C (source name: “LOCL”)
Last Successful Sync Time: 10/11/2017 11:12:47 PM
Source: Free-running System Clock
Poll Interval: 6 (64s)
Leap Indicator: 0(no warning)
Stratum: 1 (primary reference - syncd by radio clock)
Precision: -6 (15.625ms per tick)
Root Delay: 0.0000000s
Root Dispersion: 10.0000000s
ReferenceId: 0x4C4F434C (source name: “LOCL”)
Last Successful Sync Time: 10/11/2017 11:12:47 PM
Source: Free-running System Clock
Poll Interval: 6 (64s)
Parsing the host Win32tm output, the localhost computer list to simple parse Win32tm output
Function Get-HostWin32tmConfiguration
{
[CmdletBinding()]
Param
(
[string]$ComputerList = $env:COMPUTERNAME
)
ForEach ($TargetHost in $ComputerList)
{
(w32tm /query /computer:$TargetHost /status) -replace ': ',',' `
| ConvertFrom-Csv -Header Name,Value `
| Where {$_.Name -match "Source|Precision|Sync "} `
| Select @{Name = 'HostName';Expression = {$TargetHost}},Name,Value
}
}
Write the CSV capture of the full request
Get-HostWin32tmConfiguration -ComputerList (‘localhost’,$env:COMPUTERNAME) `
| Format-Table -AutoSize
Results
HostName Name Value
localhost Precision -6 (15.625ms per tick)
localhost Last Successful Sync Time 10/11/2017 11:12:47 PM
localhost Source Free-running System Clock
WC01 Precision -6 (15.625ms per tick)
WC01 Last Successful Sync Time 10/11/2017 11:12:47 PM
WC01 Source Free-running System Clock
Or, just in case you wanted to pick more properties
Function Get-HostWin32tmConfiguration
{
[CmdletBinding()]
Param
(
[string]$ComputerList = $env:COMPUTERNAME
)
ForEach ($TargetHost in $ComputerList)
{
(w32tm /query /computer:$TargetHost /status) -replace ': ',',' `
| ConvertFrom-Csv -Header Name,Value `
| Select @{Name = 'HostName';Expression = {$TargetHost}},Name,Value
}
}
Write the CSV capture of the full request
Get-HostWin32tmConfiguration -ComputerList (‘localhost’,$env:COMPUTERNAME) `
| Select HostName,Name,Value `
| Where Name -match ‘Source|Precision|Sync’ `
| Format-Table -AutoSize
HostName Name Value
localhost Precision -6 (15.625ms per tick)
localhost Last Successful Sync Time 10/11/2017 11:12:47 PM
localhost Source Free-running System Clock
WC01 Precision -6 (15.625ms per tick)
WC01 Last Successful Sync Time 10/11/2017 11:12:47 PM
WC01 Source Free-running System Clock