Home > Software design >  Add external C# script to PowerShell script as source
Add external C# script to PowerShell script as source

Time:11-15

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

CodePudding user response:

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
  • Related