Skip to content
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

PowerShell adapter PR #363

Merged
merged 115 commits into from
Apr 12, 2024
Merged
Changes from 1 commit
Commits
Show all changes
115 commits
Select commit Hold shift + click to select a range
46af53d
vscode powershell formatting
mgreenegit Mar 8, 2024
e296241
apply formatter again
mgreenegit Mar 8, 2024
1559c45
List and Get only
mgreenegit Mar 11, 2024
efac0dc
Merge branch 'main' into powershell_group
mgreenegit Mar 11, 2024
b5bf80d
Add capabilities and manifest
mgreenegit Mar 12, 2024
3d76d9e
adds support for class based resources
mgreenegit Mar 13, 2024
503555a
Merge remote-tracking branch 'upstream/main' into powershell_group
mgreenegit Mar 14, 2024
0df0846
restore debug output
mgreenegit Mar 14, 2024
ad096c9
remove manifest; changes from review
mgreenegit Mar 14, 2024
87be1ae
comment out tests for test/set/export
mgreenegit Mar 15, 2024
c53fd38
comment export command tests
mgreenegit Mar 18, 2024
caef28f
expect invalid schema to error
mgreenegit Mar 18, 2024
3eaa216
woraround for modulename property
mgreenegit Mar 18, 2024
602c60c
different approach to should throw
mgreenegit Mar 18, 2024
0c3e80b
shouldn't need to null the output
mgreenegit Mar 18, 2024
278e84e
trap error, exclude null from list
mgreenegit Mar 18, 2024
c758f46
workaround for null type
mgreenegit Mar 18, 2024
c512000
null or empty
mgreenegit Mar 18, 2024
b9c74de
bad char
mgreenegit Mar 18, 2024
84567d1
resolved list issues
mgreenegit Mar 19, 2024
cfed4e0
pester nuance for capturing errors, test
mgreenegit Mar 19, 2024
357c24d
try pester output match
mgreenegit Mar 19, 2024
b439202
capture error as stdout and select text
mgreenegit Mar 19, 2024
721f757
LASTEXITCODE 2
mgreenegit Mar 19, 2024
59e701b
error match and arg type
mgreenegit Mar 19, 2024
4717d0f
correct class example for test
mgreenegit Mar 19, 2024
4bb4b01
powershell test fixes
mgreenegit Mar 20, 2024
79ee41a
type
mgreenegit Mar 20, 2024
9fc6a5e
Merge branch 'PowerShell:main' into powershell_group
mgreenegit Mar 26, 2024
ebfff63
many changes from first review
mgreenegit Apr 1, 2024
51da439
Merge remote-tracking branch 'upstream/main' into powershell_group
mgreenegit Apr 1, 2024
ea2ba05
revert Pester change
mgreenegit Apr 1, 2024
71cbe17
additional test pattern
mgreenegit Apr 1, 2024
16ff984
changes lost in previous merge
mgreenegit Apr 2, 2024
6d05cdd
schema change
mgreenegit Apr 2, 2024
fb31f63
requiredAdapter
mgreenegit Apr 2, 2024
d4c77d3
changes from feedback
mgreenegit Apr 2, 2024
7784682
wrong quote type for variable
mgreenegit Apr 2, 2024
cd166fd
move common functions to adapter module
mgreenegit Apr 2, 2024
552a913
copyfiles; missed match strings
mgreenegit Apr 2, 2024
7d7e940
troubleshooting why result is not being returned
mgreenegit Apr 3, 2024
c24fdb0
corrected build copy files
mgreenegit Apr 3, 2024
5e687d2
add debug output for psDscAdapter module
mgreenegit Apr 3, 2024
a9f984a
Only write to debug for adapter if files are missing
mgreenegit Apr 3, 2024
3754a95
Merge branch 'main' into powershell_group
mgreenegit Apr 3, 2024
ab70fec
improve feedback from adapter and stub windows adapter
mgreenegit Apr 3, 2024
3f26e1b
Merge branch 'powershell_group' of https://github.com/mgreenegit/dsc …
mgreenegit Apr 3, 2024
eeb72f9
Merge branch 'PowerShell:main' into powershell_group
mgreenegit Apr 3, 2024
e479ac0
removing winps files
mgreenegit Apr 4, 2024
b539db4
Merge branch 'powershell_group' of https://github.com/mgreenegit/dsc …
mgreenegit Apr 4, 2024
907f597
try to fix line ending issue
mgreenegit Apr 4, 2024
d8201c0
end with new line
mgreenegit Apr 4, 2024
ae646d4
Merge branch 'main' into powershell_group
SteveL-MSFT Apr 4, 2024
703ac73
variables should be 'dsc'; add support for binary resources; start wo…
mgreenegit Apr 5, 2024
7b8c475
supporting windowspowershell adapter
mgreenegit Apr 5, 2024
ed5028e
Add a few tests for winps
mgreenegit Apr 5, 2024
6732749
file must run elevated so accept null for now
mgreenegit Apr 5, 2024
722bcda
try to capture error if import module fails
mgreenegit Apr 6, 2024
1007b31
force windowspowershell adapter to use psdsc in windows
mgreenegit Apr 6, 2024
942919b
condition on error output
mgreenegit Apr 6, 2024
8eab956
contain psdscadapter to pwsh
mgreenegit Apr 6, 2024
4ca4356
module input type; dsc version checks
mgreenegit Apr 6, 2024
6712c9e
iswindows didn't exist in 5.1
mgreenegit Apr 6, 2024
3d50b70
condition was backwards
mgreenegit Apr 6, 2024
9c47e42
simpler condition
mgreenegit Apr 6, 2024
0c4336b
implementationdetail special case for binary
mgreenegit Apr 7, 2024
ab8da32
debugging build agent difference from local
mgreenegit Apr 7, 2024
af576d8
remove module name
mgreenegit Apr 7, 2024
dcb0e6e
try moving resource to psdesiredstateconfiguration rather than windows
mgreenegit Apr 7, 2024
951e331
output module version
mgreenegit Apr 7, 2024
e349948
suppress warning
mgreenegit Apr 7, 2024
4ef4538
empty string instead of json
mgreenegit Apr 7, 2024
9108dd8
check for resource
mgreenegit Apr 7, 2024
a319d59
Specify version
mgreenegit Apr 7, 2024
49b8f8e
Update psDscAdapter.psm1
mgreenegit Apr 7, 2024
6a896d3
new example
mgreenegit Apr 7, 2024
7482840
check name
mgreenegit Apr 7, 2024
9d511ce
diagnostic
mgreenegit Apr 7, 2024
66556f2
Merge branch 'PowerShell:main' into powershell_group
mgreenegit Apr 8, 2024
868524e
diagnostics
mgreenegit Apr 8, 2024
0e64fc6
diag
mgreenegit Apr 8, 2024
b70bbd9
diag
mgreenegit Apr 8, 2024
e9954e8
for version; add more tests
mgreenegit Apr 8, 2024
fc19607
add to gitignore
mgreenegit Apr 8, 2024
efcd851
comment
mgreenegit Apr 8, 2024
2b30caa
make sure psmodulepath has system modules
mgreenegit Apr 8, 2024
e3679b0
bad param name
mgreenegit Apr 8, 2024
9fe9887
trust
mgreenegit Apr 8, 2024
3194fb7
adapting to psresource
mgreenegit Apr 8, 2024
38643e7
correct build and test errors
mgreenegit Apr 8, 2024
ad75cfa
replace null properties in resource list with empty strings
mgreenegit Apr 8, 2024
ce01a1b
cleanup module imports
mgreenegit Apr 8, 2024
4631fc7
fix condition
mgreenegit Apr 8, 2024
39ff59c
bad param
mgreenegit Apr 8, 2024
bbca66a
solve issue in binary output
mgreenegit Apr 8, 2024
2a096a5
cleaner workaround for binary resources
mgreenegit Apr 8, 2024
9758b1f
remove cim properties from script output
mgreenegit Apr 9, 2024
a735c6d
Fix issues with PowerShell adapter tests and module imports
mgreenegit Apr 10, 2024
cd1f1c2
Fix issue with loading Windows resources in PSDesiredStateConfigurati…
mgreenegit Apr 10, 2024
dd66689
confirm winrm quickconfig during build
mgreenegit Apr 11, 2024
44e31f7
condition winrm
mgreenegit Apr 11, 2024
5b80546
gitignore
mgreenegit Apr 11, 2024
bb16a0d
create test file
mgreenegit Apr 11, 2024
67d9dde
Fix typo in PowerShell adapter test script
mgreenegit Apr 11, 2024
6f28cb2
Fix issue with GetScript output
mgreenegit Apr 11, 2024
305eebe
Fix issue with GetScript output in PowerShell adapter tests
mgreenegit Apr 11, 2024
49c34f7
Merge branch 'main' into powershell_group
mgreenegit Apr 11, 2024
93683be
Refactor file paths in PowerShell adapter tests to use TestDrive:\ in…
mgreenegit Apr 11, 2024
cd877c3
Merge branch 'powershell_group' of https://github.com/mgreenegit/dsc …
mgreenegit Apr 11, 2024
eb9be57
remove testdrive experiment
mgreenegit Apr 11, 2024
238b3be
wrong quotes
mgreenegit Apr 11, 2024
ec7d58c
Refactor PowerShell adapter tests to use winrm quickconfig -quiet on …
mgreenegit Apr 11, 2024
cac542f
Refactor PowerShell adapter tests to use Microsoft.Windows/WindowsPow…
mgreenegit Apr 12, 2024
4efac72
chore: Write PSModulePath to debug logs in Invoke-DscCacheRefresh
mgreenegit Apr 12, 2024
98f4126
chore: Remove PSModulePath debug logs in Invoke-DscCacheRefresh
mgreenegit Apr 12, 2024
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
Prev Previous commit
Next Next commit
remove manifest; changes from review
mgreenegit committed Mar 14, 2024
commit ad096c971e3c538919bcf84c7bad8307e140f67a
76 changes: 28 additions & 48 deletions powershell-adapter/powershell.resource.ps1
Original file line number Diff line number Diff line change
@@ -20,20 +20,6 @@ class configFormat {
[psobject] $properties
}

# manifest format for resource list
class manifest {
[string] ${$schema}
[string] $type
[string] $version
[string] $description
[string] $tags
[string] $get
[string] $set
[string] $test
[string] $export
[string] $schema
}

# output format for resource list
class resourceOutput {
[string] $type
@@ -46,25 +32,13 @@ class resourceOutput {
[string] $author
[string[]] $properties
[string] $requires
[manifest] $manifest
[string] $description
}

# If the OS is Windows, import the latest installed PSDesiredStateConfiguration module. For Linux/MacOS, only class based resources are supported and are called directly.
if ($IsWindows) {
$DscModule = Get-Module -Name PSDesiredStateConfiguration -ListAvailable | Sort-Object -Property Version -Descending | Select-Object -First 1
Import-Module $DscModule -DisableNameChecking -ErrorAction Ignore

if ($null -eq $DscModule) {
# Missing module is okay for listing resources
if ($Operation -eq 'List') {
Write-Warning 'The PowerShell adapter was called but the module PSDesiredStateConfiguration could not be found in PSModulePath. To install the module, run Install-PSResource -Name PSDesiredStateConfiguration'
exit 0
}
else {
Write-Error 'The PowerShell adapter was called but the module PSDesiredStateConfiguration could not be found in PSModulePath. To install the module, run Install-PSResource -Name PSDesiredStateConfiguration'
exit 1
}
}
# module types
enum moduleType {
ScriptBased
ClassBased
}

# Cache the results of Get-DscResource to optimize performance
@@ -146,9 +120,23 @@ function Get-ActualState {
}

# 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
switch ($cachedResourceInfo.ImplementationDetail) {
switch ([moduleType]$cachedResourceInfo.ImplementationDetail) {
'ScriptBased' {

# If the OS is Windows, import the latest installed PSDesiredStateConfiguration module. For Linux/MacOS, only class based resources are supported and are called directly.
if ($IsWindows) {
$DscModule = Get-Module -Name PSDesiredStateConfiguration -ListAvailable | Sort-Object -Property Version -Descending | Select-Object -First 1
Import-Module $DscModule -DisableNameChecking -ErrorAction Ignore

if ($null -eq $DscModule) {
Write-Error 'The PowerShell adapter was called but the module PSDesiredStateConfiguration could not be found in PSModulePath. To install the module, run Install-PSResource -Name PSDesiredStateConfiguration'
exit 1
}
} else {
Write-Error 'Script based resources are only supported on Windows.'
exit 1
}

# imports the .psm1 file for the DSC resource as a PowerShell module and stores the list of parameters
Import-Module -Scope Local -Name $cachedResourceInfo.path -Force -ErrorAction stop
$validParams = (Get-Command -Module $cachedResourceInfo.ResourceType -Name 'Get-TargetResource').Parameters.Keys
@@ -223,7 +211,7 @@ function Get-TypeInstanceFromModule {
}

# initialize OUTPUT as array
$result = @()
$result = [System.Collections.Generic.List[Object]]::new()

# process the operation requested to the script
switch ($Operation) {
@@ -234,15 +222,7 @@ switch ($Operation) {
# https://learn.microsoft.com/dotnet/api/system.management.automation.dscresourceinfo
$r = $resourceCache | Where-Object Type -EQ $Type | ForEach-Object DscResourceInfo

# TODO this does not seem to be populating correctly. Need to investigate.
$module = Get-Module -Name $r.ModuleName -ListAvailable | Sort-Object -Property Version -Descending | Select-Object -First 1
[manifest]$manifest = @{
$schema = 'https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2023/08/bundled/resource/manifest.json'
type = $Type
version = $r.version.ToString()
description = $module.Description
tags = $module.PrivateData.PSData.Tags
}

# Provide a way for existing resources to specify their capabilities, or default to Get, Set, Test
if ($module.PrivateData.PSData.Capabilities) {
@@ -264,7 +244,7 @@ switch ($Operation) {
author = $r.CompanyName
properties = $r.Properties.Name
requires = $requiresString
manifest = $manifest
description = $module.Description
} | ConvertTo-Json -Compress
}
}
@@ -312,14 +292,14 @@ switch ($Operation) {
}

# Adding some debug info to STDERR
$m = gmo PSDesiredStateConfiguration
$trace = @{"Debug"="PSVersion="+$PSVersionTable.PSVersion.ToString()} | ConvertTo-Json -Compress
$m = Get-Module PSDesiredStateConfiguration
$trace = @{'Debug' = 'PSVersion=' + $PSVersionTable.PSVersion.ToString() } | ConvertTo-Json -Compress
$host.ui.WriteErrorLine($trace)
$trace = @{"Debug"="PSPath="+$PSHome} | ConvertTo-Json -Compress
$trace = @{'Debug' = 'PSPath=' + $PSHome } | ConvertTo-Json -Compress
$host.ui.WriteErrorLine($trace)
$trace = @{"Debug"="ModuleVersion="+$m.Version.ToString()} | ConvertTo-Json -Compress
$trace = @{'Debug' = 'ModuleVersion=' + $m.Version.ToString() } | ConvertTo-Json -Compress
$host.ui.WriteErrorLine($trace)
$trace = @{"Debug"="ModulePath="+$m.Path} | ConvertTo-Json -Compress
$trace = @{'Debug' = 'ModulePath=' + $m.Path } | ConvertTo-Json -Compress
$host.ui.WriteErrorLine($trace)
$trace = @{"Debug"="PSModulePath="+$env:PSModulePath} | ConvertTo-Json -Compress
$trace = @{'Debug' = 'PSModulePath=' + $env:PSModulePath } | ConvertTo-Json -Compress
$host.ui.WriteErrorLine($trace)