Jump to content
Strawberry Orange Banana Lime Leaf Slate Sky Blueberry Grape Watermelon Chocolate Marble
Strawberry Orange Banana Lime Leaf Slate Sky Blueberry Grape Watermelon Chocolate Marble

MSFN is made available via donations, subscriptions and advertising revenue. The use of ad-blocking software hurts the site. Please disable ad-blocking software or set an exception for MSFN. Alternatively, register and become a site sponsor/subscriber and ads will be disabled automatically. 

Sign in to follow this  
vinodreddy597

VBS to import registry based on Computer Name

Recommended Posts

Hi,

Here is my vbscript which checks the computer name and then it'll run the registry key if the first 3 letters of machine code matches. But unfortunately it's not working. Please help me..

'------------------------------
' Computer name
'------------------------------
Dim COMPUTER_NAME

Dim oFso, sShell, oShellEnv, target, source
Set oFso = CreateObject("Scripting.FileSystemObject")
Set oShell = WScript.CreateObject("WScript.Shell")
Set oShellEnv = oShell.Environment("Process")
COMPUTER_NAME = oShellEnv("ComputerName")

'MsgBox COMPUTER_NAME

'------------------------------
' First Three
'------------------------------
Dim FIRST_THREE

FIRST_THREE = Mid(COMPUTER_NAME, 1, 3)

'MsgBox FIRST_THREE

'------------------------------
' Get Run Registry
'------------------------------
Dim IS_RUN_REGISTRY

If FIRST_THREE = "HAC" Then
IS_RUN_REGISTRY = true
Else
IS_RUN_REGISTRY = false
End If

'MsgBox "[" & FIRST_THREE & "][" & IS_RUN_REGISTRY & "] "

'------------------------------
' Run Registry if needed
'------------------------------
Set regFile = "Genesis.reg"

If IS_RUN_REGISTRY Then
Set oShell = CreateObject("Wscript.Shell")
sRegFile = regFile

oShell.Run "regedit.exe /s " & Chr(34) & sRegFile & Chr(34), 0, True
End If

Edit by Gsm added code box

post-378619-0-53114000-1369992312_thumb.

Share this post


Link to post
Share on other sites

Your problem is this, the error means this is missing

'------------------------------' Run Registry if needed'------------------------------Set regFile = "Genesis.reg"
Here I redid the script so it a little more cleaner and added a FileExists check

'-> This code is property of Gunsmokingman and Or Jake1Eye and you must have his permission to use.'-> This is only posted as example code and meant only to used as such.'-> Objects For RuntimeDim Act :Set Act = CreateObject("WScript.Shell")Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")'-> ComputerNameDim CName :CName = Act.ExpandEnvironmentStrings("%ComputerName%")'MsgBox CName,4128,"Test Computer Name"Dim IS_RUN_REGISTRY'-> Check For RegistryHACIf UCase(Left(CName,3)) = "HAC" ThenIS_RUN_REGISTRY = TrueElseIS_RUN_REGISTRY = FalseEnd If'-> If IS_RUN_REGISTRY = True And The Reg File ExistsIf Fso.FileExists("Genesis.reg") And IS_RUN_REGISTRY Then' Act.Run("regedit.exe /s " & Chr(34) & "Genesis.reg" & Chr(34)), 0, TrueMsgBox "Confirm Reg File"ElseIf Fso.FileExists("Genesis.reg") And IS_RUN_REGISTRY = False ThenMsgBox "ComputerName match was False",4128,"No Match ComputerName"ElseMsgBox "Missing Genesis.reg please make sure it in the" & vbCrLf &_"Genesis.reg is in the same folder as this script",4128,"Missing Reg File"End If

Share this post


Link to post
Share on other sites
Yzöwl    30

It's not often that I see a VBS question and deliberately ignore a VBS solution, but at this time I see no good reason to ignore a Windows Command Script.

SCRIPT.CMD

@IF %COMPUTERNAME:~,3%' EQU HAC' REGEDIT /S GENESIS.REG

The two single quotes/apostrophes aren't necessary, they are included because they are harmless but improve the look of the code under the forum formatting scheme

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×