Skip to content

Experimental WinPowerShell DSC import extension #1025

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 45 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
5ae4e8e
Implement PowerShell import extension
Gijsreyn Jul 31, 2025
d032063
Uncomment section
Gijsreyn Jul 31, 2025
68f6c83
Add tests
Gijsreyn Aug 2, 2025
ba03899
Copy items for other OS
Gijsreyn Aug 2, 2025
0682b39
Update to Windows PowerShell
Gijsreyn Aug 3, 2025
4fe7005
Merge branch 'main' of https://github.com/Gijsreyn/operation-methods …
Gijsreyn Aug 8, 2025
fb8ba6c
Implement PowerShell import extension
Gijsreyn Jul 31, 2025
e3e43b3
Uncomment section
Gijsreyn Jul 31, 2025
0a3b5fd
Add tests
Gijsreyn Aug 2, 2025
71d475f
Copy items for other OS
Gijsreyn Aug 2, 2025
5fdc34d
Update to Windows PowerShell
Gijsreyn Aug 3, 2025
954b0e9
Merge branch 'implement-powershell-import-extension' of https://githu…
Gijsreyn Aug 8, 2025
1129edc
Fix tests
Gijsreyn Aug 8, 2025
55d4424
Update test
Gijsreyn Aug 8, 2025
2860da7
Debug agent
Gijsreyn Aug 8, 2025
4183c27
Revert to old change
Gijsreyn Aug 8, 2025
7342f16
Find PSDesiredStateConfiguration
Gijsreyn Aug 8, 2025
29da6d7
Find PSDesiredStateConfiguration
Gijsreyn Aug 8, 2025
85f237b
Merge branch 'main' into implement-powershell-import-extension
Gijsreyn Aug 10, 2025
d9ccec4
Debug PSDesiredStateConfiguration
Gijsreyn Aug 10, 2025
526ca6c
Output available modules
Gijsreyn Aug 10, 2025
f71d987
Update PSModulePath during discovery
Gijsreyn Aug 10, 2025
2d10602
Set verbosePreference
Gijsreyn Aug 10, 2025
6496881
Return result
Gijsreyn Aug 10, 2025
fe1a799
Fix test
Gijsreyn Aug 10, 2025
490402b
Remove whitespace
Gijsreyn Aug 10, 2025
f470c1d
Run test with verbose
Gijsreyn Aug 10, 2025
03ac34c
Disable test
Gijsreyn Aug 10, 2025
a0a4fba
logging
Gijsreyn Aug 10, 2025
bd99f63
Import module
Gijsreyn Aug 10, 2025
246d471
Trace one more time
Gijsreyn Aug 10, 2025
c7a2aa3
Trace second time
Gijsreyn Aug 10, 2025
a5371c8
Run line
Gijsreyn Aug 10, 2025
70ab41b
Import error
Gijsreyn Aug 10, 2025
52738e6
Remove all PowerShell modules from module path
Gijsreyn Aug 10, 2025
71f31cc
Debug lines
Gijsreyn Aug 10, 2025
1d30a2f
Debug lines
Gijsreyn Aug 10, 2025
87da4d0
Cast tests to string
Gijsreyn Aug 10, 2025
75aebd7
Include Microsoft.DSC.Extension/PowerShell
Gijsreyn Aug 10, 2025
4cca197
Fix discovery test
Gijsreyn Aug 11, 2025
6bdf559
Not working with index
Gijsreyn Aug 11, 2025
f7069cb
Revert changes pwsh
Gijsreyn Aug 11, 2025
20dc75b
Merge branch 'implement-powershell-import-extension' of https://githu…
Gijsreyn Aug 11, 2025
d46d0b2
Wrong commit id
Gijsreyn Aug 11, 2025
d353b73
Revert file
Gijsreyn Aug 11, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ $filesForWindowsPackage = @(
'sshdconfig.exe',
'sshd-windows.dsc.resource.json',
'sshd_config.dsc.resource.json',
'windowspowershell.dsc.extension.json',
'windowspowershell.dsc.resource.json',
'wmi.dsc.resource.json',
'wmi.resource.ps1',
Expand All @@ -86,7 +87,7 @@ $filesForLinuxPackage = @(
'bicep.dsc.extension.json',
'dsc',
'dsc_default.settings.json',
'dsc.settings.json'
'dsc.settings.json',
'dscecho',
'echo.dsc.resource.json',
'assertion.dsc.resource.json',
Expand All @@ -111,7 +112,7 @@ $filesForMacPackage = @(
'bicep.dsc.extension.json',
'dsc',
'dsc_default.settings.json',
'dsc.settings.json'
'dsc.settings.json',
'dscecho',
'echo.dsc.resource.json',
'assertion.dsc.resource.json',
Expand Down Expand Up @@ -316,6 +317,7 @@ if (!$SkipBuild) {
"dsc",
"dscecho",
"extensions/bicep",
"extensions/powershell"
"osinfo",
"powershell-adapter",
'resources/PSScript',
Expand Down
13 changes: 13 additions & 0 deletions dsc/examples/variable.dsc.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
configuration VariableConfiguration {
Import-DscResource -ModuleName PSDesiredStateConfiguration
Node localhost
{
Environment PathEnvironmentVariable {
Name = 'TestPathEnvironmentVariable'
Value = 'TestValue'
Ensure = 'Present'
Path = $true
Target = @('Process')
}
}
}
44 changes: 21 additions & 23 deletions dsc/tests/dsc_extension_discover.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,28 @@ Describe 'Discover extension tests' {
It 'Discover extensions' {
$out = dsc extension list | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
if ($IsWindows) {
$out.Count | Should -Be 3 -Because ($out | Out-String)
$out[0].type | Should -Be 'Microsoft.DSC.Extension/Bicep'
$out[0].version | Should -Be '0.1.0'
$out[0].capabilities | Should -BeExactly @('import')
$out[0].manifest | Should -Not -BeNullOrEmpty
$out[1].type | Should -Be 'Microsoft.Windows.Appx/Discover'
$out[1].version | Should -Be '0.1.0'
$out[1].capabilities | Should -BeExactly @('discover')
$out[1].manifest | Should -Not -BeNullOrEmpty
$out[2].type | Should -BeExactly 'Test/Discover'
$out[2].version | Should -BeExactly '0.1.0'
$out[2].capabilities | Should -BeExactly @('discover')
$out[2].manifest | Should -Not -BeNullOrEmpty
$expectedExtensions = if ($IsWindows) {
@(
@{ type = 'Microsoft.DSC.Extension/Bicep'; version = '0.1.0'; capabilities = @('import') }
@{ type = 'Microsoft.DSC.Transitional/PSDesiredStateConfiguration'; version = '0.1.0'; capabilities = @('import') }
@{ type = 'Microsoft.Windows.Appx/Discover'; version = '0.1.0'; capabilities = @('discover') }
@{ type = 'Test/Discover'; version = '0.1.0'; capabilities = @('discover') }
)
} else {
$out.Count | Should -Be 2 -Because ($out | Out-String)
$out[0].type | Should -Be 'Microsoft.DSC.Extension/Bicep'
$out[0].version | Should -Be '0.1.0'
$out[0].capabilities | Should -BeExactly @('import')
$out[0].manifest | Should -Not -BeNullOrEmpty
$out[1].type | Should -BeExactly 'Test/Discover'
$out[1].version | Should -BeExactly '0.1.0'
$out[1].capabilities | Should -BeExactly @('discover')
$out[1].manifest | Should -Not -BeNullOrEmpty
@(
@{ type = 'Microsoft.DSC.Extension/Bicep'; version = '0.1.0'; capabilities = @('import') }
@{ type = 'Test/Discover'; version = '0.1.0'; capabilities = @('discover') }
)
}

$out.Count | Should -Be $expectedExtensions.Count -Because ($out | Out-String)

foreach ($expected in $expectedExtensions) {
$extension = $out | Where-Object { $_.type -eq $expected.type }
$extension | Should -Not -BeNullOrEmpty -Because "Extension $($expected.type) should exist"
$extension.version | Should -BeExactly $expected.version
$extension.capabilities | Should -BeExactly $expected.capabilities
$extension.manifest | Should -Not -BeNullOrEmpty
}
}

Expand Down
40 changes: 40 additions & 0 deletions extensions/powershell/convert-resource.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
[CmdletBinding()]
param (
[Parameter(ValueFromPipeline = $true)]
[string[]]$stringInput
)

begin {
$lines = [System.Collections.Generic.List[string]]::new()

if ($PSVersionTable.PSEdition -ne 'Core') {
# Remove all PowerShell paths
$env:PSModulePath = ($env:PSModulePath -split ';' | Where-Object {
$_ -notmatch 'PowerShell[\\/]7' -and
$_ -notmatch 'Program Files[\\/]PowerShell[\\/]' -and
$_ -notmatch 'Documents[\\/]PowerShell[\\/]'
}) -join ';'

# Make sure the default path is Windows PowerShell is included
$winPsPath = "$env:windir\System32\WindowsPowerShell\v1.0\Modules"
if ($env:PSModulePath -notmatch [regex]::Escape($winPsPath)) {
$env:PSModulePath = $env:PSModulePath + [System.IO.Path]::PathSeparator + $winPsPath
}
}

$scriptModule = Import-Module "$PSScriptRoot/convertDscResource.psd1" -Force -PassThru -WarningAction SilentlyContinue -ErrorAction Stop
}

process {
foreach ($line in $stringInput) {
$lines.Add($line)
}
}

end {
if ($lines.Count -ne 0) {
$result = $scriptModule.invoke( { param($lines) Build-DscConfigDocument -Content $lines }, ($lines | Out-String) )

return ($result | ConvertTo-Json -Depth 10 -Compress)
}
}
47 changes: 47 additions & 0 deletions extensions/powershell/convertDscResource.psd1
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

@{

# Script module or binary module file associated with this manifest.
RootModule = 'convertDscResource.psm1'

# Version number of this module.
moduleVersion = '0.0.1'

# ID used to uniquely identify this module
GUID = '95f93fd3-34ff-417e-80e4-f0112918a0bd'

# Author of this module
Author = 'Microsoft Corporation'

# Company or vendor of this module
CompanyName = 'Microsoft Corporation'

# Copyright statement for this module
Copyright = '(c) Microsoft Corporation. All rights reserved.'

# Description of the functionality provided by this module
Description = 'PowerShell Desired State Configuration Module for converting DSC Resources to JSON format'

# 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.
FunctionsToExport = @(
'Write-DscTrace'
'Build-DscConfigDocument'
)

# Cmdlets 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 cmdlets to export.
CmdletsToExport = @()

# Variables to export from this module
VariablesToExport = @()

# Aliases 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 aliases to export.
AliasesToExport = @()

PrivateData = @{
PSData = @{
ProjectUri = 'https://github.com/PowerShell/DSC'
}
}
}
Loading
Loading