reboot machine

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Avi G

    reboot machine

    Hi,

    what is the quick code in C# for reboot local machine?

    Thanks.
  • Michael Nemtsev

    #2
    Re: reboot machine

    Hello Avi,

    Use WMI for this, it's natural way

    Sub RebootComputer( strServer )
    Set objOSSet = GetObject("winm gmts:{(RemoteSh utdown)}//" & strServer
    & "/root/cimv2").ExecQue ry("select * from Win32_Operating System where Primary=true")

    For each objOS in objOSSet
    objOS.Reboot()
    Next
    End Sub


    AG> Hi,
    AG>
    AG> what is the quick code in C# for reboot local machine?
    AG>
    AG> Thanks.
    AG>
    ---
    WBR,
    Michael Nemtsev :: blog: http://spaces.msn.com/laflour

    "At times one remains faithful to a cause only because its opponents do not
    cease to be insipid." (c) Friedrich Nietzsche


    Comment

    • Avi G

      #3
      Re: reboot machine

      i'm looking for the code in C# for reboot machine...

      "Michael Nemtsev" wrote:
      [color=blue]
      > Hello Avi,
      >
      > Use WMI for this, it's natural way
      >
      > Sub RebootComputer( strServer )
      > Set objOSSet = GetObject("winm gmts:{(RemoteSh utdown)}//" & strServer
      > & "/root/cimv2").ExecQue ry("select * from Win32_Operating System where Primary=true")
      >
      > For each objOS in objOSSet
      > objOS.Reboot()
      > Next
      > End Sub
      >
      >
      > AG> Hi,
      > AG>
      > AG> what is the quick code in C# for reboot local machine?
      > AG>
      > AG> Thanks.
      > AG>
      > ---
      > WBR,
      > Michael Nemtsev :: blog: http://spaces.msn.com/laflour
      >
      > "At times one remains faithful to a cause only because its opponents do not
      > cease to be insipid." (c) Friedrich Nietzsche
      >
      >
      >[/color]

      Comment

      • Michael Nemtsev

        #4
        Re: reboot machine

        Hello Avi,

        Did you try to look in MSDN for the info how to use WMI (Management namespace)
        and try to convert my script to C#?
        It takes less than 5 mins for this (sample in MSDN is available, you need
        only point correct query and method)

        //Connect to the remote computer
        System.Manageme nt.ManagementSc ope ms = new ManagementScope ("\\\\<your_ser ver_name>\\root \\cimv2");
        ms.Connect();
        //Query remote computer across the connection
        System.Manageme nt.ObjectQuery oq = new System.Manageme nt.ObjectQuery( "SELECT
        * FROM Win32_Operating System");

        ManagementObjec tSearcher query1 = new ManagementObjec tSearcher(ms,
        oq);
        ManagementObjec tCollection queryCollection 1 = query1.Get();

        foreach (ManagementObje ct mo in queryCollection 1)
        {
        string[] ss ={ "" };
        mo.InvokeMethod ("Reboot", ss);
        Console.WriteLi ne(mo.ToString( ));
        }

        AG> i'm looking for the code in C# for reboot machine...
        AG>
        AG> "Michael Nemtsev" wrote:
        AG>[color=blue][color=green]
        >> Hello Avi,
        >>
        >> Use WMI for this, it's natural way
        >>
        >> Sub RebootComputer( strServer )
        >> Set objOSSet = GetObject("winm gmts:{(RemoteSh utdown)}//" & strServer
        >> & "/root/cimv2").ExecQue ry("select * from Win32_Operating System where
        >> Primary=true")
        >> For each objOS in objOSSet
        >> objOS.Reboot()
        >> Next
        >> End Sub
        >> AG> Hi,
        >> AG>
        >> AG> what is the quick code in C# for reboot local machine?
        >> AG>
        >> AG> Thanks.
        >> AG>
        >> ---
        >> WBR,
        >> Michael Nemtsev :: blog: http://spaces.msn.com/laflour
        >> "At times one remains faithful to a cause only because its opponents
        >> do not cease to be insipid." (c) Friedrich Nietzsche
        >>[/color][/color]
        ---
        WBR,
        Michael Nemtsev :: blog: http://spaces.msn.com/laflour

        "At times one remains faithful to a cause only because its opponents do not
        cease to be insipid." (c) Friedrich Nietzsche


        Comment

        • Jani Järvinen [MVP]

          #5
          Re: reboot machine

          Hi Avi,
          [color=blue]
          > what is the quick code in C# for reboot local machine?[/color]

          Unfortunately, there's no "one-liner" that you could call in C# to reboot
          the machine. Instead, it is a little bit more complex due to special
          privileges you will need. The Win32 API function to reboot the machine is
          ExitWindowsEx. See this page for details:



          Now, to the C# code. There is the direct P/Invoke route, and the WMI route
          that Michael Nemtsev pointer out. Try this page for tips:

          Choosing the right web hosting company is one of the most important — and most consequential — decisions you’ll make when starting or growing your website.


          Good luck!

          --
          Regards,

          Mr. Jani Järvinen
          C# MVP
          Helsinki, Finland
          janij@removethi s.dystopia.fi




          Comment

          • Michael Nemtsev

            #6
            Re: reboot machine

            Hello Jani Järvinen [MVP],

            There is the sample of using ExitWindowsEx from C#

            [color=blue][color=green]
            >> what is the quick code in C# for reboot local machine?
            >>[/color][/color]
            J> Unfortunately, there's no "one-liner" that you could call in C# to
            J> reboot the machine. Instead, it is a little bit more complex due to
            J> special privileges you will need. The Win32 API function to reboot
            J> the machine is ExitWindowsEx. See this page for details:
            J>
            J> http://msdn.microsoft.com/library/de...ary/en-us/shut
            J> down/base/exitwindowsex.a sp
            J>
            J> Now, to the C# code. There is the direct P/Invoke route, and the WMI
            J> route that Michael Nemtsev pointer out. Try this page for tips:
            J>
            J> http://www.dotnet247.com/247reference/msgs/9/49583.aspx

            ---
            WBR,
            Michael Nemtsev :: blog: http://spaces.msn.com/laflour

            "At times one remains faithful to a cause only because its opponents do not
            cease to be insipid." (c) Friedrich Nietzsche


            Comment

            • Mark Wilden

              #7
              Re: reboot machine

              "Avi G" <AviG@discussio ns.microsoft.co m> wrote in message
              news:0A3F1041-2BCE-40AE-A19D-5576D5402AD5@mi crosoft.com...[color=blue]
              >
              > what is the quick code in C# for reboot local machine?[/color]

              Reminds me of the good old days, when you could write REBOOT.COM (using DOS
              DEBUG) in a couple of asm statements...


              Comment

              • Mark Rae

                #8
                Re: reboot machine

                "Mark Wilden" <MarkWilden@new sgroups.nospam> wrote in message
                news:eu4gVh0hGH A.4864@TK2MSFTN GP05.phx.gbl...
                [color=blue]
                > "Avi G" <AviG@discussio ns.microsoft.co m> wrote in message
                > news:0A3F1041-2BCE-40AE-A19D-5576D5402AD5@mi crosoft.com...[color=green]
                >>
                >> what is the quick code in C# for reboot local machine?[/color]
                >
                > Reminds me of the good old days, when you could write REBOOT.COM (using
                > DOS DEBUG) in a couple of asm statements...[/color]

                :-) I still have a copy of that on an old floppy disk somewhere... :-)


                Comment

                • Willy Denoyette [MVP]

                  #9
                  Re: reboot machine


                  "Jani Järvinen [MVP]" <janij@removeth is.dystopia.fi> wrote in message
                  news:OK$aZpwhGH A.1320@TK2MSFTN GP04.phx.gbl...
                  | Hi Avi,
                  |
                  | > what is the quick code in C# for reboot local machine?
                  |
                  | Unfortunately, there's no "one-liner" that you could call in C# to reboot
                  | the machine. Instead, it is a little bit more complex due to special
                  | privileges you will need. The Win32 API function to reboot the machine is
                  | ExitWindowsEx. See this page for details:
                  |
                  |

                  |
                  | Now, to the C# code. There is the direct P/Invoke route, and the WMI route
                  | that Michael Nemtsev pointer out. Try this page for tips:
                  |
                  | http://www.dotnet247.com/247reference/msgs/9/49583.aspx
                  |
                  | Good luck!
                  |

                  Add to that a simple Process.Start call that issues the shutdown.exe
                  program.

                  Willy.


                  Comment

                  Working...