CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2006
    Posts
    41

    JScript to detect if Windows user admin

    I need JScript that will be launched on Windows from command line. The script shoudl detect (using WScript, I guess) is the current user local admin on given computer or no.

    How could I do this?

  2. #2
    Join Date
    Jun 2009
    Posts
    113

    Re: JScript to detect if Windows user admin

    Code:
    var objNetwork=new ActiveXObject("WScript.Network");
    var objAdmins = GetObject("WinNT://"+objNetwork.ComputerName+"/Administrators");
    var colMembers = objAdmins.Members();
    var Admin=false;
    for (var enumItems=new Enumerator(colMembers); !enumItems.atEnd(); enumItems.moveNext()) {
    	var objMember=enumItems.item();
    	if (objNetwork.UserName.toLowerCase()==objMember.Name.toLowerCase()) Admin=true;
    }
    (Admin==true) ? WScript.Echo("Yes, you are a local admin") : WScript.Echo("Tough luck, you're no admin");
    Last edited by PeejAvery; July 31st, 2009 at 12:55 PM. Reason: Added code tags.

  3. #3
    Join Date
    Jul 2005
    Location
    Currently in Mexico City
    Posts
    568

    Re: JScript to detect if Windows user admin

    Code:
    var net = WScript.CreateObject("WScript.Network");
    var shell = WScript.CreateObject("WScript.Shell");
    
    var exec = shell.Exec("net localgroup administrators");
    var result = exec.StdOut.ReadAll();
    WScript.Echo("admin: "+(result.indexOf("\n"+net.UserName+"\r")!=-1));
    net localgroup administrators | find "%USERNAME%" fails somehow... =S If not would be 3 lines only script...
    Last edited by Xeel; July 31st, 2009 at 02:03 PM.
    Wanna install linux on a vacuum cleaner. Could anyone tell me which distro sucks better?

    I had a nightmare last night. I was dreaming that I’m 64-bit and my blanket is 32-bit and I couldn’t cover myself with it, so I’ve spent the whole night freezing. And in the morning I find that my blanket just had fallen off the bed. =S (from: bash.org.ru)

    //always looking for job opportunities in AU/NZ/US/CA/Europe :P
    willCodeForFood(Arrays.asList("Java","PHP","C++","bash","Assembler","XML","XHTML","CSS","JS","PL/SQL"));

    USE [code] TAGS! Read this FAQ if you are new here. If this post was helpful, please rate it!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured