if wscript.arguments.count < 2 then usage if instr(wscript.arguments(0),"/") then usage if instr(wscript.arguments(0),"?") then usage if instr(wscript.arguments(0),"-") then usage sFunction=lcase(wscript.arguments(0)) sRegistryKey=wscript.arguments(1) sRegValueType="REG_SZ" if wscript.arguments.count=3 then sRegValueType=trim(ucase(wscript.arguments(2))) end if if left(sRegistryKey,2)="\\" then sServer=mid(sRegistrykey,3,instr(3,sRegistryKey,"\")-3) sRegistryKey=right(sRegistryKey,len(sRegistrykey)-instr(3,sRegistryKey,"\")) else sServer="." end if select case sFunction case "enum" aKeys=RegEnum(sServer,sRegistryKey) if not IsArray(aKeys) then end if for each sKey in aKeys wscript.echo sKey next case "enumkeys" aKeys=RegEnumKeys(sServer,sRegistryKey) if not IsArray(aKeys) then end if for each sKey in aKeys wscript.echo sKey next case "enumvalues" aKeys=RegEnumValues(sServer,sRegistryKey) if not IsArray(aKeys) then end if for each sKey in aKeys wscript.echo sKey next case "read" wscript.echo RegGetValue (sServer,sRegistryKey) case "write" wscript.echo RegWriteValue (sServer,sRegistryKey,sRegValueType) case else wscript.echo "Can't recognize function "&sFunction&"." call usage end select 'wscript.echo RegGetValue (sServer,sRegistryKey) 'Type 2 'wscript.echo RegGetValue ("pga10664","system\currentcontrolset\services\netbios\linkage\rouaa") 'Type 2 'wscript.echo RegGetValue ("pga10664","system\currentcontrolset\services\acpi\ImagePath") 'Type 2 'wscript.echo RegGetValue ("usgctd41", "hklm\software\microsoft\windows nt\CurrentVersion\Currentbuild") 'Type 1 'wscript.echo RegGetValue ("pga10664", "hklm\software\microsoft\windows nt\CurrentVersion\digitalproductid") 'Type 3 'wscript.echo RegGetValue ("pga10664", "hklm\software\microsoft\windows nt\CurrentVersion\InstallDate") 'Type 4 'wscript.echo RegWriteValue ("usgctd41", "hklm\software\microsoft\windows nt\CurrentVersion\testvalue=mytest",sRegValueType ) 'Type 4 Function RegGetValue(sServer,sPath) 'Get Registry Root sRoot=ucase(left(sPath,instr(sPath,"\")-1)) 'Set hRoot and remove registry root from path select case sRoot case "HKEY_CLASSES_ROOT","HKCR" hRoot=&H80000000 sPath=right(sPath,len(sPath)-instr(sPath,"\")) case "HKEY_CURRENT_USER","HKCU" hRoot=&H80000001 sPath=right(sPath,len(sPath)-instr(sPath,"\")) case "HKEY_LOCAL_MACHINE","HKLM" hRoot=&H80000002 sPath=right(sPath,len(sPath)-instr(sPath,"\")) case "HKEY_USERS","HKU" hRoot=&H80000003 sPath=right(sPath,len(sPath)-instr(sPath,"\")) case "HKEY_CURRENT_CONFIG","HKCC" hRoot=&H80000005 sPath=right(sPath,len(sPath)-instr(sPath,"\")) case "HKEY_DYN_DATA","HKDD" hRoot=&H80000006 sPath=right(sPath,len(sPath)-instr(sPath,"\")) case else hRoot=&H80000002 end select 'Split path into key and value parts sTargetValue=right(sPath,len(sPath)-instrrev(sPath,"\")) sKeyPath=left(sPath,instrrev(sPath,"\")-1) 'Get Registry object on error resume next Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & sServer & "\root\default:StdRegProv") if err.number <> 0 then RegGetValue="Not found. Error: "&Cstr(Hex(err.number))&" "&err.description err.clear exit function end if on error goto 0 'Enumvalues to get value data type iReturn=oReg.EnumValues (hRoot, sKeyPath, aValues, iTypes) if iReturn <> 0 then if iReturn="2" then RegGetValue="Not Found. WMI Error: "&iReturn&". Key "&sKeyPath&" not found." else RegGetValue="Not Found. WMI Error: "&iReturn&". Check http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/wbemerrorenum.asp for reason." end if exit function end if if TypeName(aValues)="Null" then RegGetValue="Not found. Error: Couldn't find the "&sTargetValue&" value (or any values) under "&sKeypath&"." exit function end if 'Get Data type for ix=0 to ubound(aValues) 'wscript.echo aValues(ix),iTypes(ix) if lcase(aValues(ix))=lcase(sTargetValue) then iType=iTypes(ix) bFound=True end if next 'Return if value not found. if not bFound then RegGetValue="Couldn't find value "&sTargetValue&" under key "&sKeyPath&"." exit function end if 'Types: REG_SZ (1), REG_EXPAND_SZ (2), REG_BINARY (3), REG_DWORD (4), REG_MULTI_SZ (7) select case iType case 1 iReturn=oReg.GetStringValue(hRoot,sKeyPath,sTargetValue,sValue) case 2 iReturn=oReg.GetExpandedStringValue(hRoot,sKeyPath,sTargetValue,sValue) case 3 iReturn=oReg.GetBinaryValue(hRoot,sKeyPath,sTargetValue,bValues) if iReturn = 0 then for each bValue in bValues sDigit=cstr(hex(bValue)) if len(sDigit)<2 then sDigit="0"&sDigit if len(sValue) = 0 then sValue=sDigit else sValue=sValue&" "&sDigit end if next end if case 4 iReturn=oReg.GetDWORDValue(hRoot,sKeyPath,sTargetValue,sValue) case 7 iReturn=oReg.GetMultiStringValue(hRoot,sKeyPath,sTargetValue,aValues) if iReturn = 0 then for each sString in aValues if len(sValue)=0 then sValue=sString else sValue=svalue&","&sString end if next end if case else sValue="Not Found. Error: Value is an unrecognized data type: "&iType&"." end select if len(sValue)=0 and iReturn <> 0 then if iReturn="2" then RegGetValue="Not Found. WMI Error: "&iReturn&". Key "&sKeyPath&" not found." else RegGetValue="Not Found. WMI Error: "&iReturn&". Check http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/wbemerrorenum.asp for reason." end if exit function end if RegGetValue=sValue End Function Function RegWriteValue(sServer,sPath, sType) set dTypes=createobject("scripting.dictionary") dTypes.comparemode=1 dTypes.add "REG_SZ",1 dTypes.add "REG_EXPAND_SZ",2 dTypes.add "REG_BINARY",3 dTypes.add "REG_DWORD",4 dTypes.add "REG_MULTI_SZ",7 'Get Value if instr(sPath,"=") then sValue=split(sPath,"=")(1) sPath=split(sPath,"=")(0) else sValue="" end if 'Get Registry Root sRoot=ucase(left(sPath,instr(sPath,"\")-1)) 'Set hRoot and remove registry root from path select case sRoot case "HKEY_CLASSES_ROOT","HKCR" hRoot=&H80000000 sPath=right(sPath,len(sPath)-instr(sPath,"\")) case "HKEY_CURRENT_USER","HKCU" hRoot=&H80000001 sPath=right(sPath,len(sPath)-instr(sPath,"\")) case "HKEY_LOCAL_MACHINE","HKLM" hRoot=&H80000002 sPath=right(sPath,len(sPath)-instr(sPath,"\")) case "HKEY_USERS","HKU" hRoot=&H80000003 sPath=right(sPath,len(sPath)-instr(sPath,"\")) case "HKEY_CURRENT_CONFIG","HKCC" hRoot=&H80000005 sPath=right(sPath,len(sPath)-instr(sPath,"\")) case "HKEY_DYN_DATA","HKDD" hRoot=&H80000006 sPath=right(sPath,len(sPath)-instr(sPath,"\")) case else hRoot=&H80000002 end select 'Split path into key and value parts sTargetValue=right(sPath,len(sPath)-instrrev(sPath,"\")) sKeyPath=left(sPath,instrrev(sPath,"\")-1) 'Get Registry object on error resume next Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & sServer & "\root\default:StdRegProv") if err.number <> 0 then RegWriteValue="Not found. Error: "&Cstr(Hex(err.number))&" "&err.description err.clear exit function end if on error goto 0 'Enumvalues to get value data type iReturn=oReg.EnumValues (hRoot, sKeyPath, aValues, iTypes) if iReturn <> 0 then if iReturn="2" then RegWriteValue="Not Found. WMI Error: "&iReturn&". Key "&sKeyPath&" not found." else RegWriteValue="Not Found. WMI Error: "&iReturn&". Check http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/wbemerrorenum.asp for reason." end if exit function end if 'If no existing value, create one bFound=False 'if TypeName(aValues)="Null" then ' RegWriteValue="Not found. Error: Couldn't find the "&sTargetValue&" value (or any values) under "&sKeypath&"." ' exit function 'end if 'Get Data type for ix=0 to ubound(aValues) 'wscript.echo aValues(ix),iTypes(ix) if lcase(aValues(ix))=lcase(sTargetValue) then iType=iTypes(ix) bFound=True end if next sOp="updated" 'Create value if not found. if not bFound then if dTypes.exists(sType) then iType=dTypes(sType) else iType=1 end if sOp="added" end if 'Types: REG_SZ (1), REG_EXPAND_SZ (2), REG_BINARY (3), REG_DWORD (4), REG_MULTI_SZ (7) select case iType case 1 iReturn=oReg.SetStringValue(hRoot,sKeyPath,sTargetValue,sValue) case 2 iReturn=oReg.SetExpandedStringValue(hRoot,sKeyPath,sTargetValue,sValue) case 3 bValues=split(sValue,",") iReturn=oReg.SetBinaryValue(hRoot,sKeyPath,sTargetValue,bValues) if iReturn = 0 then for each bValue in bValues sDigit=cstr(hex(bValue)) if len(sDigit)<2 then sDigit="0"&sDigit if len(sValue) = 0 then sValue=sDigit else sValue=sValue&" "&sDigit end if next end if case 4 iReturn=oReg.SetDWORDValue(hRoot,sKeyPath,sTargetValue,sValue) case 7 aValues=split(sValue,",") iReturn=oReg.SetMultiStringValue(hRoot,sKeyPath,sTargetValue,aValues) if iReturn = 0 then for each sString in aValues if len(sValue)=0 then sValue=sString else sValue=svalue&","&sString end if next end if case else sValue="Not Found. Error: Value is an unrecognized data type: "&iType&"." end select if len(sValue)=0 and iReturn <> 0 then if iReturn="2" then RegWriteValue="Not Found. WMI Error: "&iReturn&". Key "&sKeyPath&" not found." else RegWriteValue="Not Found. WMI Error: "&iReturn&". Check http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/wbemerrorenum.asp for reason." end if exit function end if RegWriteValue="Successfully "&sOp&" "&sPath&"="&svalue&"." End Function Function ExistKey(sServer,sKeyPath) const HKEY_LOCAL_MACHINE = &H80000002 Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & sServer & "\root\default:StdRegProv") oReg.EnumKey HKEY_LOCAL_MACHINE, sKeyPath, aSubKeys For Each sSubkey In aSubKeys if lcase(sSubKey)=lcae(sTargetKey) then ExistKey=-1 exit function end if Next ExistKey=0 End Function Function RegEnum(sServer,sPath) 'Get Registry Root sRoot=ucase(left(sPath,instr(sPath,"\")-1)) 'Set hRoot and remove registry root from path select case sRoot case "HKEY_CLASSES_ROOT","HKCR" hRoot=&H80000000 sPath=right(sPath,len(sPath)-instr(sPath,"\")) case "HKEY_CURRENT_USER","HKCU" hRoot=&H80000001 sPath=right(sPath,len(sPath)-instr(sPath,"\")) case "HKEY_LOCAL_MACHINE","HKLM" hRoot=&H80000002 sPath=right(sPath,len(sPath)-instr(sPath,"\")) case "HKEY_USERS","HKU" hRoot=&H80000003 sPath=right(sPath,len(sPath)-instr(sPath,"\")) case "HKEY_CURRENT_CONFIG","HKCC" hRoot=&H80000005 sPath=right(sPath,len(sPath)-instr(sPath,"\")) case "HKEY_DYN_DATA","HKDD" hRoot=&H80000006 sPath=right(sPath,len(sPath)-instr(sPath,"\")) case else hRoot=&H80000002 end select sKeyPath=sPath 'Get Registry object on error resume next Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & sServer & "\root\default:StdRegProv") if err.number <> 0 then RegEnum=Array("Not found. Error getting to WMI StdRegProv: "&Cstr(Hex(err.number))&" "&err.description) err.clear exit function end if on error goto 0 'Enumvalues iReturn=oReg.EnumValues(hRoot, sKeyPath, aValues, iTypes) if iReturn <> 0 then if iReturn="2" then RegEnum=Array("Not Found. WMI Error: "&iReturn&". Key "&sKeyPath&" not found.") else RegEnum=Array("Not Found. WMI Error: "&iReturn&". Check http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/wbemerrorenum.asp for reason.") end if exit function end if iReturn=oReg.EnumKey(hRoot, sKeyPath, aKeys) if iReturn <> 0 then if iReturn="2" then RegEnum=Array("Not Found. WMI Error: "&iReturn&". Key "&sKeyPath&" not found.") else RegEnum=Array("Not Found. WMI Error: "&iReturn&". Check http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/wbemerrorenum.asp for reason.") end if exit function end if 'wscript.echo "Keys: "&TypeName(akeys),ubound(aKeys) 'wscript.echo "Values: "&TypeName(aValues),ubound(aValues) if IsNull(aKeys) and IsNull(aValues) then RegEnum=Array("No keys or values found under "&sKeypath) elseif IsNull(aKeys) then for ix=0 to ubound(aValues) aValues(ix)="Value - "&aValues(ix) next RegEnum=aValues elseif IsNull(aValues) then for ix=0 to ubound(aKeys) aKeys(ix)="Key - "&aKeys(ix) next RegEnum=aKeys else iSum=ubound(aKeys)+ubound(aValues)+1 wscript.echo iSum ReDim aTmp(iSum) for ix=0 to ubound(aKeys) aTmp(ix)="Key - "&aKeys(ix) next for ix=0 to ubound(aValues) aTmp(ix+ubound(aKeys)+1)="Value - "&aValues(ix) next RegEnum=aTmp end if End Function Function RegEnumKeys(sServer,sPath) 'Get Registry Root sRoot=ucase(left(sPath,instr(sPath,"\")-1)) 'Set hRoot and remove registry root from path select case sRoot case "HKEY_CLASSES_ROOT","HKCR" hRoot=&H80000000 sPath=right(sPath,len(sPath)-instr(sPath,"\")) case "HKEY_CURRENT_USER","HKCU" hRoot=&H80000001 sPath=right(sPath,len(sPath)-instr(sPath,"\")) case "HKEY_LOCAL_MACHINE","HKLM" hRoot=&H80000002 sPath=right(sPath,len(sPath)-instr(sPath,"\")) case "HKEY_USERS","HKU" hRoot=&H80000003 sPath=right(sPath,len(sPath)-instr(sPath,"\")) case "HKEY_CURRENT_CONFIG","HKCC" hRoot=&H80000005 sPath=right(sPath,len(sPath)-instr(sPath,"\")) case "HKEY_DYN_DATA","HKDD" hRoot=&H80000006 sPath=right(sPath,len(sPath)-instr(sPath,"\")) case else hRoot=&H80000002 end select sKeyPath=sPath 'Get Registry object on error resume next Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & sServer & "\root\default:StdRegProv") if err.number <> 0 then RegEnumKeys=Array("Not found. Error getting to WMI StdRegProv: "&Cstr(Hex(err.number))&" "&err.description) err.clear exit function end if on error goto 0 iReturn=oReg.EnumKey(hRoot, sKeyPath, aKeys) if iReturn <> 0 then if iReturn="2" then RegEnumKeys=Array("Not Found. WMI Error: "&iReturn&". Key "&sKeyPath&" not found.") else RegEnumKeys=Array("Not Found. WMI Error: "&iReturn&". Check http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/wbemerrorenum.asp for reason.") end if exit function end if if IsNull(aKeys) then RegEnumKeys=Array("No keys or values found under "&sKeypath) else RegEnumKeys=aKeys end if End Function Function RegEnumValues(sServer,sPath) 'Get Registry Root sRoot=ucase(left(sPath,instr(sPath,"\")-1)) 'Set hRoot and remove registry root from path select case sRoot case "HKEY_CLASSES_ROOT","HKCR" hRoot=&H80000000 sPath=right(sPath,len(sPath)-instr(sPath,"\")) case "HKEY_CURRENT_USER","HKCU" hRoot=&H80000001 sPath=right(sPath,len(sPath)-instr(sPath,"\")) case "HKEY_LOCAL_MACHINE","HKLM" hRoot=&H80000002 sPath=right(sPath,len(sPath)-instr(sPath,"\")) case "HKEY_USERS","HKU" hRoot=&H80000003 sPath=right(sPath,len(sPath)-instr(sPath,"\")) case "HKEY_CURRENT_CONFIG","HKCC" hRoot=&H80000005 sPath=right(sPath,len(sPath)-instr(sPath,"\")) case "HKEY_DYN_DATA","HKDD" hRoot=&H80000006 sPath=right(sPath,len(sPath)-instr(sPath,"\")) case else hRoot=&H80000002 end select sKeyPath=sPath 'Get Registry object on error resume next Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & sServer & "\root\default:StdRegProv") if err.number <> 0 then RegEnumValues=Array("Not found. Error getting to WMI StdRegProv: "&Cstr(Hex(err.number))&" "&err.description) err.clear exit function end if on error goto 0 'Enumvalues iReturn=oReg.EnumValues(hRoot, sKeyPath, aValues, iTypes) if iReturn <> 0 then if iReturn="2" then RegEnumValues=Array("Not Found. WMI Error: "&iReturn&". Key "&sKeyPath&" not found.") else RegEnumValues=Array("Not Found. WMI Error: "&iReturn&". Check http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/wbemerrorenum.asp for reason.") end if exit function end if iReturn=oReg.EnumKey(hRoot, sKeyPath, aKeys) if iReturn <> 0 then if iReturn="2" then RegEnumValues=Array("Not Found. WMI Error: "&iReturn&". Key "&sKeyPath&" not found.") else RegEnumValues=Array("Not Found. WMI Error: "&iReturn&". Check http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/wbemerrorenum.asp for reason.") end if exit function end if 'wscript.echo "Keys: "&TypeName(akeys),ubound(aKeys) 'wscript.echo "Values: "&TypeName(aValues),ubound(aValues) if IsNull(aValues) then RegEnumValues=Array("No keys or values found under "&sKeypath) else RegEnumValues=aValues end if End Function Sub Usage wscript.echo wscript.echo "Usage:" wscript.echo wscript.echo "cscript reg.vbs " wscript.echo wscript.echo "for example:" wscript.echo wscript.echo "cscript reg.vbs enum ""\\usgctd92\hklm\software\microsoft\windows nt\currentversion""" wscript.echo "cscript reg.vbs read ""\\usgctd92\hklm\software\microsoft\windows nt\currentversion\csdversion""" wscript.echo "cscript reg.vbs write ""\\ustems152\hklm\software\microsoft\mssqlserver\mssqlserver\supersocketnetlib\protocollist=np,tcp"" reg_multi_sz" wscript.echo wscript.echo "If no server is specified, the local registry will be read." wscript.echo wscript.echo "Valid types are REG_SZ, REG_EXPAND_SZ, REG_BINARY, REG_DWORD and REG_MULTI_SZ." wscript.echo "If no type is specified REG_SZ will be used." wscript.echo "For multi-value writes, please delimit values with "",""" wscript.echo wscript.quit End Sub