Trying to run a bit of C# code from a batch file.
I do NOT want to create an intermediate temporary file, compile it and then run it.
Not the self-compiling method, will not do for my purposeenter link description here
It should be possible to run that C# program on a single line, calling it with powershell -command Add-Type -TypeDefinition @" "@ etc..
First here is the simple C# program to run
Add-Type -TypeDefinition @"
using System;
public class LogicCheck
{
public static void Check(bool condition)
{
if (condition)
{
Console.WriteLine("Result is True");
}
else
{
Console.WriteLine("Result is False");
}
}
}
"@
[LogicCheck]::Check($false)
You can executing it by saving it to a file and running from console
powershell -command "Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass"
powershell -file "csharp script.ps1"
Here are some of my early attempts
powershell -command Add-Type -TypeDefinition @" using System; public class LogicCheck { public static void Check(bool condition) { if (condition) { Console.WriteLine("Result is True"); } else { Console.WriteLine("Result is False"); } } } "@ [LogicCheck]::Check($false)
powershell -command Add-Type -TypeDefinition @" using System; public class LogicCheck { public static void Check(bool condition) { if (condition) { Console.WriteLine(\"Result is True\"); } else { Console.WriteLine(\"Result is False\"); } } } "@ [LogicCheck]::Check($false)
powershell -command Add-Type -TypeDefinition @' using System; public class LogicCheck { public static void Check(bool condition) { if (condition) { Console.WriteLine("Result is True"); } else { Console.WriteLine("Result is False"); } } } '@ [LogicCheck]::Check($false)
Then after some readings, I discovered here string need to open and close on their own lines
So I tried a streamlined version without using here strings. After all, C# is semi colon terminated and shouldn't care at all about the presence of CrLFs
So I tried a few more permutations without the here string construct
powershell -command Add-Type -TypeDefinition using System; public class LogicCheck { public static void Check(bool condition) { if (condition) { Console.WriteLine("Result is True"); } else { Console.WriteLine("Result is False"); } } } [LogicCheck]::Check($false)
powershell -command Add-Type -TypeDefinition " using System; public class LogicCheck { public static void Check(bool condition) { if (condition) { Console.WriteLine("Result is True"); } else { Console.WriteLine("Result is False"); } } } " [LogicCheck]::Check($false)
powershell -command " Add-Type -TypeDefinition using System; public class LogicCheck { public static void Check(bool condition) { if (condition) { Console.WriteLine(\"Result is True\"); } else { Console.WriteLine(\"Result is False\"); } } } [LogicCheck]::Check($false)"
powershell -command " Add-Type -TypeDefinition using System; public class LogicCheck { public static void Check(bool condition) { if (condition) { Console.WriteLine(^"Result is True^"); } else { Console.WriteLine(^"Result is False^"); } } } [LogicCheck]::Check($false)"
These do not work either
So I figured, maybe I can use the here string after all, if I insert %LF% characters in the proper place ?
powershell -command Add-Type -TypeDefinition @"%LF% using System; public class LogicCheck { public static void Check(bool condition) { if (condition) { Console.WriteLine("Result is True"); } else { Console.WriteLine("Result is False"); } } } %LF%"@
powershell -command Add-Type -TypeDefinition @"%LF% using System; public class LogicCheck { public static void Check(bool condition) { if (condition) { Console.WriteLine(\"Result is True\"); } else { Console.WriteLine(\"Result is False\"); } } } %LF%"@
powershell -command Add-Type -TypeDefinition @'%LF% using System; public class LogicCheck { public static void Check(bool condition) { if (condition) { Console.WriteLine(\"Result is True\"); } else { Console.WriteLine(\"Result is False\"); } } } %LF%'@
powershell -command " Add-Type -TypeDefinition @"%LF% using System; public class LogicCheck { public static void Check(bool condition) { if (condition) { Console.WriteLine("Result is True"); } else { Console.WriteLine("Result is False"); } } } %LF%"@
This too, did not work
Here are all the permutations I have tried so far
code https://pastebin.com/KqVAQ1T9 console output https://pastebin.com/6kTHJgEN code https://pastebin.com/CTbS0hfK console output https://pastebin.com/QbMG4E00