WSH プログラミングの基本的なお話


  既定のスクリプトホスト




Cscript.exe も、Wscript.exe も引数に、既定のスクリプトホストを変更するオプションを持っています。

//H:CScript 既定のスクリプト ホストを CScript.exe に変更する
//H:WScript 既定のスクリプト ホストを WScript.exe に変更する (既定値)

これらが実行された時に何が変わるかというと、レジストリの中の WSH に関係づけられた拡張子の Shell\Open\Command と Shell\Open2\Command が入れ替わります。

例えば、HKEY_LOCAL_MACHINE\SOFTWARE\Classes\VBSFile\Shell\Open\Command の内容を表示するスクリプトは以下のようになります

  

Set WshShell = WScript.CreateObject("WScript.Shell")

strPath = "HKLM\SOFTWARE\Classes\VBSFile\Shell\Open\Command\"
strValue = WshShell.RegRead( strPath )
MsgBox( strValue )
  



この事を利用して、もし既定のスクリプトホストが、WScript であったなら、一時的に CScript に変更してコマンドプロンプトを表示して別のスクリプトを実行して PAUSE するというプログラムを作成してみます

必ず CScript で実行されるスクリプト


  必ず CScript で実行されるスクリプト




以下のスクリプトは、表示を Wscript.Echo で行っている為、WScript で実行されるとメッセージボックスが何度も表示されてしまいます。その為、Wscript では実行できないようにチェックをしています

  

str = WScript.FullName
str = Right( str, 11 )
str = Ucase( str )
if str <> "CSCRIPT.EXE" then
	strMessage = "コマンドプロンプトより cscript " & WScript.ScriptFullName
	strMessage = strMessage & " と指定して実行して下さい   " & vbCrLf & vbCrLf
	strMessage = strMessage & "( この文字列をクリップボードにコピーしたい場合は"
	strMessage = strMessage & " ctrl+c です )"
	WScript.Echo strMessage
	WScript.Quit
end if
' **********************************************************
' 実行中サービス一覧
' **********************************************************
strServer = "."

on error resume next
Set objLocator = CreateObject("WbemScripting.SWbemLocator")
if Err.Number <> 0 then
	Wscript.Echo Err.Description
	Wscript.Quit
end if

Set objWmi = objLocator.ConnectServer( _
	strServer, _
	"root\cimv2", _
	strUser, _
	strPass )
if Err.Number <> 0 then
	Wscript.Echo Err.Description
	Wscript.Quit
end if

objWmi.Security_.ImpersonationLevel = 3
if Err.Number <> 0 then
	Wscript.Echo Err.Description
	Wscript.Quit
end if
on error goto 0

Set objTarget = objWmi.ExecQuery( _
 "select Name,Started,PathName from Win32_Service" _
)

on error resume next
For Each obj in objTarget

	if obj.Started then
		Wscript.Echo obj.Name
	end if

Next
on error goto 0
  

そこで、以下のような呼び出し用のスクリプトを作成します

  

Set WshShell = WScript.CreateObject( "WScript.Shell" )
Set Fso	= CreateObject( "Scripting.FileSystemObject" )

' **********************************************************
' 実行されているこのディレクトリを取得
' **********************************************************
strCurPath = WScript.ScriptFullName
Set obj = Fso.GetFile( strCurPath )
Set obj = obj.ParentFolder
strCurPath = obj.Path

' **********************************************************
' チェック用の文字列を取得
' **********************************************************
strPath = "HKLM\SOFTWARE\Classes\VBSFile\Shell\Open\Command\"
strValue = WshShell.RegRead( strPath )

' **********************************************************
' Wscript なら、Cscript に変更
' **********************************************************
bWscript = False
if InStr( 1, strValue, "WSCRIPT", 1 ) > 0 then
	bWscript = True
	' コマンドプロンプトを開かない同期処理
	Call WshShell.Run( "cscript.exe /H:Cscript", 0, True )
end if

' **********************************************************
' 同期でコマンドプロンプトを開いて、PAUSE
' **********************************************************
strCommand = "cmd.exe /c """ & strCurPath
strCommand = strCommand & "\list_service_started.vbs"""
strCommand = strCommand & " & pause"
Call WshShell.Run( strCommand, , True )


' **********************************************************
' 元に戻す
' **********************************************************
if bWscript then
	Call WshShell.Run( "cscript.exe /H:Wscript", 0, True )
end if
  

WshShell.Run については、以下を参照して下さい
http://winofsql.jp/VA003334/vbsguide051215192026.htm




yahoo  google  MSDN  MSDN(us)  WinFAQ  Win Howto  tohoho  ie_DHTML  vector  wdic  辞書  天気 


[vbsguide]
claudebot
24/03/29 23:41:01
InfoBoard Version 1.00 : Language=Perl

1 BatchHelper COMprog CommonSpec Cprog CprogBase CprogSAMPLE CprogSTD CprogSTD2 CprogWinsock Cygwin GameScript HTML HTMLcss InstallShield InstallShieldFunc JScript JScriptSAMPLE Jsfuncs LLINK OldProg OracleGold OracleSilver PRO PRObrowser PROc PROconePOINT PROcontrol PROftpclient PROjscript PROmailer PROperl PROperlCHAT PROphp PROphpLesson PROphpLesson2 PROphpLesson3 PROphpfunction PROphpfunctionArray PROphpfunctionMisc PROphpfunctionString PROsql PROvb PROvbFunction PROvbString PROvbdbmtn PROvbonepoint PROwebapp PROwin1POINT PROwinSYSTEM PROwinYOROZU PROwindows ProjectBoard RealPHP ScriptAPP ScriptMaster VBRealtime Vsfuncs a1root access accreq adsi ajax amazon argus asp aspSample aspVarious aspdotnet aw2kinst cappvariety centura ckeyword classStyle cmaterial cmbin cmdbapp cmenum cmlang cmlistbox cmstd cmstdseed cmtxt cs daz3d db dbCommon dbaccess dnettool dos download flex2 flex3 flex4 framemtn framereq freeWorld freesoft gimp ginpro giodownload google hdml home hta htmlDom ie9svg install java javaSwing javascript jetsql jquery jsp jspTest jspVarious lightbox listasp listmsapi listmsie listmsiis listmsnt listmspatch listmsscript listmsvb listmsvc memo ms msde mysql netbeans oraPlsql oracle oracleWiper oraclehelper orafunc other panoramio pear perl personal pgdojo pgdojo_cal pgdojo_holiday pgdojo_idx pgdojo_ref pgdojo_req php phpVarious phpguide plsql postgres ps r205 realC realwebapp regex rgaki ruby rule sboard sc scprint scquest sdb sdbquest seesaa setup sh_Imagick sh_canvas sh_dotnet sh_google sh_tool sh_web shadowbox shgm shjquery shvbs shweb sjscript skadai skywalker smalltech sperl sqlq src systemdoc tcpip tegaki three toolbox twitter typeface usb useXML vb vbdb vbsfunc vbsguide vbsrc vpc wcsignup webanymind webappgen webclass webparts webtool webwsh win8 winofsql wmi work wp youtube