2016-12-28 34 views
1

我一直在使用Otter脚本,现在我想直接从我的一个计划中执行C#代码。我知道我可以直接使用PSExec执行PowerShell代码,但有没有像C#一样的CSExec或类似的?直接从Otter脚本执行C#代码

下面是代码我想运行:

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

回答

1

你可以从那里使用PSExec直接创建PowerShell中的一个新的类型,并调用代码:

$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 ,将该C#代码编译成程序集,例如MyApplication.exe,然后编写一个执行该程序的powershell脚本:

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

然后使用PSExec从奥特脚本来运行PowerShell的

+1

嗯以上位,编译到一个EXE可能是矫枉过正。更直接的等价物就像[scriptcs](http://scriptcs.net/)。 – mason

+0

@mason我在顶部添加了一个备用解决方案 – Bassie

+1

我的另一个选择是为它创建一个exe文件,并使用'Exec'来代替自定义的PowerShell,但是'Add-Type'命令现在可以使用,谢谢! –