0

I've tried to convert an old .bat script that needs to be ran for another propriatary system into c#, given I know more C# than I do bash. The gist of this is that it prints out some functions from systeminfo into a plain text file disguised as a propriatary filetype. (Having opened them, they are litterally just .txt's renamed.)

My current .bat code looks like this (Horribly inefficient, but still);

del %USERPROFILE%\documents\systeminfo_nm.ncf

echo CUR INF SET - DO NOT MODIFY>>"%USERPROFILE%\documents\systeminfo_nm.ncf"

systeminfo | find /I "OS Version">>"%USERPROFILE%\documents\systeminfo_nm.ncf"
systeminfo | find /I "BIOS Version">>"%USERPROFILE%\documents\systeminfo_nm.ncf"
systeminfo | find /I "System Model">>"%USERPROFILE%\documents\systeminfo_nm.ncf"
systeminfo | find /I "Time:">>"%USERPROFILE%\documents\systeminfo_nm.ncf"
systeminfo | find /I "Original">>"%USERPROFILE%\documents\systeminfo_nm.ncf"

And this gets parsed to an external func. - HOWEVER! This will not work, as we are phasing out ALL .bat scripts, and they need to be turned into .exe's as my usual converting tool to exe just packs it up and disguises it, but it gets blocked once it unpacks the .bat, and the only other way I know how to do that is to write it out in C# code.

Currently my C# code looks as such - however C# keeps trying to read these lines as code, and not as the text it is supposed to parse to the CMD module:

static void Main(string[] args)

            string strCmdTextOne;
            strCmdTextOne = @"systeminfo | find /I @"OS Version">>@"%USERPROFILE%\documents\systeminfo_nm.ncf"";


            string strCmdTextOne;
            strCmdTextOne = @"systeminfo | find /I @"BIOS Version">>@"%USERPROFILE%\documents\systeminfo_nm.ncf"";

            System.Diagnostics.Process.Start("CMD.exe", strCmdTextOne, strCmdTextTwo);

Is there a way for me to escape this, or maybe run the commands all on the same systeminfo command, to optimize it too, if that is possible?

In the end, all I really need is for it to run in a standalone c# .exe rather than a .bat

Encore
  • 11
  • 1
  • 6
  • 5
    In verbatim strings the quotes are escaped by doubling them. So `@"systeminfo [...]""OS Version"">>[...]"`, etc. – György Kőszeg Oct 08 '20 at 11:34
  • 1
    `.bat` is the extension used for *DOS* batch files. Windows uses the `.cmd` extension. As for horribly inefficient - yes. You could get that information, well formatted and accessible as objects and properties with PowerShell. Or by using the appropriate .NET class – Panagiotis Kanavos Oct 08 '20 at 11:37
  • 1
    With Powershell, you can get the system information with [Get-ComputerInfo](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-computerinfo?view=powershell-7), store the result in a variable and access the properties, eg `$x=Get-ComputerInfo; $x.BiosName` – Panagiotis Kanavos Oct 08 '20 at 11:41
  • @PanagiotisKanavos I would have loved to use PS, however I'm not sure it will work because of two worries; 1. I don't know if PS will throw an exception to writing to a fake format as shown above, and 2. It needs to be ran as an EXE as .ps1 files are blocked. (As you might be able to tell this system and the vendor are to be throttled for not allowing even administrative accounts to run basic scripts from .cmd or .ps1 files.) – Encore Oct 08 '20 at 11:47
  • 1
    Your batch file would have been much better just as a single line command, `@%SystemRoot%\System32\systeminfo.exe | %SystemRoot%\System32\findstr.exe /I /C:"OS Version" /C:"BIOS Version" /C:"System Model" /C:"Time:" /C:"Original" 1> "%UserProfile%\Documents\systeminfo_nm.ncf"`. As you can see, this is considerably more efficient, because it runs the systeminfo utility only once, not five times. You should also be aware that your current C# code does that too, which is the same bad idea. – Compo Oct 08 '20 at 12:23
  • 2
    I will not generally advise the use of `systeminfo.exe` however, because it takes a while to load a great many things you do not need to retrieve. It would be quicker to retrieve only what you do need, most of which is available via WMI. In your C# code you should be able to get some information from `Environment` too. – Compo Oct 08 '20 at 12:24

0 Answers0