Skip to content

Commit 6c3f2fd

Browse files
authored
Fix mismatch between user and builtinaccount (#387)
1 parent 3aea926 commit 6c3f2fd

3 files changed

Lines changed: 47 additions & 15 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
55

66
## [Unreleased]
77

8+
### Fixed
9+
10+
- ScheduledTask
11+
- No longer conflates resource parameter `BuiltInAccount` and `*-ScheduledTask` parameter `user` - Fixes [Issue #385](https://github.com/dsccommunity/ComputerManagementDsc/issues/385)
12+
813
### Added
914

1015
- Computer

source/DSCResources/DSC_ScheduledTask/DSC_ScheduledTask.psm1

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,7 @@ function Set-TargetResource
792792
non-null value to be 'LOCAL SERVICE', 'NETWORK SERVICE' or
793793
'SYSTEM'
794794
#>
795-
$username = 'NT AUTHORITY\' + $BuiltInAccount
795+
$username = Set-DomainNameInAccountName -AccountName $BuiltInAccount -DomainName 'NT AUTHORITY'
796796
$registerArguments.Add('User', $username)
797797
$LogonType = 'ServiceAccount'
798798
}
@@ -804,7 +804,6 @@ function Set-TargetResource
804804
elseif ($PSBoundParameters.ContainsKey('ExecuteAsCredential'))
805805
{
806806
$username = $ExecuteAsCredential.UserName
807-
808807
# If the LogonType is not specified then set it to password
809808
if ([System.String]::IsNullOrEmpty($LogonType))
810809
{
@@ -829,7 +828,7 @@ function Set-TargetResource
829828
privileges, should we default to 'NT AUTHORITY\LOCAL SERVICE'
830829
instead?
831830
#>
832-
$username = 'NT AUTHORITY\SYSTEM'
831+
$username = Set-DomainNameInAccountName -AccountName 'SYSTEM' -DomainName 'NT AUTHORITY'
833832
$registerArguments.Add('User', $username)
834833
$LogonType = 'ServiceAccount'
835834
}
@@ -1423,16 +1422,17 @@ function Test-TargetResource
14231422

14241423
if ($PSBoundParameters.ContainsKey('BuiltInAccount'))
14251424
{
1426-
$PSBoundParameters.User = $BuiltInAccount
1427-
$currentValues.User = $BuiltInAccount
1425+
$user = Set-DomainNameInAccountName -AccountName 'SYSTEM' -DomainName 'NT AUTHORITY'
1426+
$PSBoundParameters.User = $user
1427+
$currentValues.User = $user
14281428

14291429
$PSBoundParameters.ExecuteAsCredential = $BuiltInAccount
14301430
$currentValues.ExecuteAsCredential = $BuiltInAccount
14311431

14321432
$PSBoundParameters['LogonType'] = 'ServiceAccount'
14331433
$currentValues['LogonType'] = 'ServiceAccount'
14341434

1435-
$PSBoundParameters['BuiltInAccount'] = 'NT AUTHORITY\' + $BuiltInAccount
1435+
$PSBoundParameters['BuiltInAccount'] = $BuiltInAccount
14361436
}
14371437
elseif ($PSBoundParameters.ContainsKey('ExecuteAsCredential'))
14381438
{
@@ -1464,6 +1464,16 @@ function Test-TargetResource
14641464
}
14651465
else
14661466
{
1467+
$user = Set-DomainNameInAccountName -AccountName 'SYSTEM' -DomainName 'NT AUTHORITY'
1468+
$PSBoundParameters.User = $user
1469+
$currentValues.User = $user
1470+
1471+
$PSBoundParameters.ExecuteAsCredential = 'SYSTEM'
1472+
$currentValues.ExecuteAsCredential = 'SYSTEM'
1473+
1474+
$PSBoundParameters.Add('BuiltInAccount', $BuiltInAccount)
1475+
$currentValues.BuiltInAccount = $BuiltInAccount
1476+
14671477
# Must be running as System, login type is ServiceAccount
14681478
$PSBoundParameters['LogonType'] = 'ServiceAccount'
14691479
$currentValues['LogonType'] = 'ServiceAccount'
@@ -1915,9 +1925,15 @@ function Get-CurrentResource
19151925
Delay = ConvertTo-TimeSpanStringFromScheduledTaskString -TimeSpan $trigger.Delay
19161926
}
19171927

1918-
if (($result.ContainsKey('LogonType')) -and ($result['LogonType'] -ieq 'ServiceAccount'))
1928+
if (
1929+
(($result.ContainsKey('LogonType')) -and ($result['LogonType'] -ieq 'ServiceAccount')) -or
1930+
$result.Principal.UserID -in @('SYSTEM', 'LOCAL SERVICE', 'NETWORK SERVICE')
1931+
)
19191932
{
1920-
$builtInAccount = Set-DomainNameInAccountName -AccountName $task.Principal.UserId -DomainName 'NT AUTHORITY'
1933+
$result.User = Set-DomainNameInAccountName `
1934+
-AccountName $task.Principal.UserId `
1935+
-DomainName 'NT AUTHORITY'
1936+
$builtInAccount = $task.Principal.UserId
19211937
$result.Add('BuiltInAccount', $builtInAccount)
19221938
}
19231939
}

tests/Unit/DSC_ScheduledTask.Tests.ps1

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1788,6 +1788,15 @@ try
17881788
}
17891789
}
17901790

1791+
$testParameters.Add('User', 'WrongUser')
1792+
1793+
It 'Should Disregard User and Set User to the BuiltInAccount' {
1794+
Set-TargetResource @testParameters
1795+
Assert-MockCalled -CommandName Register-ScheduledTask -Times 1 -Scope It -ParameterFilter {
1796+
$User -ieq ('NT AUTHORITY\' + $testParameters['BuiltInAccount'])
1797+
}
1798+
}
1799+
17911800
$testParameters.Add('LogonType', 'Password')
17921801

17931802
It 'Should overwrite LogonType to "ServiceAccount"' {
@@ -1799,14 +1808,16 @@ try
17991808

18001809
Mock -CommandName Get-ScheduledTask -MockWith {
18011810
@{
1802-
TaskName = $testParameters.TaskName
1803-
TaskPath = $testParameters.TaskPath
1804-
Actions = @(
1811+
Description = '+'
1812+
TaskName = $testParameters.TaskName
1813+
TaskPath = $testParameters.TaskPath
1814+
Actions = @(
18051815
[pscustomobject] @{
18061816
Execute = $testParameters.ActionExecutable
18071817
}
18081818
)
1809-
Triggers = @(
1819+
ActionArguments = '-File "C:\something\right.ps1"'
1820+
Triggers = @(
18101821
[pscustomobject] @{
18111822
Repetition = @{
18121823
Duration = "PT$([System.TimeSpan]::Parse($testParameters.RepetitionDuration).TotalHours)H"
@@ -1817,12 +1828,12 @@ try
18171828
}
18181829
}
18191830
)
1820-
Settings = [pscustomobject] @{
1831+
Settings = [pscustomobject] @{
18211832
Enabled = $true
18221833
MultipleInstances = 'IgnoreNew'
18231834
}
1824-
Principal = [pscustomobject] @{
1825-
UserId = 'NT AUTHORITY\' + $testParameters.BuiltInAccount
1835+
Principal = [pscustomobject] @{
1836+
UserId = $testParameters.BuiltInAccount
18261837
LogonType = 'ServiceAccount'
18271838
}
18281839
}

0 commit comments

Comments
 (0)