EDC.Base.AddRegistryValue.Script.WriteAction (WriteActionModuleType)

Element properties:

TypeWriteActionModuleType
IsolationAny
AccessibilityPublic
RunAsDefault
InputTypeSystem.BaseData

Member Modules:

ID Module Type TypeId RunAs 
Script WriteAction Microsoft.Windows.ScriptWriteAction Default

Source Code:

<WriteActionModuleType ID="EDC.Base.AddRegistryValue.Script.WriteAction" Accessibility="Public" Batching="false">
<Configuration>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="RegistryPath" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="ValueType" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="Value" type="xsd:string"/>
</Configuration>
<ModuleImplementation Isolation="Any">
<Composite>
<MemberModules>
<WriteAction ID="Script" TypeID="Windows!Microsoft.Windows.ScriptWriteAction">
<ScriptName>AddRegistryValue.vbs</ScriptName>
<Arguments>"$Config/RegistryPath$" "$Config/ValueType$" "$Config/Value$"</Arguments>
<ScriptBody><Script>
Option Explicit
'***********************************************************************************************************
' Disclaimer
'
' This sample script is not supported under any Microsoft standard support program or service. This sample
' script is provided AS IS without warranty of any kind. Microsoft further disclaims all implied warranties
' including, without limitation, any implied warranties of merchantability or of fitness for a particular
' purpose. The entire risk arising out of the use or performance of this sample script and documentation
' remains with you. In no event shall Microsoft, its authors, or anyone else involved in the creation,
' production, or delivery of this script be liable for any damages whatsoever (including, without limitation,
' damages for loss of business profits, business interruption, loss of business information, or other
' pecuniary loss) arising out of the use of or inability to use this sample script or documentation, even
' if Microsoft has been advised of the possibility of such damages.
'
'***********************************************************************************************************
' AddRegistryValue 1.0 v20090313
'
' Creates a new registry value. This script adds a new value to a given key. It uses a number as the value name,
' starting with 0 and using the first free number.
' This functionality is used for example for the EDC File Share Probe sample, where the number will be the perspective ID.
'
' Arguments: &lt;registrypath&gt; &lt;valuetype&gt; &lt;value&gt;
'
' RegistryPath: path of the key to which the new unique registry value will be added. The key must end with a backslash ("\").
' ValueType: Type of the value to be written. Must be REG_SZ, REG_EXPAND_SZ, REG_DWORD or REG_BINARY. A REG_BINARY
' is limited to at most one REG_DWORD.
' Value: Value to set. Either a string or an integer.
'
' Version for Operations Manager 2007
'
' JM
'***********************************************************************************************************
'Script specific constants
'Const SCRIPT_NAME = "AddRegistryValue"
Const SCRIPT_VERSION = "1.0"

Const MAX_ID = 100000 'Just to prevent an endless loop, not meaning it is a good idea to add 100000 registry values to a key

Call Main

'********************************************************************************
' PROCEDURE: Main
' DESCRIPTION: Read the arguments and restart the given Win32 service including
' all services depending on it if they are running.
' PARAMETERS: void
'******************************************************************************
Private Sub Main()

Dim strRegistryPath 'As String
Dim strRegistryType 'As String
Dim varValue 'As Variant
Dim objWSHShell 'As WScript.Shell
Dim varTmp 'As Variant
Dim i 'As Long

Call SetLocale("en-us")

If Not GetScriptParameters(strRegistryPath, strRegistryType, varValue) Then
Call WScript.Echo("Error! An argument is missing." &amp; vbCrLf &amp; "Syntax: cscript AddRegistryValue.vbs &lt;registrykey&gt; &lt;valuetype&gt; &lt;value&gt;")
Exit Sub
End If

Set objWSHShell = CreateObject("WScript.Shell")

On Error Resume Next

Call WScript.Echo("Searching for an unused value name to store the value '" &amp; CStr(varValue) &amp; "' (" &amp; strRegistryType &amp; ") within the registry key '" &amp; strRegistryPath &amp; "'..." &amp; vbCrLf)

For i = 0 To MAX_ID
varTmp = objWSHShell.RegRead(strRegistryPath &amp; CStr(i))
If Err.Number = 0 Then
'value name already in use. Go on.
'Call WScript.Echo("The registry path '" &amp; strRegistryPath &amp; CStr(i) &amp; "' already exists with a value of '" &amp; varTmp &amp; "'.")
Else
'value name not in use. Use it to store the value.
Err.Clear
Call WScript.Echo("The registry path '" &amp; strRegistryPath &amp; CStr(i) &amp; "' does not exist already. Using it to store '" &amp; CStr(varValue) &amp; "' (" &amp; strRegistryType &amp; ")..." &amp; vbCrLf)
Call objWSHShell.RegWrite(strRegistryPath &amp; CStr(i), varValue, strRegistryType)
Exit For
End If
Next 'i


If Err.Number = 0 Then
Call WScript.Echo("Successfully written '" &amp; strRegistryPath &amp; "' = '" &amp; varValue &amp; "' (" &amp; strRegistryType &amp; ").")
Else
Call WScript.Echo("Error! Failed to write '" &amp; strRegistryPath &amp; "' = '" &amp; varValue &amp; "' (" &amp; strRegistryType &amp; ")." &amp; vbCrLf &amp; _
Err.Description)
End If

End Sub

'******************************************************************************
' FUNCTION: GetScriptParameters
' DESCRIPTION: Reads the script's parameters and sets the variables accordingly.
' PARAMETERS: OUT String strServiceName: the name of the service to be restarted
' OUT Long lngTimeOut: timeout for starting or stopping a service in seconds
' RETURNS: Boolean: True if successful
'******************************************************************************
Private Function GetScriptParameters(ByRef strRegistryPath, ByRef strRegistryType, ByRef varValue) 'As Boolean

Dim objArguments

On Error Resume Next

Set objArguments = WScript.Arguments
If objArguments.Count &lt; 3 Then Exit Function

'Get parameters and set global variables
strRegistryPath = objArguments(0)
'validate must end with a backslash
If Not Right(strRegistryPath, 1) = "\" Then strRegistryPath = strRegistryPath &amp; "\"
strRegistryType = objArguments(1)
varValue = objArguments(2)

If Len(strRegistryPath) &gt; 0 And Len(strRegistryType) &gt; 0 And Len(CStr(varValue)) &gt; 0 Then GetScriptParameters = True

End Function
</Script></ScriptBody>
<TimeoutSeconds>30</TimeoutSeconds>
</WriteAction>
</MemberModules>
<Composition>
<Node ID="Script"/>
</Composition>
</Composite>
</ModuleImplementation>
<InputType>System!System.BaseData</InputType>
</WriteActionModuleType>