PowerShell 5: Can you ‘export’ a class definition from an EngineEvent into the current session scope?

Hello!

I am playing with classes and events in Powershell 5 and trying to get a class definition exported from an event being triggered into the current scope.

For example would something like this be possible?

C:pathtoRandomClass.ps1 contents:

Class SomeClass{ [string]$Property SomeClass([string]$Prop){ $this.Property = $Prop } } 

Event Registration:

Register-EngineEvent -SourceIdentifier "ClassDeclarationFile" -Action { . $Event.SourceArgs[0] } 

Event Triggering:

New-Event -SourceIdentifier "ClassDeclarationFile" -Sender "EventTester" -EventArguments "C:pathtoRandomClass.ps1" 

and then this would ideally not be a syntax error:

[SomeClass]::new("Value") 

If that isn’t possible is there any real way to bring a dynamic class into scope other than dot-sourcing the file? I poked at using a bit, but it didn’t seem useful outside the context of a module.

submitted by /u/acct_for_work
[link] [comments]

Leave a Reply