Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get locally logged on username, input name into this script

Status
Not open for further replies.

Cstorms

IS-IT--Management
Sep 29, 2006
556
US
Hello.

Environment = WinXP client, joined to AD domain. User logged in as their user account.


Here is the desired result. User logs in, script runs, gathers username of whoevers logged in. Adds that user to the local administrators group. I found this script but requires the manual entry of a user name.

Dim DomainName
Dim UserAccount
Set net = WScript.CreateObject("WScript.Network")
local = net.ComputerName
DomainName = "DomainName"
UserAccount = "userAccount"
set group = GetObject("WinNT://"& local &"/Administrators") on error resume next
group.Add "WinNT://"& DomainName &"/"& UserAccount &"" CheckError sub
CheckError if not err.number=0
then set ole = CreateObject("ole.err") MsgBox ole.oleError (err.Number), vbCritical err.clear
else
MsgBox "Done."
end if
end sub

I know you guys are certified geniuses here so please lend me a hand :)

Thanks in advance!


Cory
 
Ok I found this on the MS site and was wondering if it would be possible to incorporate this into the previous script (even though I never tested the previous one to see if it works..), however I know this retured the right username.


strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * From Win32_ComputerSystem")

For Each objItem in colItems
arrName = Split(objItem.UserName, "\")
Wscript.Echo "Domain: " & arrName(0)
Wscript.Echo "Name: " & arrName(1)
Next


Could I take the arrName(1) variable and plug it into the first script somehow?

Thanks!

Cory
 
>DomainName = "DomainName"
[tt]DomainName = net.userdomain[/tt]
>UserAccount = "userAccount"
[tt]UserAccount = net.username[/tt]
 
For those interested here is the script I went with, with a bit of customizing.. Found on a random forum post so I cant give credit appropriately.. Anyway..

Gets all the info (domain name, username) and adds the current logged in username to the local admin group. This is going to be used in a migration scenario so its pretty pointless for most things..


Set objWshNet = CreateObject("WScript.Network")

' // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' // Configure basic script variables

strDomain = objWshNet.UserDomain
strComputer = objWshNet.ComputerName
Set objGroup = GetObject("WinNT://" & strComputer & "/Administrators,group")


' // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' // Configure to add a domain user to the Local Administrators Group


strUser = objWshNet.UserName
Set objUser = GetObject("WinNT://" & strDomain & "/" & strUser & ",user")


' // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' // Configure to add a domain group to the Local Administrators Group

'strUser = "domaingrouphere'
'Set objUser = GetObject("WinNT://" & strDomain & "/" & strUser & ",group")


' // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' // We actually add the user or group here, if not already a member of the local
' // Administrators group:

If Not objGroup.IsMember(objUser.ADsPath) Then
objGroup.Add(objUser.ADsPath)
End If

Cory
 
I only see your searching skill is more agile by the days.
 
haha as soon as i found out what to search for :) thanks for the reply, i appreciate it.

Cory
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top