none
passing result from CS file to batch file? RRS feed

  • Question

  • Hello,

    I am trying to run a CS file within a batch file but so far nothing happens..

    echo on
    
    cd \
    net use z: \\CONFIGMGRTEST\SMSPKGC$\TST00014
    
    rem FFCS-MultiPlatform.bat file
    rem a selection is made to know which program has to be launched
    
    rem SETTING THE LOCATION OF THE C# COMPILER
    SET framework=%SystemRoot%\Microsoft.NET\Framework\v2.0.50727
    rem COMPILING THE CS FILE
    %framework%\csc  Architecture-Processor.cs
    rem EXECUTING  THE C# APPLICATION
    
    
    :32BIT
    C:\Source Files\Packages\FOREFRONT\x86\FFCS.bat
    
    goto END
    
    
    :64BIT
    C:\Source Files\Packages\FOREFRONT\x64\FFCS.bat
    
    goto END
    
    :END
    exit
    
    

    and the CS file is:
    using System;
    using System.Management;
    
    public class ManagementObject
    {
        public static void Main()
        {
            /// 
            ManagementObject mo;
            mo = new ManagementObject("Win32_Processor.AddressWidth=''");
            ushort i = (ushort)mo["Architecture"];
    
            /// Test the Processor and populated the field to be tested in the batch file
            switch (i)
            {
                case 0:
                    return "32 Bit";
                    break;
                case 9:
                    return "64 Bit";
                    break;
            }
        }
    }
    How do I pass the 32 Bit and/or 64 Bit collected by the CS file to the batch file to be used to the if .... goto 32Bit or 64 Bits

    Thanks,
    Dom
    System Center Operations Manager 2007 / System Center Configuration Manager 2007 R2 Support
    Saturday, February 6, 2010 12:36 AM

Answers

  • Hi Felyjos,
    You may test your method in a separate C# project first before use it in batch.
    The given method cannot build in VS, ManagementObject is a build-in class of .NET, we need to change the class name to something others, for example, MyManagementObject.
    The given method doesn't take any parameters, that's why we cannot pass parameters to this method from batch file.
    I did a similar test, and it works in my side, hope following steps can hlep you:
    1. Create a .cs file: source.cs.
    2. In command window, input command: SET framework=%SystemRoot%\Microsoft.NET\Framework\v2.0.50727
    3. In command window, input command: %framework%\csc  source.cs
    4. In command window, input command: source.exe Win32_Processor
    5. You will see properties of Win32_Processor class.
    6.  source.cs is:

             using System;

    using System.Management;

     

    public class MyManUagementObject

    {

        public static int Main(string[] args)

        {

            if (args.Length == 0)

            {

                Console.WriteLine("Please input a WMI Class name.");

                return -1;

            }

            try

            {

                string remoteMachine = "localhost";

                string queryStr = "select * from " + args[0];

     

                ManagementScope ms = new ManagementScope(string.Format(@"\\{0}\root\cimv2", remoteMachine), null);

                ms.Connect();

                if (ms.IsConnected)

                {

                    ObjectQuery query = new ObjectQuery(queryStr);

                    using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(ms, query))

                    {

                        using (ManagementObjectCollection searchResult = searcher.Get())

                        {

                            foreach (ManagementObject mo in searchResult)

                            {

                                foreach (PropertyData property in mo.Properties)

                                {

                                    Console.WriteLine("{0}:{1}", property.Name, mo.Properties[property.Name].Value == null ? "null" : mo.Properties[property.Name].Value.ToString());

                                }

                            }

                        }

                    }

                }

                return 1;

            }

            catch (Exception e)

            {

                Console.WriteLine(e.Message);

                return -1;

            }

            finally

            {

     

            }

        }

    }

       
    Sincerely,
    Eric
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Felyjos Sunday, February 14, 2010 11:29 PM
    Monday, February 8, 2010 2:38 AM

All replies

  • 1) The C# source code doesn't compile - you cannot return from Main which has void return type.
    2) Where is the line to execute the compiled executable in your batch file?
    3) Any application (C#, C++, assembler, anything), can return only error code (integer), not a string. You can access it from batch file via %ERRORLEVEL%.

    Do you need just find out if you are running on x86 OS, or on x64 OS (possible in WOW)?
    If yes, then use PROCESSOR_ARCHITECTURE and PROCESSOR_ARCHITEW6432 environment variables in your batch file.
    If not, either use the errorlevel as I suggested above (#3), or print the desired output to the console in your app and parse it in batch file (you can use e.g. for /F)

    BTW: Why your batch file needs to compile the application all the time? Just compile it once and use only the exe from your batch file.

    I believe that you can easily access WMI from JScript. Why not to use JScript instead of batch file then? (Its syntax is much easier than C# and batch files tricks)

    -Karel
    Saturday, February 6, 2010 10:20 PM
    Moderator
  • Hello,
    I was trying to get somethimg close to http://social.msdn.microsoft.com/Forums/en/clr/thread/5f61ff23-2da0-46c7-b01f-86c22fcb0610
    .



    1) The C# source code doesn't compile [quote] I thought this command will compile the C# %framework%\csc Architecture-Processor.cs[/quote]
    - you cannot return from Main which has void return type. [q]Excellent information. Do I need anyway a void? Should I add/replace the Main void with another type[/q] 2) Where is the line to execute the compiled executable in your batch file? [q]Opps good catch I think I missed it...
    I need to have something a line like
    Architecture-Processor.cs
    [/q]
    3) Any application (C#, C++, assembler, anything), can return only error code (integer), not a string. You can access it from batch file via %ERRORLEVEL%.
    [q]so it is impossible to get an information from C# and pass it to a batch file?[/q]
    Do you need just find out if you are running on x86 OS, or on x64 OS (possible in WOW)? If yes, then use PROCESSOR_ARCHITECTURE and PROCESSOR_ARCHITEW6432 environment variables in your batch file. If not, either use the errorlevel as I suggested above (#3), or print the desired output to the console in your app and parse it in batch file (you can use e.g. for /F) [q]I was using PROCESSOR_ARCHITECURE = "x86" etc.. it is limited as we could have x86, AMD64, IA64 etc...
    even some specifics where the name is even not specifying the 32 or 64 bits,
    also it seems I have to specify all processors one by one... [/q]
    BTW: Why your batch file needs to compile the application all the time? Just compile it once and use only the exe from your batch file.
    [q]good advice Thanks[/q] I believe that you can easily access WMI from JScript. Why not to use JScript instead of batch file then? (Its syntax is much easier than C# and batch files tricks) [q]C# is heavily used in our products so it makes easy to use it in case we need an interface[/q]
    Thanks for this huge help,
    Dom



    System Center Operations Manager 2007 / System Center Configuration Manager 2007 R2 Support
    Saturday, February 6, 2010 11:18 PM
  • Hi Felyjos,
    You may test your method in a separate C# project first before use it in batch.
    The given method cannot build in VS, ManagementObject is a build-in class of .NET, we need to change the class name to something others, for example, MyManagementObject.
    The given method doesn't take any parameters, that's why we cannot pass parameters to this method from batch file.
    I did a similar test, and it works in my side, hope following steps can hlep you:
    1. Create a .cs file: source.cs.
    2. In command window, input command: SET framework=%SystemRoot%\Microsoft.NET\Framework\v2.0.50727
    3. In command window, input command: %framework%\csc  source.cs
    4. In command window, input command: source.exe Win32_Processor
    5. You will see properties of Win32_Processor class.
    6.  source.cs is:

             using System;

    using System.Management;

     

    public class MyManUagementObject

    {

        public static int Main(string[] args)

        {

            if (args.Length == 0)

            {

                Console.WriteLine("Please input a WMI Class name.");

                return -1;

            }

            try

            {

                string remoteMachine = "localhost";

                string queryStr = "select * from " + args[0];

     

                ManagementScope ms = new ManagementScope(string.Format(@"\\{0}\root\cimv2", remoteMachine), null);

                ms.Connect();

                if (ms.IsConnected)

                {

                    ObjectQuery query = new ObjectQuery(queryStr);

                    using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(ms, query))

                    {

                        using (ManagementObjectCollection searchResult = searcher.Get())

                        {

                            foreach (ManagementObject mo in searchResult)

                            {

                                foreach (PropertyData property in mo.Properties)

                                {

                                    Console.WriteLine("{0}:{1}", property.Name, mo.Properties[property.Name].Value == null ? "null" : mo.Properties[property.Name].Value.ToString());

                                }

                            }

                        }

                    }

                }

                return 1;

            }

            catch (Exception e)

            {

                Console.WriteLine(e.Message);

                return -1;

            }

            finally

            {

     

            }

        }

    }

       
    Sincerely,
    Eric
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Felyjos Sunday, February 14, 2010 11:29 PM
    Monday, February 8, 2010 2:38 AM
  • Hi,
    I'm writing to check the issue status, please feel free to let us know if you have any concern.
    Sincerely,
    Eric
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, February 10, 2010 1:24 AM
  • I haven't tested yet as I am on SCCM this week...

    Sorry for the delay

    Thanks,
    Dom
    System Center Operations Manager 2007 / System Center Configuration Manager 2007 R2 Support
    Wednesday, February 10, 2010 1:45 AM
  • Hi,
    I see, it will be appreciated if you could share the test resut with us.
    Sincerely,
    Eric
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, February 11, 2010 1:11 AM
  • Hi,

    I will, no problem, as soon as I am out of the patches for SCOM and SCCM and the IIS MP upgrades and the SCOM CU1 for Windows and CU2 for UNIX I will be be on this... estimation 1 week more...

    Thanks,
    Dom
    System Center Operations Manager 2007 / System Center Configuration Manager 2007 R2 Support
    Thursday, February 11, 2010 2:08 AM
  • Hi Eric,

    The code works sucessfully by iteself all information are here. I trying to integrate it now.

    Thanks,
    Dom
    System Center Operations Manager 2007 / System Center Configuration Manager 2007 R2 Support
    Sunday, February 14, 2010 11:14 PM
  • Hi,

    I will, no problem, as soon as I am out of the patches for SCOM and SCCM and the IIS MP upgrades and the SCOM CU1 for Windows and CU2 for UNIX I will be be on this... estimation 1 week more...

    Thanks,
    Dom
    System Center Operations Manager 2007 / System Center Configuration Manager 2007 R2 Support

    Thanks for your analysis! Now I understand more about it.
    Thursday, September 9, 2010 2:55 AM