今まで、コマンドプロンプトやエクスプローラから実行するのが一般的でしたが、Windows10 の PowerShell ウインドウからも実行できます( コマンドプロンプトと一応は同じです ) open_reg.vbs
ソースツールバーの右端のアイコンよりダウンロードできます
Set obj = Wscript.CreateObject("Shell.Application") Dim strParam if Wscript.Arguments.Count = 0 then ' 引数が無い場合は、まずクリップボードを確認 ' クリップボード用 ' ※ HTA 等では直接 window.clipboardData より実行 ' ※ するように書き換える必要があります Set objIE = CreateObject("InternetExplorer.Application") objIE.Navigate("about:blank") Do While objIE.Busy ' 100 ミリ秒 Wscript.Sleep 100 Loop strParam = objIE.document.parentWindow.clipboardData.GetData( "Text" ) & "" objIE.Quit strParam = Trim( strParam ) ' 無ければ入力 if strParam = "" then strParam = InputBox("開く対象となるレジストリーのキーを入力して下さい","指定したキーでレジストリエディタを開く","HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft") end if ' 管理者として実行を強制する obj.ShellExecute "wscript.exe", WScript.ScriptFullName & " runas """ & strParam & """", "", "runas", 1 Wscript.Quit else strParam = WScript.Arguments(0) if strParam <> "runas" then ' 管理者として実行を強制する Set obj = Wscript.CreateObject("Shell.Application") if Wscript.Arguments.Count = 1 then obj.ShellExecute "wscript.exe", WScript.ScriptFullName & " runas """ & strParam & """", "", "runas", 1 Wscript.Quit end if end if end if ' 引数 strParam = WScript.Arguments(1) strParam = Trim( strParam ) ' レジストリ書き込み用 Set WshShell = CreateObject( "WScript.Shell" ) ' WMI用 Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2") ' レジストリエディタが最後に開いていたキーの登録を行います strPath = "Software\Microsoft\Windows\CurrentVersion\Applets\Regedit\LastKey" if GetOSVersion() >= 6 then strRegPath = "コンピューター\" & strParam else strRegPath = "マイ コンピュータ\" & strParam end if ' 既に regedit が実行中の場合はいったん終了させます Set colProcessList = objWMIService.ExecQuery _ ("Select * from Win32_Process Where Name = 'regedit.exe'") For Each objProcess in colProcessList ' 最後のウインドウの位置とサイズを保存する為の終わらせ方 WshShell.AppActivate("レジストリ エディタ") Wscript.Sleep(500) WshShell.SendKeys ("%{F4}") Wscript.Sleep(500) ' 上記終わらせ方が失敗した時の強制終了 on error resume next objProcess.Terminate() on error goto 0 Next WshShell.RegWrite "HKCU\" & strPath, strRegPath, "REG_SZ" ' レジストリエディタを起動します Call WshShell.Run( "regedit.exe" ) ' レジストリエディタが終わるまで待つ場合は以下のようにします ' Call WshShell.Run( "regedit.exe", , True ) REM ********************************************************** REM OS バージョンの取得 REM ********************************************************** Function GetOSVersion() Dim colTarget,str,aData,I,nTarget Set colTarget = objWMIService.ExecQuery( _ "select Version from Win32_OperatingSystem" _ ) For Each objRow in colTarget str = objRow.Version Next aData = Split( str, "." ) For I = 0 to Ubound( aData ) if I > 1 then Exit For end if if I > 0 then nTarget = nTarget & "." end if nTarget = nTarget & aData(I) Next GetOSVersion = CDbl( nTarget ) End Function
ファイル名を指定、またはコマンドプロンプトから実行では、以下のようにします。wscript.exe open_reg.vbs HKEY_CURRENT_USER\Consoleクリップボードにキーがある場合は open_reg.vbs をそのままエクスプローラからダブルクリックします。 クリツプボードにデータが空の場合は、入力ダイアログを表示します(上記コードではデフォルトとして HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft を表示しています) 権限やセキュリティの関係でいろいろダイアログが表示されると事がほとんどです。 ▼ IE11 のクリップボード操作の制限 (IE の設定で、常に使用可能にする事もできます) また、これ以外にも、regedit.exe を実行する際に、管理者実行の確認がありますが、サンプルとして常に管理者権限で実行できるようにしています。。 管理者権限を取得しない単純コード
ソースツールバーの右端のアイコンよりダウンロードできます
Dim strParam if Wscript.Arguments.Count = 0 then strParam = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName" else strParam = WScript.Arguments(0) end if ' レジストリ書き込み用 Set WshShell = CreateObject( "WScript.Shell" ) ' WMI用 Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2") ' レジストリエディタが最後に開いていたキーの登録を行います strPath = "Software\Microsoft\Windows\CurrentVersion\Applets\Regedit\LastKey" if GetOSVersion() >= 6 then strRegPath = "コンピューター\" & strParam else strRegPath = "マイ コンピュータ\" & strParam end if ' 既に regedit が実行中の場合はいったん終了させます Set colProcessList = objWMIService.ExecQuery _ ("Select * from Win32_Process Where Name = 'regedit.exe'") For Each objProcess in colProcessList ' 最後のウインドウの位置とサイズを保存する為の終わらせ方 WshShell.AppActivate("レジストリ エディタ") Wscript.Sleep(500) WshShell.SendKeys ("%{F4}") Wscript.Sleep(500) ' 上記終わらせ方が失敗した時の強制終了 on error resume next objProcess.Terminate() on error goto 0 Next WshShell.RegWrite "HKCU\" & strPath, strRegPath, "REG_SZ" ' レジストリエディタを起動します Call WshShell.Run( "regedit.exe" ) ' レジストリエディタが終わるまで待つ場合は以下のようにします ' Call WshShell.Run( "regedit.exe", , True ) REM ********************************************************** REM OS バージョンの取得 REM ********************************************************** Function GetOSVersion() Dim colTarget,str,aData,I,nTarget Set colTarget = objWMIService.ExecQuery( _ "select Version from Win32_OperatingSystem" _ ) For Each objRow in colTarget str = objRow.Version Next aData = Split( str, "." ) For I = 0 to Ubound( aData ) if I > 1 then Exit For end if if I > 0 then nTarget = nTarget & "." end if nTarget = nTarget & aData(I) Next GetOSVersion = CDbl( nTarget ) End Function
|
【VBScriptの最新記事】
- 指定した位置でレジストリエディタを開く為の VBScript をダウンロードします
- VBScript(WSH) と PHP(7.3) で氏名をランダムに作成
- VBScript : XCOPYで新しいファイルのみバックアップする為のスクリプトを作成するスクリプト
- VBScript + PowerShell : PowerShell がファイルを開くダイアログで取得したパスをテキストファイル経由で VBScript に戻して使用する
- VBScript : XMLファイルのテキストノードの値の更新
- HTA : 指定したキーでレジストリエディタを開く
- VBScript : WEB上のHTMLを使用して、InternetExplorer.Application でパスワード入力を実装する
- VBS : 正規表現で、URL リストの中のドメイン部分のみを取り出す
- 文字列を指定してその名前の変数でオブジェクトを作成する : WEB に VBScript ライブラリ
- 実行中のスクリプトのタイプを知る : WEB に VBScript ライブラリ
- VBS : FileZilla用誰にでも使える拡張子ランチャー