Jump to content

Welcome to MSFN Forum
Register now to gain access to all of our features. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more. This message will be removed once you have signed in.
Login to Account Create an Account



Photo

VBS to import registry based on Computer Name

- - - - -

  • Please log in to reply
2 replies to this topic

#1
vinodreddy597

vinodreddy597
  • Member
  • 1 posts
  • Joined 31-May 13
  • OS:Windows 7 x64
  • Country: Country Flag
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

Attached Files




How to remove advertisement from MSFN

#2
gunsmokingman

gunsmokingman

    MSFN Master

  • Super Moderator
  • 2,419 posts
  • Joined 02-August 03
  • OS:none specified
  • Country: Country Flag
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 Runtime
Dim Act :Set Act = CreateObject("WScript.Shell")
Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")
'-> ComputerName
Dim CName :CName = Act.ExpandEnvironmentStrings("%ComputerName%")

'MsgBox CName,4128,"Test Computer Name"

Dim IS_RUN_REGISTRY
'-> Check For RegistryHAC
If UCase(Left(CName,3)) = "HAC" Then
IS_RUN_REGISTRY = True
Else
IS_RUN_REGISTRY = False
End If

'-> If IS_RUN_REGISTRY = True And The Reg File Exists
If Fso.FileExists("Genesis.reg") And IS_RUN_REGISTRY Then
' Act.Run("regedit.exe /s " & Chr(34) & "Genesis.reg" & Chr(34)), 0, True
MsgBox "Confirm Reg File"
ElseIf Fso.FileExists("Genesis.reg") And IS_RUN_REGISTRY = False Then
MsgBox "ComputerName match was False",4128,"No Match ComputerName"
Else
MsgBox "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



GunSmokingMan



#3
Yzöwl

Yzöwl

    Wise Owl

  • Super Moderator
  • 4,540 posts
  • Joined 13-October 04
  • OS:Windows 7 x64
  • Country: Country Flag

Donator

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 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users