Closed LaurentDardenne closed 5 years ago
@LaurentDardenne Hmm not sure. Good question. I think you are correct it would be related to scoping and PowerShell handling of the app domain. It requires further investigation.
@LaurentDardenne I've looked into this a little more.
Actually the enum is there, it's just being not found by the type resolver. As far as I can tell it's intentional, to work around the issue of unloading types from PowerShell and to limit cross runspace issues.
Interesting that the Add-Type
has a different result, but I suspect it was to preserve existing behavior.
We can even create an instance of the enum, just not using [MyAction].
$Path='C:\temp'
$Rule='Test.Rule.ps1'
@'
Rule test {
try {
#[System.Enum]::Parse([MyActions],$TargetObject,$true) > $null
[System.AppDomain]::CurrentDomain.GetAssemblies()|
Where-object {$_.location -eq $null}|
Foreach-object {
Write-Warning "Assembly : $($_.Fullname)";
$_.DefinedTypes|
% {Write-warning $_}
$_.DefinedTypes|
% { $_::new() -ne $Null }
}
#[MyActions] > $null
$true
}
catch {
Write-Warning "Exception inside Rule: `r`n$($_|select * |out-string)"
$false
}
}
'@ >"$Path\$Rule"
$Script='CallRule.ps1'
@"
'Delete'|Invoke-PSRule -path "$Path\$Rule"
"@ >"$Path\$Script"
enum MyActions{
Delete
Add
}
cd $path
&"$Path\$Script"
If you want the enum to be accessed by PSRule, add the enum to the same .Rule.ps1
file or a peer file like Common.Rule.ps1
that gets loaded by Invoke-PSRule -Path .\
.
Hope that answers your question?
Hope that answers your question?
Yes, it specifies the behavior and the way of doing things. Thank you.
Description of the issue A Rule do not find an enum generated by Powershell ( native class version 5.1 ) but found a class generated by Add-Type.
First case, we create an Enum with the 'enum' powershell keyword :
Access to the [Actions] class throws an exception, although the dynamic assembly exists.
Second case, we create an Enum with the 'Add-Type' cmdlet inside a new session. We use the previous scripts :
In this case the code inside the rule find the class. I don't know if it is an issue with scoping or with the runtime Powershell , may be the type of assembly is different and is problematic...