1
1
# Copyright (c) Microsoft Corporation.
2
2
# Licensed under the MIT License.
3
3
4
+ function Write-DscTrace {
5
+ param (
6
+ [Parameter (Mandatory = $false )]
7
+ [ValidateSet (' Error' , ' Warn' , ' Info' , ' Debug' , ' Trace' )]
8
+ [string ]$Operation = ' Debug' ,
9
+
10
+ [Parameter (Mandatory = $true , ValueFromPipeline = $true )]
11
+ [string ]$Message
12
+ )
13
+
14
+ $trace = @ {$Operation = $Message } | ConvertTo-Json - Compress
15
+ $host.ui.WriteErrorLine ($trace )
16
+ }
17
+
4
18
# if the version of PowerShell is greater than 5, import the PSDesiredStateConfiguration module
5
19
# this is necessary because the module is not included in the PowerShell 7.0+ releases;
6
20
# In Windows PowerShell, we should always use version 1.1 that ships in Windows.
12
26
$env: PSModulePath += " ;$env: windir \System32\WindowsPowerShell\v1.0\Modules"
13
27
$PSDesiredStateConfiguration = Import-Module - Name ' PSDesiredStateConfiguration' - RequiredVersion ' 1.1' - Force - PassThru - ErrorAction stop - ErrorVariable $importModuleError
14
28
if (-not [string ]::IsNullOrEmpty($importModuleError )) {
15
- $trace = @ {' Debug' = ' ERROR: Could not import PSDesiredStateConfiguration 1.1 in Windows PowerShell. ' + $importModuleError } | ConvertTo-Json - Compress
16
- $host.ui.WriteErrorLine ($trace )
29
+ ' ERROR: Could not import PSDesiredStateConfiguration 1.1 in Windows PowerShell. ' + $importModuleError | Write-DscTrace
17
30
}
18
31
}
19
32
@@ -39,17 +52,20 @@ function Invoke-DscCacheRefresh {
39
52
40
53
$refreshCache = $false
41
54
42
- $cacheFilePath = Join-Path $env: LocalAppData " dsc\PSAdapterCache.json"
43
- if ($PSVersionTable.PSVersion.Major -le 5 ) {
44
- $cacheFilePath = Join-Path $env: LocalAppData " dsc\WindowsPSAdapterCache.json"
45
- }
46
- if ($IsLinux -or $IsMacOS ) {
47
- $cacheFilePath = Join-Path $env: HOME " .dsc" " PSAdapterCache.json"
55
+ $cacheFilePath = if ($IsWindows ) {
56
+ # PS 6+ on Windows
57
+ Join-Path $env: LocalAppData " dsc\PSAdapterCache.json"
58
+ } else {
59
+ # either WinPS or PS 6+ on Linux/Mac
60
+ if ($PSVersionTable.PSVersion.Major -le 5 ) {
61
+ Join-Path $env: LocalAppData " dsc\WindowsPSAdapterCache.json"
62
+ } else {
63
+ Join-Path $env: HOME " .dsc" " PSAdapterCache.json"
64
+ }
48
65
}
49
66
50
67
if (Test-Path $cacheFilePath ) {
51
- $trace = @ {' Debug' = " Reading from Get-DscResource cache file $cacheFilePath " } | ConvertTo-Json - Compress
52
- $host.ui.WriteErrorLine ($trace )
68
+ " Reading from Get-DscResource cache file $cacheFilePath " | Write-DscTrace
53
69
54
70
$cache = Get-Content - Raw $cacheFilePath | ConvertFrom-Json
55
71
$dscResourceCacheEntries = $cache.ResourceCache
@@ -58,25 +74,20 @@ function Invoke-DscCacheRefresh {
58
74
# if there is nothing in the cache file - refresh cache
59
75
$refreshCache = $true
60
76
61
- $trace = @ {' Debug' = " Filtered DscResourceCache cache is empty" } | ConvertTo-Json - Compress
62
- $host.ui.WriteErrorLine ($trace )
77
+ " Filtered DscResourceCache cache is empty" | Write-DscTrace
63
78
}
64
79
else
65
80
{
66
- $trace = @ {' Debug' = " Checking cache for stale entries" } | ConvertTo-Json - Compress
67
- $host.ui.WriteErrorLine ($trace )
81
+ " Checking cache for stale entries" | Write-DscTrace
68
82
69
83
foreach ($cacheEntry in $dscResourceCacheEntries ) {
70
- $trace = @ {' Trace' = " Checking cache entry '$ ( $cacheEntry.Type ) $ ( $cacheEntry.LastWriteTimes ) '" } | ConvertTo-Json - Compress
71
- $host.ui.WriteErrorLine ($trace )
84
+ " Checking cache entry '$ ( $cacheEntry.Type ) $ ( $cacheEntry.LastWriteTimes ) '" | Write-DscTrace - Operation Trace
72
85
73
86
$cacheEntry.LastWriteTimes.PSObject.Properties | ForEach-Object {
74
87
75
88
if (-not ((Get-Item $_.Name ).LastWriteTime.Equals([DateTime ]$_.Value )))
76
89
{
77
- $trace = @ {' Debug' = " Detected stale cache entry '$ ( $_.Name ) '" } | ConvertTo-Json - Compress
78
- $host.ui.WriteErrorLine ($trace )
79
-
90
+ " Detected stale cache entry '$ ( $_.Name ) '" | Write-DscTrace
80
91
$refreshCache = $true
81
92
break
82
93
}
@@ -85,8 +96,7 @@ function Invoke-DscCacheRefresh {
85
96
if ($refreshCache ) {break }
86
97
}
87
98
88
- $trace = @ {' Debug' = " Checking cache for stale PSModulePath" } | ConvertTo-Json - Compress
89
- $host.ui.WriteErrorLine ($trace )
99
+ " Checking cache for stale PSModulePath" | Write-DscTrace
90
100
91
101
$m = $env: PSModulePath -split [IO.Path ]::PathSeparator | % {Get-ChildItem - Directory - Path $_ - Depth 1 - ea SilentlyContinue}
92
102
@@ -95,24 +105,20 @@ function Invoke-DscCacheRefresh {
95
105
$hs_cache.SymmetricExceptWith ($hs_live )
96
106
$diff = $hs_cache
97
107
98
- $trace = @ {' Debug' = " PSModulePath diff '$diff '" } | ConvertTo-Json - Compress
99
- $host.ui.WriteErrorLine ($trace )
108
+ " PSModulePath diff '$diff '" | Write-DscTrace
100
109
101
110
if ($diff.Count -gt 0 ) {
102
111
$refreshCache = $true
103
112
}
104
113
}
105
114
}
106
115
else {
107
- $trace = @ {' Debug' = " Cache file not found '$cacheFilePath '" } | ConvertTo-Json - Compress
108
- $host.ui.WriteErrorLine ($trace )
109
-
116
+ " Cache file not found '$cacheFilePath '" | Write-DscTrace
110
117
$refreshCache = $true
111
118
}
112
119
113
120
if ($refreshCache ) {
114
- $trace = @ {' Debug' = ' Constructing Get-DscResource cache' } | ConvertTo-Json - Compress
115
- $host.ui.WriteErrorLine ($trace )
121
+ ' Constructing Get-DscResource cache' | Write-DscTrace
116
122
117
123
# create a list object to store cache of Get-DscResource
118
124
[dscResourceCacheEntry []]$dscResourceCacheEntries = [System.Collections.Generic.List [Object ]]::new()
@@ -153,8 +159,7 @@ function Invoke-DscCacheRefresh {
153
159
if ( $psdscVersion -ge ' 2.0.7' ) {
154
160
# only support known dscResourceType
155
161
if ([dscResourceType ].GetEnumNames() -notcontains $dscResource.ImplementationDetail ) {
156
- $trace = @ {' Debug' = ' WARNING: implementation detail not found: ' + $dscResource.ImplementationDetail } | ConvertTo-Json - Compress
157
- $host.ui.WriteErrorLine ($trace )
162
+ ' WARNING: implementation detail not found: ' + $dscResource.ImplementationDetail | Write-DscTrace
158
163
continue
159
164
}
160
165
}
@@ -221,8 +226,7 @@ function Invoke-DscCacheRefresh {
221
226
222
227
# save cache for future use
223
228
# TODO: replace this with a high-performance serializer
224
- $trace = @ {' Debug' = " Saving Get-DscResource cache to '$cacheFilePath '" } | ConvertTo-Json - Compress
225
- $host.ui.WriteErrorLine ($trace )
229
+ " Saving Get-DscResource cache to '$cacheFilePath '" | Write-DscTrace
226
230
$jsonCache = $cache | ConvertTo-Json - Depth 90
227
231
New-Item - Force - Path $cacheFilePath - Value $jsonCache - Type File | Out-Null
228
232
}
@@ -242,8 +246,7 @@ function Get-DscResourceObject {
242
246
243
247
# catch potential for improperly formatted configuration input
244
248
if ($inputObj.resources -and -not $inputObj.metadata .' Microsoft.DSC' .context -eq ' configuration' ) {
245
- $trace = @ {' Debug' = ' WARNING: The input has a top level property named "resources" but is not a configuration. If the input should be a configuration, include the property: "metadata": {"Microsoft.DSC": {"context": "Configuration"}}' } | ConvertTo-Json - Compress
246
- $host.ui.WriteErrorLine ($trace )
249
+ ' WARNING: The input has a top level property named "resources" but is not a configuration. If the input should be a configuration, include the property: "metadata": {"Microsoft.DSC": {"context": "Configuration"}}' | Write-DscTrace
247
250
}
248
251
249
252
# match adapter to version of powershell
@@ -290,16 +293,13 @@ function Invoke-DscOperation {
290
293
)
291
294
292
295
$osVersion = [System.Environment ]::OSVersion.VersionString
293
- $trace = @ {' Debug' = ' OS version: ' + $osVersion } | ConvertTo-Json - Compress
294
- $host.ui.WriteErrorLine ($trace )
296
+ ' OS version: ' + $osVersion | Write-DscTrace
295
297
296
298
$psVersion = $PSVersionTable.PSVersion.ToString ()
297
- $trace = @ {' Debug' = ' PowerShell version: ' + $psVersion } | ConvertTo-Json - Compress
298
- $host.ui.WriteErrorLine ($trace )
299
+ ' PowerShell version: ' + $psVersion | Write-DscTrace
299
300
300
301
$moduleVersion = Get-Module PSDesiredStateConfiguration | ForEach-Object Version
301
- $trace = @ {' Debug' = ' PSDesiredStateConfiguration module version: ' + $moduleVersion } | ConvertTo-Json - Compress
302
- $host.ui.WriteErrorLine ($trace )
302
+ ' PSDesiredStateConfiguration module version: ' + $moduleVersion | Write-DscTrace
303
303
304
304
# get details from cache about the DSC resource, if it exists
305
305
$cachedDscResourceInfo = $dscResourceCache | Where-Object Type -EQ $DesiredState.type | ForEach-Object DscResourceInfo
@@ -315,17 +315,15 @@ function Invoke-DscOperation {
315
315
if ($_.TypeNameOfValue -EQ ' System.String' ) { $addToActualState .$ ($_.Name ) = $DesiredState .($_.Name ) }
316
316
}
317
317
318
- $trace = @ {' Debug' = ' DSC resource implementation: ' + [dscResourceType ]$cachedDscResourceInfo.ImplementationDetail } | ConvertTo-Json - Compress
319
- $host.ui.WriteErrorLine ($trace )
318
+ ' DSC resource implementation: ' + [dscResourceType ]$cachedDscResourceInfo.ImplementationDetail | Write-DscTrace
320
319
321
320
# workaround: script based resources do not validate Get parameter consistency, so we need to remove any parameters the author chose not to include in Get-TargetResource
322
321
switch ([dscResourceType ]$cachedDscResourceInfo.ImplementationDetail ) {
323
322
' ScriptBased' {
324
323
325
324
# For Linux/MacOS, only class based resources are supported and are called directly.
326
325
if ($IsLinux ) {
327
- $trace = @ {' Debug' = ' ERROR: Script based resources are only supported on Windows.' } | ConvertTo-Json - Compress
328
- $host.ui.WriteErrorLine ($trace )
326
+ ' ERROR: Script based resources are only supported on Windows.' | Write-DscTrace
329
327
exit 1
330
328
}
331
329
@@ -361,8 +359,7 @@ function Invoke-DscOperation {
361
359
$addToActualState.properties = $getDscResult
362
360
}
363
361
catch {
364
- $trace = @ {' Debug' = ' ERROR: ' + $_.Exception.Message } | ConvertTo-Json - Compress
365
- $host.ui.WriteErrorLine ($trace )
362
+ ' ERROR: ' + $_.Exception.Message | Write-DscTrace
366
363
exit 1
367
364
}
368
365
}
@@ -401,21 +398,18 @@ function Invoke-DscOperation {
401
398
}
402
399
catch {
403
400
404
- $trace = @ {' Debug' = ' ERROR: ' + $_.Exception.Message } | ConvertTo-Json - Compress
405
- $host.ui.WriteErrorLine ($trace )
401
+ ' ERROR: ' + $_.Exception.Message | Write-DscTrace
406
402
exit 1
407
403
}
408
404
}
409
405
' Binary' {
410
406
if ($PSVersionTable.PSVersion.Major -gt 5 ) {
411
- $trace = @ {' Debug' = ' To use a binary resource such as File, Log, or SignatureValidation, use the Microsoft.Windows/WindowsPowerShell adapter.' } | ConvertTo-Json - Compress
412
- $host.ui.WriteErrorLine ($trace )
407
+ ' To use a binary resource such as File, Log, or SignatureValidation, use the Microsoft.Windows/WindowsPowerShell adapter.' | Write-DscTrace
413
408
exit 1
414
409
}
415
410
416
411
if (-not (($cachedDscResourceInfo.ImplementedAs -eq ' Binary' ) -and (' File' , ' Log' , ' SignatureValidation' -contains $cachedDscResourceInfo.Name ))) {
417
- $trace = @ {' Debug' = ' Only File, Log, and SignatureValidation are supported as Binary resources.' } | ConvertTo-Json - Compress
418
- $host.ui.WriteErrorLine ($trace )
412
+ ' Only File, Log, and SignatureValidation are supported as Binary resources.' | Write-DscTrace
419
413
exit 1
420
414
}
421
415
@@ -438,14 +432,12 @@ function Invoke-DscOperation {
438
432
$addToActualState.properties = $getDscResult
439
433
}
440
434
catch {
441
- $trace = @ {' Debug' = ' ERROR: ' + $_.Exception.Message } | ConvertTo-Json - Compress
442
- $host.ui.WriteErrorLine ($trace )
435
+ ' ERROR: ' + $_.Exception.Message | Write-DscTrace
443
436
exit 1
444
437
}
445
438
}
446
439
Default {
447
- $trace = @ {' Debug' = ' Can not find implementation of type: ' + $cachedDscResourceInfo.ImplementationDetail } | ConvertTo-Json - Compress
448
- $host.ui.WriteErrorLine ($trace )
440
+ ' Can not find implementation of type: ' + $cachedDscResourceInfo.ImplementationDetail | Write-DscTrace
449
441
exit 1
450
442
}
451
443
}
@@ -455,8 +447,7 @@ function Invoke-DscOperation {
455
447
else {
456
448
$dsJSON = $DesiredState | ConvertTo-Json - Depth 10
457
449
$errmsg = ' Can not find type "' + $DesiredState.type + ' " for resource "' + $dsJSON + ' ". Please ensure that Get-DscResource returns this resource type.'
458
- $trace = @ {' Debug' = ' ERROR: ' + $errmsg } | ConvertTo-Json - Compress
459
- $host.ui.WriteErrorLine ($trace )
450
+ ' ERROR: ' + $errmsg | Write-DscTrace
460
451
exit 1
461
452
}
462
453
}
0 commit comments