Skip to content

Commit 582303a

Browse files
authored
Fix Get-DomainName - retrieve computer NETBIOS domain name instead of user (#444)
1 parent 2f94178 commit 582303a

File tree

3 files changed

+12
-13
lines changed

3 files changed

+12
-13
lines changed

CHANGELOG.md

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

88
### Fixed
99

10+
- Computer
11+
- Fix Get-ComputerDomain function to retrieve the computer NETBIOS domain name instead of the user.
1012
- `UserAccountControl`
1113
- Tests remove parameter type on `Assert-BoundParameter` mocks.
1214

source/DSCResources/DSC_Computer/DSC_Computer.psm1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ function Get-ComputerDomain
630630
{
631631
if ($NetBios)
632632
{
633-
$domainName = (Get-Item -Path Env:\USERDOMAIN).Value
633+
$domainName = (Get-CimInstance -ClassName Win32_NTDomain -Filter "DnsForestName='$($domainInfo.Domain)'").DomainName
634634
}
635635
else
636636
{

tests/Unit/DSC_Computer.Tests.ps1

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1707,17 +1707,17 @@ Describe 'DSC_Computer\Get-ComputerDomain' -Tag 'Private' {
17071707
}
17081708
}
17091709

1710-
Mock -CommandName Get-Item -ParameterFilter { $Path -eq 'Env:\USERDOMAIN' } -MockWith {
1710+
Mock -CommandName Get-CimInstance -ParameterFilter { $ClassName -eq 'Win32_NTDomain' } -MockWith {
17111711
[PSCustomObject] @{
1712-
Value = 'CONTOSO'
1712+
DomainName = 'CONTOSO'
17131713
}
17141714
}
17151715
}
17161716

17171717
BeforeDiscovery {
17181718
$testCases = @(
1719-
@{ value = $true; result = 'CONTOSO'; GetItemCount = 1 }
1720-
@{ value = $false; result = 'contoso.com'; GetItemCount = 0 }
1719+
@{ value = $true; result = 'CONTOSO'; GetCimInstanceCount = 2 }
1720+
@{ value = $false; result = 'contoso.com'; GetCimInstanceCount = 1 }
17211721
)
17221722
}
17231723

@@ -1733,8 +1733,7 @@ Describe 'DSC_Computer\Get-ComputerDomain' -Tag 'Private' {
17331733
Get-ComputerDomain @getComputerDomainParameters | Should -Be $result
17341734
}
17351735

1736-
Should -Invoke -CommandName Get-CimInstance -Exactly -Times 1 -Scope It
1737-
Should -Invoke -CommandName Get-Item -Exactly -Times $GetItemCount -Scope It
1736+
Should -Invoke -CommandName Get-CimInstance -Exactly -Times $GetCimInstanceCount -Scope It
17381737
}
17391738
}
17401739

@@ -1747,9 +1746,9 @@ Describe 'DSC_Computer\Get-ComputerDomain' -Tag 'Private' {
17471746
}
17481747
}
17491748

1750-
Mock -CommandName Get-Item -ParameterFilter { $Path -eq 'Env:\USERDOMAIN' } -MockWith {
1749+
Mock -CommandName Get-CimInstance -ParameterFilter { $ClassName -eq 'Win32_NTDomain' } -MockWith {
17511750
[PSCustomObject] @{
1752-
Value = 'CONTOSO'
1751+
DomainName = 'CONTOSO'
17531752
}
17541753
}
17551754
}
@@ -1761,7 +1760,6 @@ Describe 'DSC_Computer\Get-ComputerDomain' -Tag 'Private' {
17611760
}
17621761

17631762
Should -Invoke -CommandName Get-CimInstance -Exactly -Times 1 -Scope It
1764-
Should -Invoke -CommandName Get-Item -Exactly -Times 0 -Scope It
17651763
}
17661764
}
17671765

@@ -1782,9 +1780,9 @@ Describe 'DSC_Computer\Get-ComputerDomain' -Tag 'Private' {
17821780
}
17831781
}
17841782

1785-
Mock -CommandName Get-Item -ParameterFilter { $Path -eq 'Env:\USERDOMAIN' } -MockWith {
1783+
Mock -CommandName Get-CimInstance -ParameterFilter { $ClassName -eq 'Win32_NTDomain' } -MockWith {
17861784
[PSCustomObject] @{
1787-
Value = 'Computer1'
1785+
DomainName = $null
17881786
}
17891787
}
17901788
}
@@ -1801,7 +1799,6 @@ Describe 'DSC_Computer\Get-ComputerDomain' -Tag 'Private' {
18011799
}
18021800

18031801
Should -Invoke -CommandName Get-CimInstance -Exactly -Times 1 -Scope It
1804-
Should -Invoke -CommandName Get-Item -Exactly -Times 0 -Scope It
18051802
}
18061803
}
18071804
}

0 commit comments

Comments
 (0)