You must Sign In to post a response.
  • Category: .NET

    Remote ShutDown

    The forum thread has not been reviewed by Editors yet. Readers are advised to use their best judgement before accessing this thread.
    This thread will be reviewed shortly.
    If you think this thread contain inappropriate content, please report to webmaster.
    Hello,
    Using Wmi Win32_OperatingSystem class . Im able to shutdown my local computer.
    But for the same if i try with remote system it show access denied.Can any one
    Help regd this.Thanks in advance .
    Wther should i use the the moniker as impersonateLevel or debug Shutdown .
    Can i use the Vb Script ,with some Syntax change in .net.
    Coding as follow :
    OpSysSet=GetObject("winmgmts:{(Debug,RemoteShutdown)}//"
    & ComputerName & "/root/cimv2").ExecQuery(
    "Select * from Win32_OperatingSystem where Primary=true");
  • #1108
    cool.. you got two ways:

    1. writing a client and server. invoke the client, by sending a value from server. The client will get the value and run the same WMI script u run to shutdown the local machine [self shutdown].

    2. Second way is, u should know the admin username and password. if u r admin, following is the script you can use.


    ' With VBScript and Windows Management Instrumentation (WMI).

    Dim oFSO, oTS, sClient, oWindows, oLocator, oConnection, oSys
    Dim sUser, sPassword

    'set remote credentials
    sUser = "Administrator"
    sPassword = "password"

    'open list of client names
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Set oTS = oFSO.OpenTextFile("C:\clients.txt")

    Do Until oTS.AtEndOfStream

    'get next client name
    sClient = oTS.ReadLine

    'get WMI locator
    Set oLocator = CreateObject("WbemScripting.SWbemLocator")

    'Connect to remote WMI
    Set oConnection = oLocator.ConnectServer(sClient, _
    "root\cimv2", sUser, sPassword)

    'issue shutdown to OS
    ' 4 = force logoff
    ' 5 = force shutdown
    ' 6 = force rebooot
    ' 12 = force power off
    Set oWindows = oConnection.ExecQuery("Select " & _
    "Name From Win32_OperatingSystem")
    For Each oSys In oWindows
    oSys.Win32ShutDown(5)
    Next

    Loop

    'close the text file
    oTS.Close
    WScript.Echo "Successfully done!"



    NOTE:
    1. First, you need to have a text file named C:\Clients.txt on the machine where the script runs. That file should contain the list of client names you want to shut down.

    2. Instead of using a text file, you can modify this script to use ADSI, enabling it to query computer names from Active Directory or an NT domain. or by running 'net view' and write in a text file in C:\Clients.txt

    3. However, as you probably don't want to run this script against every computer on your network (shutting down all of your servers might be bad), the text file provides you with complete control over which computers the script will affect.

    4.You can also pass the Username and password. dont hardcode it.

  • #1111
    Thanz for your valiable response.
    Can i try the same code in c# with some Management class .wther it works or only
    Possible with c#.but my associate machine doesnt have any password what shall i do for those parament.i tried by passing "".But access denied is the result.

    With Regards

    Senthil

  • #1112
    1. Yes. you can do in C# too, keeping the same logic..
    2. Use Admin Account's username and password and not your associate's system username and password.

  • #1114
    Have you tried the same in c# , I tried with ManagementScope Class it connects to the remote Machine and fails.
    Wther should i make any Properties change of the the associate Systems Wmi Environment.I enabled the Security to ALL users .is there any thing more to be done.
    Thanz for ur concern for spending ur valuable Time

  • #1115
    this C# code will do the work..



    using System.Management;

    public class RemoteShutDown
    [StructLayout(LayoutKind.Sequential, Pack=1)]
    internal struct TokPriv1Luid
    {
    public int Count;
    public long Luid;
    public int Attr;
    }

    [DllImport("kernel32.dll", ExactSpelling=true) ]
    internal static extern IntPtr GetCurrentProcess();

    [DllImport("advapi32.dll", ExactSpelling=true, SetLastError=true) ]
    internal static extern bool OpenProcessToken( IntPtr h, int acc, ref IntPtr phtok );

    [DllImport("advapi32.dll", SetLastError=true) ]
    internal static extern bool LookupPrivilegeValue( string host, string name, ref long pluid );

    [DllImport("advapi32.dll", ExactSpelling=true, SetLastError=true) ]
    internal static extern bool AdjustTokenPrivileges( IntPtr htok, bool disall,
    ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen );

    [DllImport("user32.dll", ExactSpelling=true, SetLastError=true) ]
    internal static extern bool ExitWindowsEx( int flg, int rea );

    internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
    internal const int TOKEN_QUERY = 0x00000008;
    internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
    internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
    internal const int EWX_LOGOFF = 0x00000000;
    internal const int EWX_SHUTDOWN = 0x00000001;
    internal const int EWX_REBOOT = 0x00000002;
    internal const int EWX_FORCE = 0x00000004;
    internal const int EWX_POWEROFF = 0x00000008;
    internal const int EWX_FORCEIFHUNG = 0x00000010;




    private void RShutdown()
    {
    DoExitWin(EWX_REBOOT + EWX_FORCE);
    }

    private void DoExitWin( int flg )
    {
    bool ok;
    TokPriv1Luid tp;
    IntPtr hproc = GetCurrentProcess();
    IntPtr htok = IntPtr.Zero;
    ok = OpenProcessToken( hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok );
    tp.Count = 1;
    tp.Luid = 0;
    tp.Attr = SE_PRIVILEGE_ENABLED;
    ok = LookupPrivilegeValue( null, SE_SHUTDOWN_NAME, ref tp.Luid );
    ok = AdjustTokenPrivileges( htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero );
    ok = ExitWindowsEx( flg, 0 );
    }

    }


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.