Skip to content

Instantly share code, notes, and snippets.

@ebibibi
Created May 5, 2012 11:33
Show Gist options
  • Select an option

  • Save ebibibi/2601725 to your computer and use it in GitHub Desktop.

Select an option

Save ebibibi/2601725 to your computer and use it in GitHub Desktop.
[vbscript]SetRegistry
SetRegistry "HKCU\test", "test", "REG_SZ"
'-------------------------------------------------------------------------------------------
' レジストリの設定
'-------------------------------------------------------------------------------------------
' レジストリの現在の設定をログファイルに出力後、実際に書き換え、成功したかどうかを確認する。
' UACが有効な場合には管理者権限があっても失敗するので事前に昇格させておくこと。
' http://ebi.dyndns.biz/diary/20080813.html
'-------------------------------------------------------------------------------------------
' RegistryPath - レジストリのパス
' NewConfiguration - 設定する値
' RegistryType - レジストリのタイプ
' 文字列 REG_SZ
' 文字列 REG_EXPAND_SZ
' 整数 REG_DWORD
' 文字列 REG_BINARY
'-------------------------------------------------------------------------------------------
Public Function SetRegistry(RegistryPath, NewConfiguration, RegistryType)
On Error Resume Next
Dim WshShell, CurrentConfiguration
'レジストリの確認を実施、すでに意図した設定になっている場合には処理しない
Set WshShell = WScript.CreateObject("WScript.Shell")
CurrentConfiguration = WshShell.RegRead(RegistryPath)
If IsArray(CurrentConfiguration) <> True Then
If CurrentConfiguration = NewConfiguration Then
WScript.Echo(RegistryPath & "の設定はすでに意図したものが設定されています: " & CurrentConfiguration)
SetRegistry = 0
Exit Function
End If
End If
'既存の設定を出力後、書き込みを行う
WScript.Echo("既存の値 : " & RegistryPath & " = " & CurrentConfiguration)
WScript.Echo("書き込む値 : " & RegistryPath & " = " & NewConfiguration)
WshShell.RegWrite RegistryPath, NewConfiguration, RegistryType
'確認
CurrentConfiguration = WshShell.RegRead(RegistryPath)
WScript.Echo("更新後の値 : " & RegistryPath & " = " & CurrentConfiguration)
If CurrentConfiguration = NewConfiguration Then
WScript.Echo("正常に値が設定されました。")
SetRegistry = 0
Else
WScript.Echo("値の更新に失敗しました。")
IF Err.Number <> 0 Then
WScript.Echo "エラー : " & Err.Number & vbCrLf & Err.Description
End If
SetRegistry = 1
End If
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment