Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

check and add a registry key

647 views
Skip to first unread message

tree leafs

unread,
Mar 7, 2008, 4:16:28 AM3/7/08
to
Hi,
is it possible to have a script that checks a remote PC (running XP pro) to
see whether a particular registry key exists, if not run a xyz.reg file to
add it on the remote PC?
thanks in advance,


Monitor

unread,
Mar 7, 2008, 7:18:49 AM3/7/08
to
"tree leafs" <tree...@hotmail.com> wrote in message
news:#JO2nPDg...@TK2MSFTNGP02.phx.gbl...

In answer to your question: Yes, it is possible to do this
with VB scripts or with batch files.


Thomas - MCSE/MCSA/MCDBA@discussions.microsoft.com Corey Thomas - MCSE/MCSA/MCDBA

unread,
Mar 7, 2008, 4:54:01 PM3/7/08
to
Yes, you can read the registry of a remote computer and you can even add or
change keys and values. However, if you are looking for the HKCU hive, you
will need to do some round-a-bout coding.

First, when you attempt to access HKCU with a script, it pulls the HKCU for
the account that is running the script. For example, your name is Bob and
you are trying to pull the HKCU for Sue on computer B. When your script
runs, it doesn't pull the HKCU for Sue, even though she's logged on. It will
pull your HKCU as if you were logged on.

To get around this:

First, query the computer for the current logged on user.
Next, get the SID for that account.
Then access HKU (HKEY_USERS) and start with that SID.


If you need code to do any of this, let me know!

-Corey Thomas

tree leafs

unread,
Mar 7, 2008, 8:59:36 PM3/7/08
to
Thanks, would appreciate some sample code.
Cheers,

"Monitor" <nos...@spam.com> wrote in message
news:%23QWJo1E...@TK2MSFTNGP03.phx.gbl...

tree leafs

unread,
Mar 7, 2008, 9:03:18 PM3/7/08
to
Thank you very much Corey.
Yes, it would be great if you could provide some samaple codes.
In my case I currently only want to add a new DWORD value in HKLM if it does
not exist.
thanks in advance,

"Corey Thomas - MCSE/MCSA/MCDBA" <Corey Thomas -
MCSE/MCSA/MC...@discussions.microsoft.com> wrote in message
news:A9FC5517-EF92-4051...@microsoft.com...

Pegasus (MVP)

unread,
Mar 8, 2008, 6:45:26 PM3/8/08
to

"tree leafs" <tree...@hotmail.com> wrote in message
news:OM0dXAMg...@TK2MSFTNGP04.phx.gbl...

Here is some sample code:

Const sMode = "1" '1: AutoLogon=on, 0: AutoLogon=off
Const sComputer="Nalle"
Const HKEY_LOCAL_MACHINE = &H80000002 'HKLM hive
Const sKeyPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"
Const sValueName = "AutoAdminLogon"

Dim oReg, sCurrValue, iResult, sSuccess, sFailure, sText
Dim LF: LF = Chr(10)

On Error Resume Next
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
sComputer & "\root\default:StdRegProv")
If Error > 0 Then
MsgBox "Cannot reach this machine!", 0, "Set AutoLogon on " & sComputer
WScript.Quit
End If
On Error Goto 0

oReg.GetStringValue HKEY_LOCAL_MACHINE, sKeyPath, sValueName, sCurrValue
iResult = oReg.SetStringValue (HKEY_LOCAL_MACHINE, sKeyPath, sValueName,
sMode)


Corey Thomas - MCSE/MCSA/MCDBA

unread,
Mar 10, 2008, 9:04:01 AM3/10/08
to
First, get the current user for the machine:

'==================================================================
Function GetCurrentUser(strComputer)
'Input: strComputer = machine to query
'Output: Current User as domain\logon
'On Error Resume Next

Set objWMIService = GetObject("winmgmts:" &
"{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery("Select * from Win32_Process
Where Name = 'explorer.exe'")
For Each objProcess in colProcessList
objProcess.GetOwner strUserName, strUserDomain
'Wscript.Echo strComputer & ",Is logged into by " & strUserDomain & "\"
& strUserName
Next

GetCurrentUser = strUserDomain & "\" & strUserName

End Function
'==================================================================

Then use the current user to get the SID:

'==================================================================
Function GetSIDFromUser(UserName)
'Input: UserName as domain\logon
'Output: SID

Dim DomainName, Result, WMIUser

If InStr(UserName, "\") > 0 Then
DomainName = Mid(UserName, 1, InStr(UserName, "\") - 1)
UserName = Mid(UserName, InStr(UserName, "\") + 1)
Else
DomainName = CreateObject("WScript.Network").UserDomain
End If

On Error Resume Next
Set WMIUser = GetObject("winmgmts:{impersonationlevel=impersonate}!" _
& "/root/cimv2:Win32_UserAccount.Domain='" & DomainName & "'" _
& ",Name='" & UserName & "'")
If Err = 0 Then Result = WMIUser.SID Else Result = ""
On Error GoTo 0

GetSIDFromUser = Result
End Function

'==================================================================

Now you have the SID, find the key you need in HKU\SID\... and that will be
the actual current user for that machine.

Saravana

unread,
Mar 10, 2008, 2:50:03 PM3/10/08
to

U can check for the key existance using....

HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."


Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\"

&strComputer & "\root\default:StdRegProv")
strKeyPath = "SOFTWARE\RealNetworks\RealPlayer"
if oReg.EnumKey (HKEY_LOCAL_MACHINE, strKeyPath, arrSubKeys) = 0 then
'key exists..
'do this...
end if

Murali KRishna

unread,
Dec 16, 2008, 10:23:33 AM12/16/08
to

Hi,
I want to Check for a registry key in a remote machine and if it exists
grep the key and run uninstaller of an app.
Is it possible to have a batch script that checks a remote PC (running
XP pro) tosee whether a particular registry key exists on the remote PC?

Thanks in advance,
Murali.


*** Sent via Developersdex http://www.developersdex.com ***

Pegasus (MVP)

unread,
Dec 16, 2008, 3:32:36 PM12/16/08
to

"Murali KRishna" <muralimk...@yahoo.com> wrote in message
news:OXEBDJ5X...@TK2MSFTNGP02.phx.gbl...

Are you looking for a batch file or a VB Script solution? If it's a VB
Script solution then this link should help:
http://www.microsoft.com/technet/scriptcenter/guide/sas_reg_famr.mspx?mfr=True.
Replace [strComputer = "."] with [strComputer = "ComputerName" to query a
remote machine.

If you're after a batch file solution then here is an example that will do
the trick: reg query HKEY_CURRENT_USER\Console. To query a remote machine,
use psexec.exe (www.sysinternals.com) to invoke reg.exe.


0 new messages