Skip to content

Commit 90580c5

Browse files
committed
Enhancement #15: Name validation added.
Added validation for name length and illegal characters. Includes new test cases for same.
1 parent fd98a90 commit 90580c5

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

DSCResources/MSFT_xComputer/MSFT_xComputer.psm1

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ function Get-TargetResource
99
param
1010
(
1111
[parameter(Mandatory)]
12+
[ValidateLength(1,15)]
13+
[ValidateScript({$_ -inotmatch'[\/\\:*?"<>|]' })]
1214
[string] $Name,
1315

1416
[string] $DomainName,
@@ -39,6 +41,8 @@ function Set-TargetResource
3941
param
4042
(
4143
[parameter(Mandatory)]
44+
[ValidateLength(1,15)]
45+
[ValidateScript({$_ -inotmatch'[\/\\:*?"<>|]' })]
4246
[string] $Name,
4347

4448
[string] $DomainName,
@@ -181,6 +185,8 @@ function Test-TargetResource
181185
param
182186
(
183187
[parameter(Mandatory)]
188+
[ValidateLength(1,15)]
189+
[ValidateScript({$_ -inotmatch'[\/\\:*?"<>|]' })]
184190
[string] $Name,
185191

186192
[PSCredential]$Credential,
@@ -191,6 +197,8 @@ function Test-TargetResource
191197

192198
[string] $WorkGroupName
193199
)
200+
201+
Write-Verbose -Message "Validate desired Name is a valid name"
194202

195203
Write-Verbose -Message "Checking if computer name is $Name"
196204
if ($Name -ne $env:COMPUTERNAME) {return $false}

Tests/xComputermanagement.Tests.ps1

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ InModuleScope MSFT_xComputer {
8282
Mock GetComputerDomain {'contoso.com'}
8383
Test-TargetResource -Name $Env:ComputerName -WorkGroupName 'Contoso' -Credential $Credential -UnjoinCredential $Credential | Should Be $false
8484
}
85+
It 'Throws if name is to long' {
86+
{Test-TargetResource -Name "ThisNameIsTooLong"} | Should Throw
87+
}
88+
It 'Throws if name contains illigal characters' {
89+
{Test-TargetResource -Name "ThisIsBad<>"} | Should Throw
90+
}
8591

8692
}
8793
Context Get-TargetResource {
@@ -93,6 +99,12 @@ InModuleScope MSFT_xComputer {
9399
$Result.GetType().Fullname | Should Be 'System.Collections.Hashtable'
94100
$Result.Keys | Should Be @('Name','DomainName','Credential','UnjoinCredential','WorkGroupName')
95101
}
102+
It 'Throws if name is to long' {
103+
{Get-TargetResource -Name "ThisNameIsTooLong"} | Should Throw
104+
}
105+
It 'Throws if name contains illigal characters' {
106+
{Get-TargetResource -Name "ThisIsBad<>"} | Should Throw
107+
}
96108
}
97109
Context Set-TargetResource {
98110
Mock Rename-Computer {}
@@ -171,6 +183,12 @@ InModuleScope MSFT_xComputer {
171183
Assert-MockCalled -CommandName Rename-Computer -Exactly 1 -Scope It
172184
Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It
173185
}
186+
It 'Throws if name is to long' {
187+
{Set-TargetResource -Name "ThisNameIsTooLong"} | Should Throw
188+
}
189+
It 'Throws if name contains illigal characters' {
190+
{Set-TargetResource -Name "ThisIsBad<>"} | Should Throw
191+
}
174192
}
175193
}
176194
}

0 commit comments

Comments
 (0)