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

Export multi methods #579

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,30 @@ class TestClassResource : BaseTestClass

return $resultList.ToArray()
}

static [TestClassResource[]] Export([bool]$UseExport)
{
if ($UseExport)
{
return [TestClassResource]::Export()
}
else
{
$resultList = [List[TestClassResource]]::new()
$resultCount = 5
if ($env:TestClassResourceResultCount) {
$resultCount = $env:TestClassResourceResultCount
}
1..$resultCount | %{
$obj = New-Object TestClassResource
$obj.Name = "Object$_"
$obj.Prop1 = "Property of object$_"
$resultList.Add($obj)
}
}

return $resultList.ToArray()
}
}

[DscResource()]
Expand Down
10 changes: 8 additions & 2 deletions powershell-adapter/psDscAdapter/psDscAdapter.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -484,8 +484,14 @@ function Invoke-DscOperation {
$addToActualState.properties = [psobject]@{'InDesiredState'=$Result}
}
'Export' {
$t = $dscResourceInstance.GetType()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not remove this line;
otherwise $t is $null and you get You cannot call a method on a null-valued expression..

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I was a bit too aggressive on the removal. The line is added and the test should be working. I shouldn't have made such hasty assumptions. Do you mind running it once again?

$method = $t.GetMethod('Export')
$methods = $t.GetMethods() | Where-Object { $_.Name -eq 'Export' }
$method = foreach ($m in $methods) {
if ($m.GetParameters().Count -eq 0) {
$m
break
}
}

if ($null -eq $method) {
"Export method not implemented by resource '$($DesiredState.Type)'" | Write-DscTrace -Operation Error
exit 1
Expand Down
Loading