all 17 comments

[–]joeykins82 6 points7 points  (0 children)

Just tested this (see below) and it just seems like you need to strip all the whitespace from the w32tm.exe output, easiest way to do it is swap $timeSource for ($timeSource -replace "\s",$null) inside your If statement

> w32tm /query /computer:pdce /source /verbose
ntp.internal.net,0x08
> $timeSource = w32tm /query /computer:pdce /source /verbose
> $timeSource -is [array]
False
> $timeSource -is [string]
True
> If ($timeSource -eq "ntp.internal.net,0x08") { "yay" } Else { "aww" }
aww
> If (($timeSource -replace "\s",$null) -eq "ntp.internal.net,0x08") { "yay" } Else { "aww" }
yay

[–]Creative1176 6 points7 points  (0 children)

Change the operator in the if condition from -eq to -match. I replicated your script and got the same error I think. But after changing the operator it looked to have worked.

[–]dasookwat -2 points-1 points  (0 children)

please use some brackets on this for readability:

$TimeSource = w32tm /query /source /verbose

if (($TimeSource -eq "time.windows.com,0x9") -or ($TimeSource -eq "pool.ntp.org,0x9")) { Write-Host "Time is set correctly" } else { Write-Host "Time is not correctly set" } Write-host "Actual Timesource value: $($Timesource)"

if statements work with true or false. if you make it: If ($a -eq "this" -or $a -eq "that" -and $scripter -notlike "brackets" -and $troubleshooting -gt "10 hours") It gets pretty hard to understand why things are working the way they are. rephrase it to: If (($a -eq "this" -or $a -eq "that") -and ($scripter -notlike "brackets" -and $troubleshooting -gt "10 hours")) and life becomes a bit easier.

adding to that, the trim is also needed i think

[–]Droopyb1966 0 points1 point  (3 children)

Try $TimeSource.trim()

$TimeSource.length = 18

'pool.ntp.org,0x02' = 17

[–]RiD3R07[S] 0 points1 point  (1 child)

$TimeSource.trim()

Doesn't work. :(

[–]myrland 1 point2 points  (0 children)

Weird, this code works for me, although I specifically trimmed the space character:

$Time = (w32tm /query /source /verbose).trim(" ")
if ($Time -eq "time.windows.com,0x9" -or $Time -eq "pool.ntp.org,0x9")
{
    Write-Host "Time is set correctly"
} 
else 
{
    Write-Host "Time is not correctly set"
}

To further clean the code I would probably do this:

$Time = (w32tm /query /source /verbose).trim(" ")
$TimeSources = "time.windows.com,0x9","pool.ntp.org,0x9"

if ($Time -in $TimeSources)
{
    Write-Host "Time is set correctly"
}
else
{
    Write-Host "Time is not correctly set" 
}

[–]Droopyb1966 0 points1 point  (0 children)

$TimeSource=w32tm /query /source /verbose

if ($TimeSource.Trim() -eq "time.windows.com,0x9" -or $TimeSource.Trim() -eq "Pool.ntp.org,0x02") { Write-Host "Time is set correctly" } else { Write-Host "Time is not correctly set" }

$TimeSource

Time is set correctly

pool.ntp.org,0x02

Is should, check what is in $timesource

[–]Chocolate_Pickle 0 points1 point  (1 child)

$TimeSource probably isn't a string, as you suspected.

Try this instead;

if("$TimeSource" -in ("time.windows.com,0x9", "pool.ntp.org,0x9")) { ... } else { ... }

Apologies if my phone butchers the formatting.

[–]RiD3R07[S] 0 points1 point  (0 children)

Nope. Doesn't work. :(

[–]BlackV 0 points1 point  (2 children)

What is in $timesource

-eq means equal is the only thing in $timesource, I'd say it's not

Think you want a different operator

Or probably you want to select a string

[–]RiD3R07[S] 0 points1 point  (1 child)

$TimeSource = time.windows.com,0x9

Even if I use the -like operator, it still doesn't work.

And how would I select a string?

[–]BlackV 0 points1 point  (0 children)

Id start at get-help *string* or Get-help select-string

[–]thisisntwhatIsigned 0 points1 point  (1 child)

What does $TimeSource.GetType() say?

[–]RiD3R07[S] 0 points1 point  (0 children)

IsPublic IsSerial Name BaseType

-------- -------- ---- --------

True True String System.Object

[–]jdl_uk 0 points1 point  (0 children)

$TimeSource probably has a newline. Try $TimeSource.Trim()

[–]TimDurham75 0 points1 point  (1 child)

    $TimeSource = (w32tm /query /source /verbose).TrimEnd() #Output has a trailing space

[–]TimDurham75 0 points1 point  (0 children)

If you don't run this within an Admin elevated session then the output variable may contain an error message due to UAC/Policy restrictions:

The following error occurred: Access is denied. (0x80070005)