• Announcements

    • xper

      MSFN Sponsorship and AdBlockers!   07/10/2016

      Dear members, MSFN is made available via subscriptions, donations and advertising revenue. The use of ad-blocking software hurts the site. Please disable ad-blocking software or set an exception for MSFN. Alternatively, become a site sponsor and ads will be disabled automatically and by subscribing you get other sponsor benefits.
Sign in to follow this  
Followers 0
vinodreddy597

VBS to import registry based on Computer Name

3 posts in this topic

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.

0

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
0

Share this post


Link to post
Share on other sites

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

0

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  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.