CodePidgy
CodePidgy

Reputation: 53

Add external C# script to PowerShell script as source

I have a PowerShell script that uses Add-Type and for -TypeDefinition I would like to, instead of defining $Source = @""@, define $Source = script.cs.

Is this possible and if so how does one go about doing it?

$Source = Get-Content -Path $PSScriptRoot\script.cs -Raw

Add-Type -ReferencedAssemblies 'System', 'System.Runtime.InteropServices' -TypeDefinition $Source -Language CSharp

Then I use whatever in C# script later on in the PS script. I do believe these are the only two important area right now

Upvotes: 1

Views: 405

Answers (1)

Zach W
Zach W

Reputation: 353

Get-Content can grab the contents a file and use it as a string. Here is a link to the documentation. For instance:

$Source = Get-Content -Path .\path\to\script.cs -Raw

Upvotes: 1

Related Questions