Windows Scripting
  Home arrow Windows Scripting arrow Page 2 - Nilpo`s Scripting Secrets, Vol I
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
WINDOWS SCRIPTING

Nilpo`s Scripting Secrets, Vol I
By: Nilpo
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2008-12-15

    Table of Contents:
  • Nilpo`s Scripting Secrets, Vol I
  • Determine if a user is an Administrator
  • Wait for a program to end
  • More scripting secrets

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Nilpo`s Scripting Secrets, Vol I - Determine if a user is an Administrator


    (Page 2 of 4 )

    I have seen this one asked hundreds of times across various forums and newsgroups. “Is there a way to determine if a user is an Administrator?”  Quite often this question goes unanswered, but it doesn’t have to.

    Function IsAdminUser(strUserName)

       strUserName = Chr(34) & strUserName & Chr(34)

       strComputer = "."

       Set objWMIService = GetObject("winmgmts:" & strComputer & "rootcimv2")

       Set colInstances = objWMIService.ExecQuery("Select * From Win32_GroupUser")

    You begin by building a function that queries the Win32_GroupUser class. This class returns several instances that represent the installed users and their group assignments. Each instance will have a GroupComponent that represents the user group and a PartComponent that represents the user name. There will be one instance for every group that a user is in, so there can be several times as many instances as there are users.

       blnIsAdmin = False

       For Each objInstance In colInstances

           arrGroup = Split(objInstance.GroupComponent, ",")

           arrAccount = Split(objInstance.PartComponent, ",")

           strGroup = Right(arrGroup(1), Len(arrGroup(1)) - InStr(arrGroup(1), "="))

           strAccount = Right(arrAccount(1), Len(arrAccount(1)) - InStr(arrAccount(1), "="))

     

           If strGroup = """Administrators""" And strAccount = strUserName Then blnIsAdmin = True

       Next

    Since this query will return several instances, we’ll use a Boolean value to track the data we’re looking for. It should begin with a default value of false. Next, we loop through each instance, determining the user name and group assignment.  If a particular instance finds the user we’re looking for listed with an administrator user group, then the Boolean value is set to true.

       IsAdminUser = blnIsAdmin

    End Function

    The function then returns that Boolean value. So the function can be used like this:

    Set WshNetwork = CreateObject("WScript.Network")

    strUserName = WshNetwork.UserName

     

    If IsAdminUser(strUserName) Then

       WScript.Echo strUserName, "is an administrator."

    Else

       WScript.Echo strUserName, "is not an administrator."

    End If

    More Windows Scripting Articles
    More By Nilpo


       · Thank you very much for those valuable tips.Reading your article gave me an...
       · Hi Patrick,You make a very good point, but there's one small mistake in your...
       · Sorry, you misread my comment. Reading every line but the last line was precisely...
       · My apologies. In that case your example is spot on. Thanks for sharing with our...
     

    WINDOWS SCRIPTING ARTICLES

    - More Windows Scripting Workarounds from Nilpo
    - Overloading Methods and More in VBScript
    - Improving MFC for Windows Vista
    - Regular Expressions in VBScript
    - Working with Dates in WMI
    - Completing Calendars with VBScript Date Func...
    - Building Calendars with VBScript Date Functi...
    - Working With Dates and Times in VBScript
    - Designing WCF DataContract Classes Using the...
    - Understanding Dates and Times in VBScript
    - Working With Arrays in VBScript
    - Compressed Folders in WSH
    - Using .NET Interops in VBScript
    - Nilpo`s Scripting Secrets, Vol I
    - Database operations using Silverlight 2.0 WC...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek