I want to call a PowerShell script with 3 arguments.
The first argument is the path of a C# script file.
Inside the PowerShell script, I want to execute a method in the C# script and also pass all the arguments to it.
# RunBackup.ps1
$csharp = Get-Content -Path $args[0]
$job = Start-Job -ScriptBlock {
Add-Type -TypeDefinition "$csharp"
$obj = New-Object Main
$result = $obj.Execute($args)
$result
}
Wait-Job $job
Receive-Job $job
// Main.cs
using System;
public class Main
{
public int Execute(string[] args)
{
Console.WriteLine(args[0]);
return 0;
}
}
But when I run the PS script with ./RunBackup Main.cs 1 2 3, I got the following error.
Cannot bind argument to parameter 'TypeDefinition' because it is an empty string.
+ CategoryInfo : InvalidData: (:) [Add-Type], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.AddTypeCommand
+ PSComputerName : localhost
Cannot find type [Main]: verify that the assembly containing this type is loaded.
+ CategoryInfo : InvalidType: (:) [New-Object], PSArgumentException
+ FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand
+ PSComputerName : localhost
You cannot call a method on a null-valued expression.
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
+ PSComputerName : localhost
Main, then compile the app. You will then either get an exe or dll, which you can then run withdotnet file.dll parametersdotnet run app args, it'll build and run from source code. That'll meet my requirements. Thanks.