Skip to content

Commit b819896

Browse files
committed
First commit
0 parents  commit b819896

17 files changed

Lines changed: 1281 additions & 0 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DSCResource.Tests
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
<#
2+
.SYNOPSIS
3+
Tests if the current machine is a Nano server.
4+
#>
5+
function Test-IsNanoServer
6+
{
7+
[OutputType([Boolean])]
8+
[CmdletBinding()]
9+
param ()
10+
11+
return $PSVersionTable.PSEdition -ieq 'Core'
12+
}
13+
14+
<#
15+
.SYNOPSIS
16+
Creates and throws an invalid argument exception
17+
18+
.PARAMETER Message
19+
The message explaining why this error is being thrown
20+
21+
.PARAMETER ArgumentName
22+
The name of the invalid argument that is causing this error to be thrown
23+
#>
24+
function New-InvalidArgumentException
25+
{
26+
[CmdletBinding()]
27+
param
28+
(
29+
[Parameter(Mandatory = $true)]
30+
[ValidateNotNullOrEmpty()]
31+
[String]
32+
$Message,
33+
34+
[Parameter(Mandatory = $true)]
35+
[ValidateNotNullOrEmpty()]
36+
[String]
37+
$ArgumentName
38+
)
39+
40+
$argumentException = New-Object -TypeName 'ArgumentException' -ArgumentList @( $Message,
41+
$ArgumentName )
42+
$newObjectParams = @{
43+
TypeName = 'System.Management.Automation.ErrorRecord'
44+
ArgumentList = @( $argumentException, $ArgumentName, 'InvalidArgument', $null )
45+
}
46+
$errorRecord = New-Object @newObjectParams
47+
48+
throw $errorRecord
49+
}
50+
51+
<#
52+
.SYNOPSIS
53+
Creates and throws an invalid operation exception
54+
55+
.PARAMETER Message
56+
The message explaining why this error is being thrown
57+
58+
.PARAMETER ErrorRecord
59+
The error record containing the exception that is causing this terminating error
60+
#>
61+
function New-InvalidOperationException
62+
{
63+
[CmdletBinding()]
64+
param
65+
(
66+
[ValidateNotNullOrEmpty()]
67+
[String]
68+
$Message,
69+
70+
[ValidateNotNull()]
71+
[System.Management.Automation.ErrorRecord]
72+
$ErrorRecord
73+
)
74+
75+
if ($null -eq $Message)
76+
{
77+
$invalidOperationException = New-Object -TypeName 'InvalidOperationException'
78+
}
79+
elseif ($null -eq $ErrorRecord)
80+
{
81+
$invalidOperationException =
82+
New-Object -TypeName 'InvalidOperationException' -ArgumentList @( $Message )
83+
}
84+
else
85+
{
86+
$invalidOperationException =
87+
New-Object -TypeName 'InvalidOperationException' -ArgumentList @( $Message,
88+
$ErrorRecord.Exception )
89+
}
90+
91+
$newObjectParams = @{
92+
TypeName = 'System.Management.Automation.ErrorRecord'
93+
ArgumentList = @( $invalidOperationException.ToString(), 'MachineStateIncorrect',
94+
'InvalidOperation', $null )
95+
}
96+
$errorRecordToThrow = New-Object @newObjectParams
97+
throw $errorRecordToThrow
98+
}
99+
100+
<#
101+
.SYNOPSIS
102+
Retrieves the localized string data based on the machine's culture.
103+
Falls back to en-US strings if the machine's culture is not supported.
104+
105+
.PARAMETER ResourceName
106+
The name of the resource as it appears before '.strings.psd1' of the localized string file.
107+
108+
For example:
109+
For WindowsOptionalFeature: MSFT_xWindowsOptionalFeature
110+
For Service: MSFT_xServiceResource
111+
For Registry: MSFT_xRegistryResource
112+
#>
113+
function Get-LocalizedData
114+
{
115+
[CmdletBinding()]
116+
param
117+
(
118+
[Parameter(Mandatory = $true)]
119+
[ValidateNotNullOrEmpty()]
120+
[String]
121+
$ResourceName
122+
)
123+
124+
$resourceDirectory = (Join-Path -Path $PSScriptRoot -ChildPath $ResourceName)
125+
$localizedStringFileLocation = Join-Path -Path $resourceDirectory -ChildPath $PSUICulture
126+
127+
if (-not (Test-Path -Path $localizedStringFileLocation))
128+
{
129+
# Fallback to en-US
130+
$localizedStringFileLocation = Join-Path -Path $resourceDirectory -ChildPath 'en-US'
131+
}
132+
133+
Import-LocalizedData `
134+
-BindingVariable 'localizedData' `
135+
-FileName "$ResourceName.strings.psd1" `
136+
-BaseDirectory $localizedStringFileLocation
137+
138+
return $localizedData
139+
}
140+
141+
Export-ModuleMember -Function @( 'Test-IsNanoServer', 'New-InvalidArgumentException',
142+
'New-InvalidOperationException', 'Get-LocalizedData' )
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[ClassVersion("1.0.0.0"),FriendlyName("ReplaceString")]
2+
class MSFT_ReplaceString : OMI_BaseResource
3+
{
4+
[Key, Description("The path to the text file to replace the string in.")] String Path;
5+
[Key, Description("The RegEx string to use to search the text file.")] String Search;
6+
[Write, Description("Specifies the value type to use as the replacement string."),ValueMap{"Text", "Password"},Values{"Text", "Password"}] string Type;
7+
[Write, Description("The text to replace the text identifed by the RegEx. Only used when Type is set to 'Text'.")] String Text;
8+
[write, Description("The password to replace the text identified by the RegEx. Only used when Type is set to 'Password'."),EmbeddedInstance("MSFT_Credential")] string Password;
9+
};
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
Set-StrictMode -Version 'Latest'
2+
3+
Import-Module `
4+
-Name (Join-Path `
5+
-Path (Split-Path -Path $PSScriptRoot -Parent) `
6+
-ChildPath 'CommonResourceHelper.psm1')
7+
$script:localizedData = Get-LocalizedData -ResourceName 'MSFT_ReplaceText'
8+
9+
<#
10+
.SYNOPSIS
11+
Retrieves the current state of the text file.
12+
13+
.PARAMETER Path
14+
The path to the text file to replace the string in.
15+
16+
.PARAMETER Search
17+
The RegEx string to use to search the text file.
18+
#>
19+
function Get-TargetResource
20+
{
21+
[OutputType([Hashtable])]
22+
[CmdletBinding()]
23+
param
24+
(
25+
[Parameter(Mandatory = $true)]
26+
[ValidateNotNullOrEmpty()]
27+
[String]
28+
$Path,
29+
30+
[Parameter(Mandatory = $true)]
31+
[ValidateNotNullOrEmpty()]
32+
[String]
33+
$Search
34+
)
35+
36+
}
37+
38+
<#
39+
.SYNOPSIS
40+
Replaces text the matches the RegEx in the file.
41+
42+
.PARAMETER Path
43+
The path to the text file to replace the string in.
44+
45+
.PARAMETER Search
46+
The RegEx string to use to search the text file.
47+
48+
.PARAMETER Type
49+
Specifies the value type to use as the replacement string.
50+
51+
.PARAMETER Text
52+
The text to replace the text identifed by the RegEx.
53+
Only used when Type is set to 'Text'.
54+
55+
.PARAMETER Password
56+
The password to replace the text identified by the RegEx.
57+
Only used when Type is set to 'Password'.
58+
#>
59+
function Set-TargetResource
60+
{
61+
[CmdletBinding()]
62+
param
63+
(
64+
[Parameter(Mandatory = $true)]
65+
[ValidateNotNullOrEmpty()]
66+
[String]
67+
$Path,
68+
69+
[Parameter(Mandatory = $true)]
70+
[ValidateNotNullOrEmpty()]
71+
[String]
72+
$Search,
73+
74+
[ValidateSet('Text', 'Password')]
75+
[String]
76+
$Type = 'Text',
77+
78+
[ValidateNotNullOrEmpty()]
79+
[String]
80+
$Text,
81+
82+
[ValidateNotNullOrEmpty()]
83+
[System.Management.Automation.PSCredential]
84+
[System.Management.Automation.Credential()]
85+
$Password
86+
)
87+
}
88+
89+
<#
90+
.SYNOPSIS
91+
Tests if any text in the file matches the RegEx.
92+
93+
.PARAMETER Path
94+
The path to the text file to replace the string in.
95+
96+
.PARAMETER Search
97+
The RegEx string to use to search the text file.
98+
99+
.PARAMETER Type
100+
Specifies the value type to use as the replacement string.
101+
102+
.PARAMETER Text
103+
The text to replace the text identifed by the RegEx.
104+
Only used when Type is set to 'Text'.
105+
106+
.PARAMETER Password
107+
The password to replace the text identified by the RegEx.
108+
Only used when Type is set to 'Password'.
109+
#>
110+
function Test-TargetResource
111+
{
112+
[OutputType([Boolean])]
113+
[CmdletBinding()]
114+
param
115+
(
116+
[Parameter(Mandatory = $true)]
117+
[ValidateNotNullOrEmpty()]
118+
[String]
119+
$Path,
120+
121+
[Parameter(Mandatory = $true)]
122+
[ValidateNotNullOrEmpty()]
123+
[String]
124+
$Search,
125+
126+
[ValidateSet('Text', 'Password')]
127+
[String]
128+
$Type = 'Text',
129+
130+
[ValidateNotNullOrEmpty()]
131+
[String]
132+
$Text,
133+
134+
[ValidateNotNullOrEmpty()]
135+
[System.Management.Automation.PSCredential]
136+
[System.Management.Automation.Credential()]
137+
$Password
138+
)
139+
}
140+
141+
Export-ModuleMember -Function '*-TargetResource'
Binary file not shown.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Localized resources for MSFT_ReplaceText
2+
3+
ConvertFrom-StringData @'
4+
'@
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[ClassVersion("1.0.0.0"),FriendlyName("ReplaceText")]
2+
class MSFT_ReplaceText : OMI_BaseResource
3+
{
4+
[Key, Description("The path to the text file to replace the string in.")] String Path;
5+
[Key, Description("The RegEx string to use to search the text file.")] String Search;
6+
[Write, Description("Specifies the value type to use as the replacement string."),ValueMap{"Text", "Password"},Values{"Text", "Password"}] string Type;
7+
[Write, Description("The text to replace the text identifed by the RegEx. Only used when Type is set to 'Text'.")] String Text;
8+
[write, Description("The password to replace the text identified by the RegEx. Only used when Type is set to 'Password'."),EmbeddedInstance("MSFT_Credential")] string Password;
9+
};

0 commit comments

Comments
 (0)