-
-
Save FilipChalupa/7c4dedec4541ee097f145f874361a297 to your computer and use it in GitHub Desktop.
Write-Host "Synchronizing your docker time." | |
$principal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent()) | |
if ($principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { | |
$name = (Get-VMIntegrationService -VMName DockerDesktopVM | Where-Object { $_.Id.StartsWith("Microsoft:") -and $_.Id.EndsWith("\2497F4DE-E9FA-4204-80E4-4B75C46419C0") }).Name # "Time Synchronization" for English Windows | |
Get-VMIntegrationService -VMName DockerDesktopVM -Name $name | Disable-VMIntegrationService | |
Get-VMIntegrationService -VMName DockerDesktopVM -Name $name | Enable-VMIntegrationService | |
echo $name | |
Write-Host "Time synced!" | |
Read-Host -Prompt "Press Enter to exit." | |
} else { | |
Write-Host "Elevated privileges are required." | |
Start-Process -FilePath "powershell" -ArgumentList "$('-File ""')$(Get-Location)$('\')$($MyInvocation.MyCommand.Name)$('""')" -Verb runAs | |
} |
FilipChalupa
commented
Feb 6, 2020
That's very useful!
Though for people not running an EN version of Windows, it's worth indicating they should first check what their localized name for "Time Synchronization" is and modify the script accordingly. For example, with a French Windows, it should be replaced by "Synchronisation date/heure"
Darn Windows! Can't use a stable name instead of a localized one!
Cool. Thank you for pointing that out.
You're welcome! Btw I had a very rough version of your script that didn't ask for elevated privileges (I'm not good at powershell). Hence, yours was really a time saver for me :)
@anyone, you can get the name of the services activated in your Docker VM by simply running Get-VMIntegrationService -VMName DockerDesktopVM
in an elevated powershell. this dumps a pretty table like this one:
VMName Name Enabled PrimaryStatusDescription SecondaryStatusDescription
------ ---- ------- ------------------------ --------------------------
DockerDesktopVM Interface de services d’invité False OK
DockerDesktopVM Pulsation True OK
DockerDesktopVM Échange de paires clé-valeur False OK
DockerDesktopVM Arrêt True OK
DockerDesktopVM Synchronisation date/heure True OK
DockerDesktopVM VSS False OK
I'm not experienced in Poweshell myself. I'm thinking, can we use Get-VMIntegrationService -VMName DockerDesktopVM
from you to get the localized name? :D We can cut the second column seventh row but I don't think the order is guaranteed.
Just for a reference this is my output:
VMName Name Enabled PrimaryStatusDescription SecondaryStatusDescription
------ ---- ------- ------------------------ --------------------------
DockerDesktopVM Guest Service Interface False OK
DockerDesktopVM Heartbeat True OK
DockerDesktopVM Key-Value Pair Exchange False OK
DockerDesktopVM Shutdown True OK
DockerDesktopVM Time Synchronization True OK
DockerDesktopVM VSS False OK
It seems the data is sorted alphabetically by the English Name (even in French). That seems to indicate either there are additional properties not listed here or this is the natural order of the list (also likely). However using the list order does not seem very reliable to me.
I've done some testing. In particular, Get-VMIntegrationService -VMName DockerDesktopVM | Format-List
dumps all the properties. There I found that the Time sync service was associated with the Microsoft:A43838FD-F598-4C0B-A5D5-C122E9482EA8\2497F4DE-E9FA-4204-80E4-4B75C46419C0
id. If this id is stable (ie you have the same one), then, we could retrieve the localized name of the service like this:
$name = (Get-VMIntegrationService -VMName DockerDesktopVM | Where-Object {$_.Id -eq "Microsoft:A43838FD-F598-4C0B-A5D5-C122E9482EA8\2497F4DE-E9FA-4204-80E4-4B75C46419C0" }).Name
I'm not on my work laptop right now, so won't test this until monday, but I'll probably give it a try (this would give us a script that works across borders; great in times of confinement 😏 )!
So, I double-checked on another computer, and it is not that easy: the id must be parsed...
- On my first machine the id was:
Microsoft:92E819AC-4AF5-4417-8227-65251A9303F7\2497F4DE-E9FA-4204-80E4-4B75C46419C0
- On the second one:
Microsoft:A43838FD-F598-4C0B-A5D5-C122E9482EA8\2497F4DE-E9FA-4204-80E4-4B75C46419C0
Only Microsoft
and the Guid after the \
are common.
On my machine: Microsoft:E43C372E-23C2-41A6-89E7-5FBF3A080CAC\2497F4DE-E9FA-4204-80E4-4B75C46419C0
Making the script respect locale seems doable.
Yes, this seems to work:
$name = (Get-VMIntegrationService -VMName DockerDesktopVM | Where-Object { $_.Id.StartsWith("Microsoft:") -and $_.Id.EndsWith("\2497F4DE-E9FA-4204-80E4-4B75C46419C0") }).Name
Nice. Good job. I've updated the original gist. Thank you. :)
👍
By the way, I upgraded my Docker for Windows to version 2.2.0.5 and it seems to be much better at resyncing time after the PC went to hibernation. Anyway the script may still prove useful if there are cases when sync fails!
👌