Scheduled Tasks and Services Under a gMSA Instead of Stored Passwords

Get-ScheduledTask -TaskName 'Nightly ETL' | Select-Object -ExpandProperty Principal returns UserId: CORP\svc-etl with no password anywhere on disk — and that is the point. A stored password in a scheduled task or service is a credential that has to be rotated, backed up and eventually leaks through a GPO preference or a copied XML export. A group Managed Service Account (gMSA) removes the password from the equation entirely: Active Directory generates and rotates a 240-character key every 30 days, and the host retrieves it at runtime. If you need a domain-joined host to try this on, compare Windows VPS plans with RDP included — gMSA requires domain membership, so a workgroup-only VPS cannot use this pattern.

Know the four account types before choosing

A local virtual account (NT SERVICE\svcname) needs no configuration and is enough for a single-member server. A standalone MSA (Add-ADServiceAccount) is tied to one computer and cannot run across a farm. A gMSA works on any host in the permitted group, which is what makes it right for scheduled tasks that may fail over and for services running on more than one node.

Check what you currently have before migrating anything: Get-ScheduledTask | Where-Object { $_.Principal.UserId -notlike 'SYSTEM*' -and $_.Principal.UserId -notlike 'NT AUTHORITY*' } | Select-Object TaskPath,TaskName,@{n='User';e={$_.Principal.UserId}}. Every row is a stored password or a service account that will expire during a holiday weekend.

Prerequisites that fail silently if missing

gMSA requires a KDS root key in the domain, and the key must be at least 10 hours old before any gMSA will work. Create it with Add-KdsRootKey -EffectiveTime ((Get-Date).AddHours(-10)) in a lab, or plan a one-day gap in production. Verify with Get-KdsRootKey. On older functional levels you also need the KDS service reachable from every host that will use the account.

The host also needs the gMSA principal to be able to read its own managed password — in a modern domain that is the host’s computer account, handled automatically. In domains where PrincipalsAllowedToRetrieveManagedPassword was set to a group, the computer must be a member. Confirm on the host with Test-ADServiceAccount -Identity gmsa-etl; a False result is the single most common reason a gMSA task fails with logon failure 0x8007052E.

Create the gMSA and grant it the right to log on

Create the account with an explicit retrieval group: New-ADServiceAccount -Name gmsa-etl -DNSHostName gmsa-etl.corp.example.com -PrincipalsAllowedToRetrieveManagedPassword 'GRP-ETL-Hosts'. Then install it on the host: Install-ADServiceAccount gmsa-etl followed by Test-ADServiceAccount gmsa-etl. The account must appear in Get-ADServiceAccount -Filter * with the correct host group.

A gMSA cannot be granted rights like a user in the GUI. Add it with icacls D:\ETL /grant "CORP\gmsa-etl$:(OI)(CI)M" — note the trailing dollar sign, which is part of the account name and the most-often-omitted character in this whole procedure. For file shares, grant at the share level with Grant-SmbShareAccess as well, or the account will authenticate and still fail to write.

Convert a scheduled task without breaking the XML

Change the principal in place: $p = New-ScheduledTaskPrincipal -UserId 'CORP\gmsa-etl$' -LogonType Password -RunLevel Highest, then Set-ScheduledTask -TaskName 'Nightly ETL' -Principal $p. The LogonType Password value looks wrong for a passwordless account but is what Task Scheduler requires for a gMSA; S4U will not work here.

Watch for tasks authored in the GUI that carry <Password> in their XML. Export-ScheduledTask -TaskName 'Nightly ETL' shows the raw definition; strip any stored credential element before re-registering, or the task keeps the old secret alongside the new principal. Confirm afterwards with (Get-ScheduledTask -TaskName 'Nightly ETL').Principal.LogonType returning Password and the UserId ending in $.

Convert a Windows service

Set the service account with sc.exe config "MyAgent" obj= "CORP\gmsa-etl$" password= "". The empty password is correct for gMSA and must be present as an empty quoted string, otherwise sc complains about an invalid parameter. Then grant the account the log-on-as-service right; it is usually inherited through SeServiceLogonRight on the host group policy, but verify with whoami /priv from a process running under the account.

Restart the service and check the System log for 7034 or 7000. If the service starts and then cannot reach a SQL instance or a UNC path, the failure is in the target’s permissions, not in the logon — the account authenticated successfully or you would have seen 7000 instead. Adding the gMSA to a database is done with CREATE LOGIN [CORP\gmsa-etl$] FROM WINDOWS, with the same trailing dollar sign.

Test the rotation before you trust it

The managed password changes every 30 days without any action, so the thing to test is resilience: reboot the host, confirm the task and service start without a manual logon, then check for 4625 failures in the Security log. A gMSA that works until the first reboot is a gMSA whose host is not in the retrieval group, and the failure appears hours after the change was declared done.

Also verify the account is not accidentally a member of a privileged group. gMSA accounts can be added to groups like Domain Admins, and that is a serious escalation path because any permitted host can impersonate them. Run Get-ADPrincipalGroupMembership 'gmsa-etl$' and keep the result to the minimum set.

When gMSA is the wrong answer

If the host is not domain-joined — common on small VPS instances — gMSA is unavailable, and a local virtual account or a tightly scoped local account is the practical choice. If the task must reach a resource in another forest without a trust, a certificate-based credential or a delegated service account is often simpler than extending gMSA across the boundary.

For everything else, gMSA is the lowest-maintenance option available: no stored secret, no rotation ticket, no credential in a GPO preference file. Review the full Windows Server specs and pricing to size a host that will run these services and keep it domain-joined, then audit the remaining stored-password principals and move them one at a time.

Leave a Comment