1

I've been working with Otter script for a bit now, and I'd like to execute C# code directly from one of my plans. I know I can execute PowerShell code directly using PSExec, but is there an equivalent for C# like CSExec or similar?

Here is the code I would like to run:

if (Directory.Exists($Path))
  LonUtil.SendEmail("Path exists!");
else
  LonUtil.SendEmail("Path does not exist.", false);

1 Answers1

1

You could create a new type in Powershell and call the code directly from there using PSExec:

$source = @"
public class MyCode
{
    public void Action(string path) 
    {
        System.Console.WriteLine(path);
    }
}
"@

Add-Type -TypeDefinition $source
$MyCode = New-Object MyCode
$MyCode.Action("Write this to the console!")

Alternatvely, compile that c# code into an assembly, say MyApplication.exe, and then write a powershell script which executes the program:

$path = "the/required/path"
& MyApplication.exe $path

Then use PSExec from Otter Script to run the above bit of Powershell

Bassie
  • 9,529
  • 8
  • 68
  • 159
  • 1
    Eh, compiling to an EXE might be overkill. A more direct equivalent would be something like [scriptcs](http://scriptcs.net/). – mason Dec 28 '16 at 19:48
  • @mason I added an alternate solution at the top – Bassie Dec 28 '16 at 19:57
  • 1
    My other option was to create an exe for it and use `Exec` instead of custom PowerShell, but the `Add-Type` command will work for now, thanks! – Lonnie Schultz Dec 28 '16 at 20:02