Skip to content

Commit d2f5205

Browse files
committed
Merge branch 'fix-enum-output' of https://github.com/Gijsreyn/operation-methods into fix-enum-output
2 parents e891084 + b32a979 commit d2f5205

File tree

268 files changed

+5001
-3302
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

268 files changed

+5001
-3302
lines changed

docs/get-started/index.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ Retrieving the current state of a resource is useful, but in practice you more o
235235
whether an instance is in the desired state. The `dsc resource test` command not only tells you
236236
whether an instance is out of state, but how it's out state.
237237

238-
> {!NOTE}
238+
> [!NOTE]
239239
> Remember that the `Registry` resource doesn't have the `test` capability. Fortunately, that
240240
> doesn't mean that you can't use the **Test** operation for a resource. When a resource doesn't
241241
> have the `test` capability, it is indicating that the resource depends on DSC's synthetic test
@@ -334,6 +334,9 @@ resource instances. In this configuration, it defines two instances:
334334
- The second instance uses the `PSModule` PSDSC resource from the same module to make sure that the
335335
**Microsoft.WinGet.Client** module is installed.
336336

337+
> [!NOTE]
338+
> When invoking resources from `WindowsPowerShell` adapter, Windows Remote Management (WinRM) needs to be configured properly on the system. For more information about configuring WinRM, see [Setting Up a WinRM Endpoint](/windows/win32/winrm/installation-and-configuration-for-windows-remote-management#to-configure-winrm-with-default-settings).
339+
337340
Open a terminal with elevated permissions. PSDSC resources in Windows PowerShell need to run as
338341
administrator. In the elevated terminal, change directory to the folder where you saved the
339342
configuration document as `example.dsc.config.yaml`. Then run the following command.

powershell-adapter/psDscAdapter/win_psDscAdapter.psm1

Lines changed: 64 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function Invoke-DscCacheRefresh {
5353
)
5454

5555
$refreshCache = $false
56-
56+
$namedModules = [System.Collections.Generic.List[Object]]::new()
5757
$cacheFilePath = Join-Path $env:LocalAppData "dsc\WindowsPSAdapterCache.json"
5858

5959
if (Test-Path $cacheFilePath) {
@@ -73,51 +73,53 @@ function Invoke-DscCacheRefresh {
7373
"Filtered DscResourceCache cache is empty" | Write-DscTrace
7474
}
7575
else {
76-
"Checking cache for stale entries" | Write-DscTrace
76+
"Checking cache for stale PSModulePath" | Write-DscTrace
77+
78+
$m = $env:PSModulePath -split [IO.Path]::PathSeparator | ForEach-Object { Get-ChildItem -Directory -Path $_ -Depth 1 -ErrorAction Ignore }
79+
80+
$hs_cache = [System.Collections.Generic.HashSet[string]]($cache.PSModulePaths)
81+
$hs_live = [System.Collections.Generic.HashSet[string]]($m.FullName)
82+
$hs_cache.SymmetricExceptWith($hs_live)
83+
$diff = $hs_cache
84+
85+
"PSModulePath diff '$diff'" | Write-DscTrace
86+
# TODO: Optimise for named module refresh
87+
if ($diff.Count -gt 0) {
88+
$refreshCache = $true
89+
}
7790

78-
foreach ($cacheEntry in $dscResourceCacheEntries) {
91+
if (-not $refreshCache) {
92+
"Checking cache for stale entries" | Write-DscTrace
7993

80-
$cacheEntry.LastWriteTimes.PSObject.Properties | ForEach-Object {
94+
foreach ($cacheEntry in $dscResourceCacheEntries) {
8195

82-
if (Test-Path $_.Name) {
83-
$file_LastWriteTime = (Get-Item $_.Name).LastWriteTimeUtc
84-
# Truncate DateTime to seconds
85-
$file_LastWriteTime = $file_LastWriteTime.AddTicks( - ($file_LastWriteTime.Ticks % [TimeSpan]::TicksPerSecond));
96+
foreach ($_ in $cacheEntry.LastWriteTimes.PSObject.Properties) {
8697

87-
$cache_LastWriteTime = [DateTime]$_.Value
88-
# Truncate DateTime to seconds
89-
$cache_LastWriteTime = $cache_LastWriteTime.AddTicks( - ($cache_LastWriteTime.Ticks % [TimeSpan]::TicksPerSecond));
98+
if (Test-Path $_.Name) {
99+
$file_LastWriteTime = (Get-Item $_.Name).LastWriteTime.ToFileTime()
100+
$cache_LastWriteTime = [long]$_.Value
90101

91-
if (-not ($file_LastWriteTime.Equals($cache_LastWriteTime))) {
92-
"Detected stale cache entry '$($_.Name)'" | Write-DscTrace
93-
$refreshCache = $true
102+
if ($file_LastWriteTime -ne $cache_LastWriteTime) {
103+
"Detected stale cache entry '$($_.Name)'" | Write-DscTrace
104+
$namedModules.Add($cacheEntry.DscResourceInfo.ModuleName)
105+
break
106+
}
107+
}
108+
else {
109+
"Detected non-existent cache entry '$($_.Name)'" | Write-DscTrace
110+
$namedModules.Add($cacheEntry.DscResourceInfo.ModuleName)
94111
break
95112
}
96113
}
97-
else {
98-
"Detected non-existent cache entry '$($_.Name)'" | Write-DscTrace
99-
$refreshCache = $true
100-
break
101-
}
102114
}
103-
104-
if ($refreshCache) { break }
105115
}
106-
107-
if (-not $refreshCache) {
108-
"Checking cache for stale PSModulePath" | Write-DscTrace
109-
110-
$m = $env:PSModulePath -split [IO.Path]::PathSeparator | % { Get-ChildItem -Directory -Path $_ -Depth 1 -ea SilentlyContinue }
111-
112-
$hs_cache = [System.Collections.Generic.HashSet[string]]($cache.PSModulePaths)
113-
$hs_live = [System.Collections.Generic.HashSet[string]]($m.FullName)
114-
$hs_cache.SymmetricExceptWith($hs_live)
115-
$diff = $hs_cache
116-
117-
"PSModulePath diff '$diff'" | Write-DscTrace
118-
if ($diff.Count -gt 0) {
119-
$refreshCache = $true
116+
if ($namedModules.Count -gt 0) {
117+
$refreshCache = $true
118+
if ($null -ne $Module) {
119+
$namedModules.AddRange(@($Module))
120120
}
121+
$namedModules = $namedModules | Sort-Object -Unique
122+
"Module list: $($namedModules -join ', ')" | Write-DscTrace
121123
}
122124
}
123125
}
@@ -131,32 +133,32 @@ function Invoke-DscCacheRefresh {
131133
'Constructing Get-DscResource cache' | Write-DscTrace
132134

133135
# create a list object to store cache of Get-DscResource
134-
[dscResourceCacheEntry[]]$dscResourceCacheEntries = [System.Collections.Generic.List[Object]]::new()
136+
$dscResourceCacheEntries = [System.Collections.Generic.List[dscResourceCacheEntry]]::new()
135137

136138
# improve by performance by having the option to only get details for named modules
137139
# workaround for File and SignatureValidation resources that ship in Windows
138-
if ($null -ne $module -and 'PSDesiredStateConfiguration' -ne $module) {
139-
if ($module.gettype().name -eq 'string') {
140-
$module = @($module)
141-
}
140+
if ($namedModules.Count -gt 0) {
142141
$DscResources = [System.Collections.Generic.List[Object]]::new()
143142
$Modules = [System.Collections.Generic.List[Object]]::new()
144143
$filteredResources = @()
145-
foreach ($m in $module) {
146-
$DscResources += Get-DscResource -Module $m
147-
$Modules += Get-Module -Name $m -ListAvailable
148-
149-
# Grab all DSC resources to filter out of the cache
150-
$filteredResources += $dscResources | Where-Object -Property ModuleName -NE $null | ForEach-Object { [System.String]::Concat($_.ModuleName, '/', $_.Name) }
144+
foreach ($m in $namedModules) {
145+
$DscResources.AddRange(@(Get-DscResource -Module $m))
146+
$Modules.AddRange(@(Get-Module -Name $m -ListAvailable))
151147
}
152148

149+
if ('PSDesiredStateConfiguration' -in $namedModules -and $PSVersionTable.PSVersion.Major -le 5 ) {
150+
# the resources in Windows should only load in Windows PowerShell
151+
# workaround: the binary modules don't have a module name, so we have to special case File and SignatureValidation resources that ship in Windows
152+
$DscResources.AddRange(@(Get-DscResource | Where-Object -Property ParentPath -eq "$env:windir\system32\Configuration\BaseRegistration"))
153+
$filteredResources = @(
154+
'PSDesiredStateConfiguration/File'
155+
'PSDesiredStateConfiguration/SignatureValidation'
156+
)
157+
}
158+
# Grab all DSC resources to filter out of the cache
159+
$filteredResources += $dscResources | Where-Object -Property ModuleName -NE $null | ForEach-Object { [System.String]::Concat($_.ModuleName, '/', $_.Name) }
153160
# Exclude the one module that was passed in as a parameter
154-
$existingDscResourceCacheEntries = $cache.ResourceCache | Where-Object -Property Type -NotIn $filteredResources
155-
}
156-
elseif ('PSDesiredStateConfiguration' -eq $module -and $PSVersionTable.PSVersion.Major -le 5 ) {
157-
# the resources in Windows should only load in Windows PowerShell
158-
# workaround: the binary modules don't have a module name, so we have to special case File and SignatureValidation resources that ship in Windows
159-
$DscResources = Get-DscResource | Where-Object { $_.modulename -eq 'PSDesiredStateConfiguration' -or ( $_.modulename -eq $null -and $_.parentpath -like "$env:windir\System32\Configuration\*" ) }
161+
$existingDscResourceCacheEntries = @($cache.ResourceCache | Where-Object -Property Type -NotIn $filteredResources)
160162
}
161163
else {
162164
# if no module is specified, get all resources
@@ -232,29 +234,29 @@ function Invoke-DscCacheRefresh {
232234
# fill in resource files (and their last-write-times) that will be used for up-do-date checks
233235
$lastWriteTimes = @{}
234236
Get-ChildItem -Recurse -File -Path $dscResource.ParentPath -Include "*.ps1", "*.psd1", "*.psm1", "*.mof" -ea Ignore | % {
235-
$lastWriteTimes.Add($_.FullName, $_.LastWriteTime)
237+
$lastWriteTimes.Add($_.FullName, $_.LastWriteTime.ToFileTime())
236238
}
237239

238-
$dscResourceCacheEntries += [dscResourceCacheEntry]@{
240+
$dscResourceCacheEntries.Add([dscResourceCacheEntry]@{
239241
Type = "$moduleName/$($dscResource.Name)"
240242
DscResourceInfo = $DscResourceInfo
241243
LastWriteTimes = $lastWriteTimes
244+
})
245+
}
246+
247+
if ($namedModules.Count -gt 0) {
248+
# Make sure all resource cache entries are returned
249+
foreach ($entry in $existingDscResourceCacheEntries) {
250+
$dscResourceCacheEntries.Add([dscResourceCacheEntry]$entry)
242251
}
243252
}
244253

245254
[dscResourceCache]$cache = [dscResourceCache]::new()
246-
$cache.ResourceCache = $dscResourceCacheEntries
255+
$cache.ResourceCache = $dscResourceCacheEntries.ToArray()
247256
$m = $env:PSModulePath -split [IO.Path]::PathSeparator | % { Get-ChildItem -Directory -Path $_ -Depth 1 -ea SilentlyContinue }
248257
$cache.PSModulePaths = $m.FullName
249258
$cache.CacheSchemaVersion = $script:CurrentCacheSchemaVersion
250259

251-
if ($existingDscResourceCacheEntries) {
252-
$cache.ResourceCache += $existingDscResourceCacheEntries
253-
254-
# Make sure all resource cache entries are returned
255-
$dscResourceCacheEntries = $cache.ResourceCache
256-
}
257-
258260
# save cache for future use
259261
# TODO: replace this with a high-performance serializer
260262
"Saving Get-DscResource cache to '$cacheFilePath'" | Write-DscTrace

schemas/build.ps1

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ using namespace System.Collections
44
<#
55
.SYNOPSIS
66
Build the DSC schema files from the source YAML files.
7-
7+
88
.DESCRIPTION
99
This build script composes the JSON Schema files from the source YAML files, creating new
1010
files in the specified output directory. It creates a schema registry to analyze the source
@@ -15,11 +15,11 @@ using namespace System.Collections
1515
param(
1616
[string]
1717
$OutputDirectory = "$PSScriptRoot",
18-
18+
1919
[Parameter(ParameterSetName='ByPath')]
2020
[string[]]
2121
$ConfigFilePath,
22-
22+
2323
[string[]]
2424
[ValidateSet('Json', 'JsonVSCode', 'Yaml', 'YamlVSCode')]
2525
$OutputFormat = @(
@@ -40,7 +40,7 @@ begin {
4040

4141
[Specialized.OrderedDictionary]
4242
$Map
43-
43+
4444
[Generic.List[Specialized.OrderedDictionary]]
4545
$List
4646

@@ -102,7 +102,7 @@ begin {
102102
# Need to ensure single-item returns get correctly handled as arays,
103103
# not munged into scalars.
104104
if (
105-
($MungedKeyValue.Count -eq 1) -or
105+
($MungedKeyValue.Count -eq 1) -or
106106
($MungedKeyValue -is [Specialized.OrderedDictionary])
107107
) {
108108
$MungedSchema.Add($_.Key, [object[]]$MungedKeyValue)
@@ -308,18 +308,18 @@ begin {
308308
[Parameter(ParameterSetName='FromPath', Mandatory)]
309309
[string]
310310
$Path,
311-
311+
312312
[Parameter(ParameterSetName='FromSchema', Mandatory)]
313313
[Specialized.OrderedDictionary]
314314
$Schema,
315-
315+
316316
[Parameter(ParameterSetName='FromPreset', Mandatory)]
317317
[ValidateSet('ConfigDocument', 'ResourceManifest')]
318318
[string]
319319
$Preset,
320320

321321
[LocalJsonSchemaRegistry] $SchemaRegistry,
322-
322+
323323
[switch]$ForVSCode,
324324
[switch]$WithoutComments,
325325
[switch]$WithoutExamples
@@ -379,9 +379,14 @@ begin {
379379
continue
380380
}
381381

382+
if ($ID -match "$Reference`$") {
383+
Write-Verbose "$ID`n`tSkipping adding self ($Reference) to `$defs"
384+
continue
385+
}
386+
382387
$ReferenceSegments = $Reference.Trim('/') -split '/'
383388
$Working = $MergedSchema.'$defs'
384-
389+
385390
for ($i = 0; $i -lt $ReferenceSegments.Count; $i++) {
386391
$Segment = $ReferenceSegments[$i]
387392

@@ -390,7 +395,7 @@ begin {
390395
$Working = $Working.$Segment
391396
continue
392397
}
393-
398+
394399
# Add an empty dictionary for non-final segments
395400
if ($i -ne ($ReferenceSegments.Count - 1)) {
396401
$Working.Add($Segment, [Specialized.OrderedDictionary]::new())
@@ -446,6 +451,11 @@ begin {
446451
continue
447452
}
448453

454+
if ($ID -match "$Reference`$") {
455+
Write-Verbose "$ID`n`tSkipping adding self ($Reference) to `$defs"
456+
continue
457+
}
458+
449459
if ($Reference -notin $Schema.'$defs'.Keys) {
450460
Write-Verbose "$ID`n`tAdding reference to `$defs: '$Reference'"
451461
$MergedSchema.'$defs'.Add($ReferenceSchema.'$id', $ReferenceSchema)
@@ -524,7 +534,7 @@ begin {
524534

525535
[string]
526536
$OutputDirectory = $PWD,
527-
537+
528538
[string[]]
529539
[ValidateSet('Json', 'JsonVSCode', 'Yaml', 'YamlVSCode')]
530540
$OutputFormat = @(
@@ -567,7 +577,7 @@ begin {
567577
Path = $ConfigFilePath
568578
SchemaRegistry = $SchemaRegistry
569579
}
570-
580+
571581
if ($MergeForNormal) {
572582
$Bundled = Merge-JsonSchema @SharedMergeParams
573583
| Set-BundledSchemaID -BundledName $Name
@@ -716,7 +726,7 @@ process {
716726
foreach ($VSCodeKeyword in $VSCodeKeywords) {
717727
$SchemaData = Remove-JsonSchemaKey -Schema $SchemaData -KeyName $VSCodeKeyword
718728
}
719-
729+
720730
$SchemaData
721731
| ConvertTo-Json -Depth 99
722732
| ForEach-Object { $_ -replace '\r\n', "`n" }

schemas/examples/configuration_document/example.dsc.config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# For testing outside of the repository, set the schema in the yaml server directive to:
2-
# https://raw.githubusercontent.com/PowerShell/DSC/main/2024/04/resource/manifest.json
2+
# https://raw.githubusercontent.com/PowerShell/DSC/main/v3/resource/manifest.json
33
#
44
# Hover on the keys to see the documentation. Try changing values and adding new settings.
5-
$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2024/04/config/document.json
5+
$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/v3/config/document.json
66
# Try adding the variables, metadata, and parameters properties,
77
# then use IntelliSense to define a few entries.
88

schemas/examples/output/config.get.yaml

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,33 @@
1-
# yaml-language-server: $schema=../../2024/04/bundled/outputs/config/get.vscode.json
1+
# yaml-language-server: $schema=../../v3.0/bundled/outputs/config/get.vscode.json
22
#
33
# Shows output from `dsc config get --path dsc/examples/osinfo_registry.dsc.yaml
44
#
55
metadata:
66
Microsoft.DSC:
7-
version: 3.0.0-preview.7
8-
operation: Get
9-
executionType: Actual
10-
startDatetime: 2024-04-16T15:29:19.776934500-05:00
11-
endDatetime: 2024-04-16T15:29:20.052168700-05:00
12-
duration: PT0.275234200S
13-
securityContext: Restricted
7+
version: 3.0.0
8+
operation: get
9+
executionType: actual
10+
startDatetime: 2025-05-09T11:57:24.223992200-05:00
11+
endDatetime: 2025-05-09T11:57:25.237202300-05:00
12+
duration: PT1.0132101S
13+
securityContext: restricted
1414
results:
1515
- metadata:
1616
Microsoft.DSC:
17-
duration: PT0.053120900S
17+
duration: PT0.0391229S
1818
name: os
1919
type: Microsoft/OSInfo
2020
result:
2121
actualState:
2222
$id: https://developer.microsoft.com/json-schemas/dsc/os_info/20230303/Microsoft.Dsc.OS_Info.schema.json
2323
family: Windows
24-
version: 10.0.22631
24+
version: 10.0.26100
2525
edition: Windows 11 Enterprise
2626
bitness: '64'
27+
architecture: x86_64
2728
- metadata:
2829
Microsoft.DSC:
29-
duration: PT0.137957200S
30+
duration: PT0.1583279S
3031
name: windows product name
3132
type: Microsoft.Windows/Registry
3233
result:
@@ -37,7 +38,7 @@ results:
3738
String: Windows 10 Enterprise
3839
- metadata:
3940
Microsoft.DSC:
40-
duration: PT0.035664200S
41+
duration: PT0.049571S
4142
name: system root
4243
type: Microsoft.Windows/Registry
4344
result:

0 commit comments

Comments
 (0)