Skip to content

Commit 75aebd7

Browse files
committed
Include Microsoft.DSC.Extension/PowerShell
1 parent 87da4d0 commit 75aebd7

8 files changed

+98
-12
lines changed

build.ps1

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ $filesForWindowsPackage = @(
6363
'osinfo.exe',
6464
'osinfo.dsc.resource.json',
6565
'powershell.dsc.resource.json',
66-
'windowspowershell.dsc.extension.json'
66+
'powershell.dsc.extension.json',
6767
'psDscAdapter/',
6868
'psscript.ps1',
6969
'psscript.dsc.resource.json',
@@ -77,6 +77,7 @@ $filesForWindowsPackage = @(
7777
'sshdconfig.exe',
7878
'sshd-windows.dsc.resource.json',
7979
'sshd_config.dsc.resource.json',
80+
'windowspowershell.dsc.extension.json',
8081
'windowspowershell.dsc.resource.json',
8182
'wmi.dsc.resource.json',
8283
'wmi.resource.ps1',
@@ -88,6 +89,9 @@ $filesForWindowsPackage = @(
8889

8990
$filesForLinuxPackage = @(
9091
'bicep.dsc.extension.json',
92+
'convert-resource.ps1',
93+
'convertDscResource.psd1',
94+
'convertDscResource.psm1',
9195
'dsc',
9296
'dsc_default.settings.json',
9397
'dsc.settings.json',
@@ -102,6 +106,7 @@ $filesForLinuxPackage = @(
102106
'osinfo',
103107
'osinfo.dsc.resource.json',
104108
'powershell.dsc.resource.json',
109+
'powershell.dsc.extension.json',
105110
'psDscAdapter/',
106111
'psscript.ps1',
107112
'psscript.dsc.resource.json',
@@ -113,6 +118,9 @@ $filesForLinuxPackage = @(
113118

114119
$filesForMacPackage = @(
115120
'bicep.dsc.extension.json',
121+
'convert-resource.ps1',
122+
'convertDscResource.psd1',
123+
'convertDscResource.psm1',
116124
'dsc',
117125
'dsc_default.settings.json',
118126
'dsc.settings.json',
@@ -127,6 +135,7 @@ $filesForMacPackage = @(
127135
'osinfo',
128136
'osinfo.dsc.resource.json',
129137
'powershell.dsc.resource.json',
138+
'powershell.dsc.extension.json',
130139
'psDscAdapter/',
131140
'psscript.ps1',
132141
'psscript.dsc.resource.json',

extensions/powershell/convertDscResource.psd1

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@
2424
# Description of the functionality provided by this module
2525
Description = 'PowerShell Desired State Configuration Module for converting DSC Resources to JSON format'
2626

27-
# Minimum version of the Windows PowerShell engine required by this module
28-
PowerShellVersion = '5.1'
29-
3027
# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
3128
FunctionsToExport = @(
3229
'Write-DscTrace'

extensions/powershell/convertDscResource.psm1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ $Script:IsPowerShellCore = $PSVersionTable.PSEdition -eq 'Core'
22

33
if ($Script:IsPowerShellCore) {
44
if ($IsWindows) {
5-
Import-Module -Name 'PSDesiredStateConfiguration' -RequiredVersion 1.1 -UseWindowsPowerShell -WarningAction SilentlyContinue
5+
Import-Module -Name 'PSDesiredStateConfiguration' -RequiredVersion 1.1 -UseWindowsPowerShell -WarningAction SilentlyContinue -ErrorAction Stop
66
}
7-
Import-Module -Name 'PSDesiredStateConfiguration' -MinimumVersion 2.0.7 -Prefix 'Pwsh' -WarningAction SilentlyContinue
8-
} else {
9-
Import-Module -Name 'PSDesiredStateConfiguration' -RequiredVersion 1.1 -WarningAction SilentlyContinue
7+
Import-Module -Name 'PSDesiredStateConfiguration' -MinimumVersion 2.0.7 -Prefix 'Pwsh' -WarningAction SilentlyContinue -ErrorAction Stop
8+
} else {
9+
Import-Module -Name 'PSDesiredStateConfiguration' -RequiredVersion 1.1 -WarningAction SilentlyContinue -ErrorAction Stop
1010
}
1111

1212
function Write-DscTrace {

extensions/powershell/copy_files.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
powershell.dsc.extension.json
12
windowspowershell.dsc.extension.json
23
convert-resource.ps1
34
convertDscResource.psd1
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"$schema": "https://aka.ms/dsc/schemas/v3/bundled/resource/manifest.json",
3+
"type": "Microsoft.DSC.Extension/PowerShell",
4+
"version": "0.1.0",
5+
"description": "Enable passing PowerShell v2 configuration document file directly to DSC. Requires PSDesiredStateConfiguration v2.0.7.",
6+
"import": {
7+
"fileExtensions": ["ps1"],
8+
"executable": "pwsh",
9+
"args": [
10+
"-NoLogo",
11+
"-NonInteractive",
12+
"-NoProfile",
13+
"-ExecutionPolicy",
14+
"Bypass",
15+
"-Command",
16+
"Get-Content",
17+
{
18+
"fileArg": ""
19+
},
20+
"| ./convert-resource.ps1"
21+
]
22+
}
23+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# Licensed under the MIT License.
3+
4+
BeforeDiscovery {
5+
if ($IsWindows) {
6+
$identity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
7+
$principal = [System.Security.Principal.WindowsPrincipal]::new($identity)
8+
$isElevated = $principal.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)
9+
}
10+
}
11+
12+
Describe 'PowerShell extension tests' {
13+
It 'Example PowerShell file should work' -Skip:(!$IsWindows -or !$isElevated) {
14+
$powerShellConfiguration = @"
15+
configuration TestClassConfiguration {
16+
Import-DscResource -ModuleName TestClassResource
17+
Node localhost
18+
{
19+
TestClassResource TestClass {
20+
Name = 'Test'
21+
}
22+
}
23+
}
24+
"@
25+
$config_path = "$TestDrive/testclass.ps1"
26+
$powerShellConfiguration | Set-Content -Path $config_path
27+
$out = dsc -l trace config get -f $config_path 2>$TestDrive/error.log | ConvertFrom-Json
28+
$LASTEXITCODE | Should -Be 0 -Because (Get-Content -Path $TestDrive/error.log -Raw | Out-String)
29+
$out.results[0].result.actualState.Ensure | Should -Be 'Present'
30+
$out.results[0].result.actualState.Name | Should -Be 'Test'
31+
$config_path = $config_path.ToString().Replace('\', '\\')
32+
(Get-Content -Path $TestDrive/error.log -Raw) | Should -Match "Importing file '$config_path' with extension 'Microsoft.DSC.Extension/PowerShell'"
33+
}
34+
35+
It 'Invalid PowerShell configuration document file returns error' -Skip:(!$IsWindows) {
36+
$psFile = "$TestDrive/invalid.ps1"
37+
Set-Content -Path $psFile -Value @"
38+
configuration InvalidConfiguration {
39+
Import-DscResource -ModuleName InvalidModule
40+
Node localhost
41+
{
42+
Test Invalid {
43+
Name = 'InvalidTest'
44+
Ensure = 'Present'
45+
}
46+
}
47+
}
48+
"@
49+
dsc -l trace config get -f $psFile 2>$TestDrive/error.log
50+
$LASTEXITCODE | Should -Be 2 -Because (Get-Content -Path $TestDrive/error.log -Raw | Out-String)
51+
$content = (Get-Content -Path $TestDrive/error.log -Raw)
52+
$psFile = $psFile.ToString().Replace('\', '\\')
53+
$content | Should -Match "Importing file '$psFile' with extension 'Microsoft.DSC.Extension/PowerShell'"
54+
$content | Should -Match "No DSC resources found in the imported modules."
55+
}
56+
}

extensions/powershell/win_powershell.tests.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Describe 'PowerShell extension tests' {
1616
$LASTEXITCODE | Should -Be 0 -Because (Get-Content -Path $TestDrive/error.log -Raw | Out-String)
1717
$out.results[0].result.actualState.Ensure | Should -Be 'Absent'
1818
$psFile = $psFile.ToString().Replace('\', '\\')
19-
(Get-Content -Path $TestDrive/error.log -Raw) | Should -Match "Importing file '$psFile' with extension 'Microsoft.DSC.Extension/WindowsPowerShell'"
19+
(Get-Content -Path $TestDrive/error.log -Raw) | Should -Match "Importing file '$psFile' with extension 'Microsoft.Windows.Extension/WindowsPowerShell'"
2020
}
2121

2222
It 'Invalid PowerShell configuration document file returns error' -Skip:(!$IsWindows) {
@@ -37,7 +37,7 @@ configuration InvalidConfiguration {
3737
$LASTEXITCODE | Should -Be 2 -Because (Get-Content -Path $TestDrive/error.log -Raw | Out-String)
3838
$content = (Get-Content -Path $TestDrive/error.log -Raw)
3939
$psFile = $psFile.ToString().Replace('\', '\\')
40-
$content | Should -Match "Importing file '$psFile' with extension 'Microsoft.DSC.Extension/WindowsPowerShell'"
40+
$content | Should -Match "Importing file '$psFile' with extension 'Microsoft.Windows.Extension/WindowsPowerShell'"
4141
$content | Should -Match "No DSC resources found in the imported modules."
4242
}
4343
}

extensions/powershell/windowspowershell.dsc.extension.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"$schema": "https://aka.ms/dsc/schemas/v3/bundled/resource/manifest.json",
3-
"type": "Microsoft.DSC.Extension/WindowsPowerShell",
3+
"type": "Microsoft.Windows.Extension/WindowsPowerShell",
44
"version": "0.1.0",
5-
"description": "Enable passing Windows PowerShell v1 configuration document file directly to DSC. Works only on Windows.",
5+
"description": "Enable passing Windows PowerShell v1 configuration document file directly to DSC. Works only on Windows and leverages the built-in PSDesiredStateConfiguration module.",
66
"import": {
77
"fileExtensions": ["ps1"],
88
"executable": "powershell",

0 commit comments

Comments
 (0)