D.R.
D.R.

Reputation: 21194

Call Write-Verbose from C#

I load an "inline-type" in PowerShell like so:

$source = " some c# here "
$type = Add-Type -TypeDefinition $source -PassThru
$myvar = [MyClass]::new()
$myvar.Do()

How to call Write-Verbose from within the C# snippet, i.e., from the source in $source?

I only find examples on the Internet which perform WriteVerbose calls if the C# code contains a Cmdlet - which it does not in my case. Also I can find code how to call cmdlets in general, however, I don't want to create a new runspace, is there maybe any way to get the current runspace?

Upvotes: 1

Views: 93

Answers (1)

D.R.
D.R.

Reputation: 21194

I found a solution, it is possible to pass a delegate to the C# code like so:

$writeVerboseDelegate = [Action[string]]{param($s) Write-Verbose $s; }
...
$myvar = [MyClass]::new($writeVerboseDelegate)

Upvotes: 1

Related Questions