• 0

VB Script- Getting Computer Name


Question

Hello, i found a script online to enable or disable a local user account, unfortunately i wanted to deploy this on AD and the script is name specific, im not much of a scripter but can i query the computer name in the script and then pass it to the string? This is what i have right now which doesn't work

*Modified*

' From the book "Windows XP Cookbook"

' ISBN: 0596007256

' ------ SCRIPT CONFIGURATION ------

' Set to FALSE to disable account or TRUE to enable account

Dim objUser

on error resume next

strDisableAccount = FALSE

strUserName = "HelpAssistant" ' e.g. rallen

strComputer = "objUser.ComputerName"

' ------ END CONFIGURATION ---------

set objUser = GetObject("WinNT://" & strComputer & "/" & strUserName)

if objUser.AccountDisabled = TRUE then

WScript.Echo "Account for " & objUser.Name & " currently disabled"

if strDisableAccount = FALSE then

objUser.AccountDisabled = strDisableAccount

objUser.SetInfo

WScript.Echo "Account enabled"

end if

else

WScript.Echo "Account currently enabled"

if strDisableAccount = TRUE then

objUser.AccountDisabled = strDisableAccount

objUser.SetInfo

WScript.Echo "Account disabled"

end if

end if

*Original*

' From the book "Windows XP Cookbook"

' ISBN: 0596007256

' ------ SCRIPT CONFIGURATION ------

' Set to FALSE to disable account or TRUE to enable account

strDisableAccount = FALSE

strUserName = "HelpAssistant" ' e.g. rallen

strComputer = ""

' ------ END CONFIGURATION ---------

set objUser = GetObject("WinNT://" & strComputer & "/" & strUserName)

if objUser.AccountDisabled = TRUE then

WScript.Echo "Account for " & objUser.Name & " currently disabled"

if strDisableAccount = FALSE then

objUser.AccountDisabled = strDisableAccount

objUser.SetInfo

WScript.Echo "Account enabled"

end if

else

WScript.Echo "Account currently enabled"

if strDisableAccount = TRUE then

objUser.AccountDisabled = strDisableAccount

objUser.SetInfo

WScript.Echo "Account disabled"

end if

end if

Link to comment
Share on other sites

11 answers to this question

Recommended Posts

  • 0

you could use the built in environmental variable in windows?

%computername%

or just this

WScript.Echo Wscript.CreateObject("Wscript.Network").ComputerName

Edited by BGM
Link to comment
Share on other sites

  • 0

Hmm good idea, when i tried that, i get a Network path not found error on line 10 char 1 which is this line i believe--- (set objUser = GetObject("WinNT://" & strComputer & "/" & strUserName)

Link to comment
Share on other sites

  • 0

here is how to use the environment variable

WScript.Echo Wscript.CreateObject("Wscript.Shell").ExpandEnvironmentStrings("%COMPUTERNAME%")

you can obviously set that to a string without too much hassle :)

Link to comment
Share on other sites

  • 0

im guessing adding it after the string is not how you do it , the script runs but the account isn't disabled, if there was presumed knowledge of scripting on my end.. thats all done now.

strDisableAccount = FALSE

strUserName = "HelpAssistant" ' e.g. rallen

strComputer = "%ComputerName%"

WScript.Echo Wscript.CreateObject("Wscript.Shell").ExpandEnvironmentStrings("%COMPUTERNAME%")

' ------ END CONFIGURATION ---------

set objUser = GetObject("WinNT://" & strComputer & "/" & strUserName)

Link to comment
Share on other sites

  • 0

well, all WScript.Echo does is pop up a message showing you the information

what you want to do is

strComputer = Wscript.CreateObject("Wscript.Shell").ExpandEnvironmentStrings("%COMPUTERNAME%")

Link to comment
Share on other sites

  • 0

Yea nice, thanks for the hand holding, i didn't know you could make a string equal so much :) thanks though. So now i have :

strDisableAccount = FALSE

strUserName = HelpAssistant ' e.g. rallen

strComputer = Wscript.CreateObject("Wscript.Shell").ExpandEnvironmentStrings("%COMPUTERNAME%")

' ------ END CONFIGURATION ---------

set objUser = GetObject("WinNT://" & strComputer & "/" & strUserName)

When i try and run it, it errors at that set objUser code:80005000

Link to comment
Share on other sites

  • 0

ka0s, this works fine for me... you just have to run it with elevated permissions, have you tried that?

also make sure "HelpAssistant" has the speech marks around it.. it is probably moaning because it can't find that user, but I'm unsure.

you could put some error checking in ?

Link to comment
Share on other sites

  • 0

Thanks so much for your help, i had to put the account in quotes stupid me, but i don't have vista so XP loves to run admin all day :) you saved me from touching 1500 computers! thanks

Link to comment
Share on other sites

  • 0

Lol, shame on my low level scripting skills. I was able to disable the wscript.echo! lol everyone in the workstation OU would be calling about those dialogs i already know, thanks again .. this time for not charging :)

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.