SQLの窓

2018年01月28日


WSH(VBScript) : 指定したキーでレジストリエディタを開く

今まで、コマンドプロンプトやエクスプローラから実行するのが一般的でしたが、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





posted by lightbox at 2018-01-28 21:58 | VBScript | このブログの読者になる | 更新情報をチェックする

2014年03月11日


VBScript : WEB上のHTMLを使用して、InternetExplorer.Application でパスワード入力を実装する

昔はセキュリティがゆるかったので、ローカルの C:\ のパスで使えたのですが、今は http:// でないと使え無いので画面を http://winofsql.jp/password.htm として置いています。



入力後、きちんと IE は閉じます。

また、IE11 環境で実行しているので、VBScript を利用可能にする為、以下の記述は必要です。
<meta http-equiv="X-UA-Compatible" content="IE=8">
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=8">
<SCRIPT LANGUAGE="VBScript">
 
Sub RunScript 
	document.getElementById("OKClicked").Value = "OK"
End Sub 

Sub CancelScript 
	document.getElementById("OKClicked").Value = "Cancelled"
End Sub 

</SCRIPT> 

<BODY> 
<pre>
Password:
<input type="password" id="UserPassword" size="40">
<input type="hidden" id="OKClicked" size="20"> 
<input class="button" type="button" value=" OK " onClick="RunScript"
> <input class="button" type="button" value="Cancel" onClick="CancelScript">
</pre>
</body>
</html>
※ ローカルでは無いので必ず、Busy 対処は必要です。
※ Sleep を使用して、入力されたかどうかを監視して待機しています。
' 管理者として実行を強制する
Set obj = Wscript.CreateObject("Shell.Application")
if Wscript.Arguments.Count = 0 then
	obj.ShellExecute "wscript.exe", WScript.ScriptFullName & " runas", "", "runas", 1
	Wscript.Quit
end if

' オブジェクト作成
Set objExplorer = WScript.CreateObject("InternetExplorer.Application") 
 
' オブジェクト設定
objExplorer.Navigate "http://winofsql.jp/password.htm"	
objExplorer.ToolBar = 0 
objExplorer.StatusBar = 0 
objExplorer.Width = 400 
objExplorer.Height = 350  
objExplorer.Left = 300 
objExplorer.Top = 200 
objExplorer.Visible = True

' ページのロードを待つ
Do While objExplorer.Busy
	' 100 ミリ秒
	Wscript.Sleep 100
Loop
 
' 入力状態を監視
Do While (objExplorer.document.getElementById("OKClicked").Value = "") 
	Wscript.Sleep 250
Loop  

' パスワード取得
strPassword = objExplorer.document.getElementById("UserPassword").Value
' ボタンの判定用
strButton = objExplorer.document.getElementById("OKClicked").Value

' オブジェクト終了
objExplorer.Quit
' 少し待機
Wscript.Sleep 250 

' 入力判定
If strButton = "Cancelled" Then 
	Wscript.Quit 
Else 
	Wscript.Echo strPassword 
End If

Wscript.Echo strPassword


関連する記事

IE11 で VBScript を使う場合の注意事項 ( 古い社内アプリ移行時必見 )



posted by lightbox at 2014-03-11 20:27 | VBScript | このブログの読者になる | 更新情報をチェックする

2011年12月21日


VBS : 正規表現で、URL リストの中のドメイン部分のみを取り出す

処理そのものは一般的なものですが、VBScript の正規表現の記述は、
JavaScript のそれより少し複雑です。

まず、最初に New RegExp で正規表現処理用のオブジェクトを作成し
て、どのような検索をするかは、プロパティに設定します。

さらに、() を使った中の文字列を取得するには、Pattern に指定した
正規表現全体の文字列が入っている Match に対して、プロパティとし
て SubMatches コレクションが存在するので、その中の先頭として取り
出しています

今回、() の指定は一つだったので、SubMatches(0) しか存在しませんが、複数
の場合は、インデックスに 1 以上を使って参照します

※ .jp のみの場合は regEx.Pattern = "http://(.+?\.jp)/"

Dim str : str = "http://winofsql.jp/php/cnvtext/frame.htm" & vbCrLf & _
"http://js4web.seesaa.net/article/122772668.html" & vbCrLf & _
"http://hp.vector.co.jp/authors/VA003334/mslink.htm"

MsgBox(str)

Dim regEx : Set regEx = New RegExp

' 検索パターンを文字列で設定
regEx.Pattern = "http://(.+?)/"
' 大文字小文字を区別しない
' ( この場合はどちらでも良い )
regEx.IgnoreCase = True
' 文字列全体を検索
' False だと一件しか検索しない
regEx.Global = True   

' 検索の実行
Dim Matches : Set Matches = regEx.Execute(str)

Dim Match
For Each Match in Matches
	' 検索結果の中の () 内の文字列を取得
	MsgBox(Match.SubMatches(0))
Next



posted by lightbox at 2011-12-21 13:17 | VBScript | このブログの読者になる | 更新情報をチェックする

2011年08月14日


文字列を指定してその名前の変数でオブジェクトを作成する : WEB に VBScript ライブラリ

WEB に VBScript ライブラリ

Dim で定義するのと同等の処理となります。
この処理は、関数内でグローバルスコープの変数を作成する事が可能な事を示しています。

※ 拡張子を .vbs にして実行してみて下さい。
' 関数のソースコードを読みだして、関数として定義
strResult = HTTPGet( "http://toolbox.winofsql.jp/vbs/createobject.php" )
' エラー処理は省略しています
ExecuteGlobal strResult 

' 実行中のスクリプトのタイプ
Call GetObj( "WshShell", "WScript.Shell" )
print WshShell.CurrentDirectory

Function HTTPGet( strUrl )

	Dim http

	Set http = CreateObject( "Msxml2.ServerXMLHTTP" )

	on error resume next
	Call http.Open("GET", strUrl, False )
	if Err.Number <> 0 then
		HTTPGet = Empty
		Exit Function
	end if
	on error goto 0

	Call http.Send()

	HTTPGet = http.responseText

End Function

PHP でホスティングされた関数
( ScriptType 関数が別途必要です ) 
<?
header( "Content-Type: text/plain; Charset=shift_jis" );
header( "pragma: no-cache" );
header( "Expires: Wed, 31 May 2000 14:59:58 GMT" );
header( "Cache-control: no-cache" );
?>
REM **********************************************************
REM 実行中のスクリプト別簡易表示
REM **********************************************************
Sub print( strData )

	Dim strType

	strType = ScriptType( )

	Select Case strType
		Case 1
			Wscript.Echo strData
		Case 2
			alert( strData )
		Case 3
			Response.Write strData
	End Select

End Sub

REM **********************************************************
REM 文字列を指定して、変数にオブシェクトを作成させる
REM **********************************************************
Function GetObj( strTarget, strObjectName )

	Dim ExecuteString

	ExecuteString = "Dim " & strTarget & " : "
	ExecuteString = ExecuteString & "Set " & strTarget & " = "

	Select Case ScriptType
		Case 1
			ExecuteString = ExecuteString & _
			"WScript.CreateObject("
		Case 2
			ExecuteString = ExecuteString & _
			"CreateObject("
		Case 3
			ExecuteString = ExecuteString & _
			"Server.CreateObject("
		Case Else
			ExecuteString = ExecuteString & _
			"CreateObject("
	End Select

	ExecuteString = ExecuteString & """" & strObjectName & """" & ")"

	ExecuteGlobal ExecuteString

End Function

関連する記事

実行中のスクリプトのタイプを知る : WEB に VBScript ライブラリ


posted by lightbox at 2011-08-14 17:36 | VBScript | このブログの読者になる | 更新情報をチェックする

実行中のスクリプトのタイプを知る : WEB に VBScript ライブラリ

WEB に VBScript ライブラリ

特に実用的なコードではありませんし、タイプと言っても WSH か HTA か ASP
なので、使い方が相当違うのでライブラリ側を完全の共用する場合にのみ必要
になって来ます。

Msxml2.ServerXMLHTTP の作成として CreateObject を使っていますが、単純な
スクリプトの場合の場合は必ず最初にこの関数だけはクライアントに必要なので
スクリプトのタイプは無視してどれでも一応実行可能な CreateObject で作成し
ています。
( 本来は、WSH は、WScript.CreateObject で、ASP は、Server.CreateObject )
' 関数のソースコードを読みだして、関数として定義
strResult = HTTPGet( "http://toolbox.winofsql.jp/vbs/createobject.php" )
if not IsEmpty( strResult ) and Left( strResult, 3 ) = "REM" then
	' 関数定義の実行
	ExecuteGlobal strResult
else
	if IsEmpty( strResult ) then
		MsgBox(Err.Description)
	else
		MsgBox(strResult)
	end if
	Wscript.Quit
end if

' 実行中のスクリプトのタイプ
MsgBox( ScriptType() )

Function HTTPGet( strUrl )

	Dim http

	Set http = CreateObject( "Msxml2.ServerXMLHTTP" )

	on error resume next
	Call http.Open("GET", strUrl, False )
	if Err.Number <> 0 then
		HTTPGet = Empty
		Exit Function
	end if
	on error goto 0

	Call http.Send()

	HTTPGet = http.responseText

End Function

以下は、php でホスティングしている VBScript の関数です
<?
header( "Content-Type: text/plain; Charset=shift_jis" );
header( "pragma: no-cache" );
header( "Expires: Wed, 31 May 2000 14:59:58 GMT" );
header( "Cache-control: no-cache" );
?>
REM **********************************************************
REM 実行中のスクリプトのタイプの取得
REM 1:WSH, 2:HTA, 3:ASP, 0:不明
REM **********************************************************
Function ScriptType( )

	Dim nType

	nType = 0

	if IsObject( Wscript ) then
		nType = 1
	else
		if IsObject( window ) then
			nType = 2
		else
			if IsObject( Server ) then
				nType = 3
			end if
		end if
	end if

	ScriptType = nType

End Function

関連する記事

VBScriptの関数定義をWEB上に置いて、Msxml2.ServerXMLHTTP で読みだして PCで使用する



posted by lightbox at 2011-08-14 17:12 | VBScript | このブログの読者になる | 更新情報をチェックする

2010年01月06日


VBS : FileZilla用誰にでも使える拡張子ランチャー

FileZilla Client 用に作りましたが、どこでも使えると思います。ただ、呼び出し方は wscript.exe の正しい位置をパスで指定して "" で囲う事が重要です

▼ 例
"C:\Windows\System32\wscript.exe" "C:\user\runext.vbs"



FileZilla は、設定したエディタで開くと、その開かれたファイルは監視されて更新されるとアップロードするかどうか聞いてきます。つまり、直接サーバーのファイルを更新するような作業ができます。ただ、エディタを一つしか指定できないので、このようなランチャーを登録してやると、テキストも画像も変更した後にすぐサーバへアップロードできます。また、複数開いても全て監視されるので、更新されたものが対象となります。とても便利です。
Set WshShell = WScript.CreateObject("WScript.Shell")

Dim aData,strExt,strExe
strExt = ""
aData = Split( Wscript.Arguments(0), "." )
if Ubound( aData ) > 0 then
	strExt = UCase( aData( Ubound( aData ) ) )
end if

' 拡張子別エディタ
if strExt = "JPG" then
	strExe = "MSPAINT.EXE"
	RunExt( strExe )
	Wscript.Quit
end if
if strExt = "JPEG" then
	strExe = "MSPAINT.EXE"
	RunExt( strExe )
	Wscript.Quit
end if
if strExt = "PNG" then
	strExe = "MSPAINT.EXE"
	RunExt( strExe )
	Wscript.Quit
end if
if strExt = "GIF" then
	strExe = "MSPAINT.EXE"
	RunExt( strExe )
	Wscript.Quit
end if

' デフォルトのエディタ
strExe = "C:\Program Files\tpad093\TeraPad.exe"
RunExt( strExe )

Function RunExt( path )

	WshShell.Run( """" & path & """" & " " _
		& """" & Wscript.Arguments(0) & """" )

End Function

関連する記事

FileZilla FTP クライアントのサーバー情報の引っ越し作業

WEB 上のファイルに使用できる文字と FileZilla





posted by lightbox at 2010-01-06 19:35 | VBScript | このブログの読者になる | 更新情報をチェックする
Seesaa の各ページの表示について
Seesaa の 記事がたまに全く表示されない場合があります。その場合は、設定> 詳細設定> ブログ設定 で 最新の情報に更新の『実行ボタン』で記事やアーカイブが最新にビルドされます。

Seesaa のページで、アーカイブとタグページは要注意です。タグページはコンテンツが全く無い状態になりますし、アーカイブページも歯抜けページはコンテンツが存在しないのにページが表示されてしまいます。

また、カテゴリページもそういう意味では完全ではありません。『カテゴリID-番号』というフォーマットで表示されるページですが、実際存在するより大きな番号でも表示されてしまいます。

※ インデックスページのみ、実際の記事数を超えたページを指定しても最後のページが表示されるようです

対処としては、このようなヘルプ的な情報を固定でページの最後に表示するようにするといいでしょう。具体的には、メインの記事コンテンツの下に『自由形式』を追加し、アーカイブとカテゴリページでのみ表示するように設定し、コンテンツを用意するといいと思います。


※ エキスパートモードで表示しています

アーカイブとカテゴリページはこのように簡単に設定できますが、タグページは HTML 設定を直接変更して、以下の『タグページでのみ表示される内容』の記述方法で設定する必要があります

<% if:page_name eq 'archive' -%>
アーカイブページでのみ表示される内容
<% /if %>

<% if:page_name eq 'category' -%>
カテゴリページでのみ表示される内容
<% /if %>

<% if:page_name eq 'tag' -%>
タグページでのみ表示される内容
<% /if %>
この記述は、以下の場所で使用します
container 終わり



フリーフォントで簡単ロゴ作成
フリーフォントでボタン素材作成
フリーフォントで吹き出し画像作成
フリーフォントではんこ画像作成
ほぼ自由に利用できるフリーフォント
フリーフォントの書体見本とサンプル
画像を大きく見る為のウインドウを開くボタンの作成

CSS ドロップシャドウの参考デモ
イラストAC
ぱくたそ
写真素材 足成
フリーフォント一覧
utf8 文字ツール
右サイド 終わり
base 終わり