Friday, May 7, 2010

Howto: Add a local account to a local group using WMI

I had to write this script to add an account generated by Sophos AV to the local Users group in order for it to download software updates from the AV server post build.

Option Explicit

Dim oNET : Set oNET = CreateObject("WScript.Network")
Dim sComputer, objWMIService, colItems, objItem, objLocalUser, objUserGroup
sComputer = oNET.ComputerName

Set objWMIService = GetObject("winmgmts:\\" & sComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Account Where LocalAccount = True AND Name like '%partialusername%'",,48)
For Each objItem in colItems
Set objLocalUser = GetObject("WinNT://" & objItem.Name)
Set objUserGroup = GetObject("WinNT://" & sComputer & "/Users,group")
objUserGroup.Add(objLocalUser.ADsPath)
Next

WScript.Quit

Mitch

No comments: