#lookup(Google.jp,検索,WSH) #contents *WSH [#r2643e45] -Microsoft TechNet: スクリプト センター --http://www.microsoft.com/japan/technet/community/scriptcenter/ **WSH [#rac80114] -Windows Script --http://www.microsoft.com/japan/msdn/scripting/ -@IT:運用 Windows管理者のためのWindows Script Host --http://www.atmarkit.co.jp/fwin2k/operation/wsh01/wsh01_01.html -Windows Scripting Host Laboratory --http://www.roy.hi-ho.ne.jp/mutaguchi/wsh/ **Shell [#a026ddb6] ***CopyHere Method [#ve751336] -Copying Folders by Using the Shell Folder Object --http://www.microsoft.com/resources/documentation/windows/2000/server/scriptguide/en-us/sas_fil_vrwr.mspx -CopyHere Method --http://msdn.microsoft.com/library/en-us/shellcc/platform/shell/reference/objects/folder/copyhere.asp ***コンソール [#x0dd5b96] -User Interaction with Windows Script Host --http://www.microsoft.com/mind/1199/interaction/interaction.asp *VBScript [#nb235981] **外部ファイルのインクルード方法 [#be87a874] -Execute ステートメントを使うことで外部ファイルを取り込むことが可能。 ***サンプル [#y11b838d] -Include.vbs Const g_strA = "AAA" Const g_strB = "BBB" -ExecuteTest.vbs Option Explicit Main Sub Main Dim fso Set fso = CreateObject("Scripting.FileSystemObject") Dim f Set f = fso.OpenTextFile( "d:\home\edu\VBScript\ExecuteTest\include.vbs", 1, False ) Dim strBuf strBuf = f.ReadAll WScript.Echo "<内容確認>" WScript.Echo strBuf Execute strBuf WScript.Echo "<Execute の実行結果>" WScript.Echo g_strA WScript.Echo g_strB End Sub -実行結果 <内容確認> Const g_strA = "AAA" Const g_strB = "BBB" <Execute の実行結果> AAA BBB **FileSystemObject [#s3331e07] ***テキストファイルを連結するサンプル [#r57abb8a] -コード ' AppendFile.vbs ' 使い方: ' 1. DIR_SRC にコピー元のファイルがあるフォルダ名を指定します。 ' 2. DIR_DST にコピー先のフォルダ名を指定します。 ' 3. FILE_ADD に追加するテキストファイルを記述します。 ' 4. AppendFile.vbs をダブルクリックします。 ' DIR_DST の場所に連結されたファイルが作成されます。 ' Const DIR_SRC = "D:\home\edu\VBScript\AppendFile\src" ' コピー元のフォルダ名 Const DIR_DST = "D:\home\edu\VBScript\AppendFile\dst" ' コピー先のフォルダ名 Const FILE_ADD = "D:\home\edu\VBScript\AppendFile\add.txt" ' 追加するテキストファイル AppendFile Sub AppendFile Dim fso Set fso = CreateObject("Scripting.FileSystemObject") Dim folder Set folder = fso.GetFolder( DIR_SRC ) Dim strFileName ' コピー元のファイル名が入ります Dim txtFile1 Dim txtFile2 Dim txtFile3 Dim strMargeText ' 文字列連結用変数 For Each strFileName In folder.Files Set txtFile1 = fso.OpenTextFile( strFileName, 1, False ) Set txtFile2 = fso.OpenTextFile( FILE_ADD, 1, False ) strMargeText = txtFile1.ReadAll & txtFile2.ReadAll Set txtFile3 = fso.CreateTextFile( DIR_DST & "\" & fso.GetFileName(strFileName) , True) txtFile3.WriteLine( strMargeText ) Next End Sub ***ログファイル [#m3d2ef2d] -Read a Comma Separated Values Log --http://www.microsoft.com/technet/scriptcenter/scripts/logs/text/lgtxvb01.mspx -Read a Fixed Width Column Log --http://www.microsoft.com/technet/scriptcenter/scripts/logs/text/lgtxvb02.mspx **ADO [#daca0f98] -Recordsetを配列として使う方法 --http://www.seraphyware.jp/dev/tips/vb.tips.ado2dim.html ***ADOレコードセットをXMLに出力するサンプル [#b0698ff0] -コード ' RecordsetToXml.vbs Const adVarChar = 200 Const adFldIsNullable = 32 Const adPersistXML = 1 Const adReadAll = -1 RecordsetToXml Sub RecordsetToXml() Dim rsTmp Set rsTmp = CreateObject("ADODB.Recordset") With rsTmp.Fields .Append "Field1", adVarChar, 80, adFldIsNullable .Append "Field2", adVarChar, 80, adFldIsNullable End With rsTmp.Open Dim vFieldList vFieldList = Array("Field1", "Field2") Dim vValues vValues = Array("aaa", "bbb") rsTmp.AddNew vFieldList, vValues vValues = Array("CCC", "DDD") rsTmp.AddNew vFieldList, vValues rsTmp.MoveFirst Dim fld Dim strLine Dim strName strLine = "" For Each fld In rsTmp.Fields strName = fld.Name strLine = strLine & strName & vbTab Next WScript.Echo strLine Dim strValue While Not rsTmp.BOF And Not rsTmp.EOF strLine = "" For Each fld In rsTmp.Fields strValue = fld.Value strLine = strLine & "[" & strValue & "]" & vbTab Next WScript.Echo strLine rsTmp.MoveNext Wend Dim stm Set stm = CreateObject("ADODB.Stream") rsTmp.Save stm, adPersistXML Dim strResult strResult = stm.ReadText(adReadAll) WScript.Echo strResult End Sub -関連情報 --Internet Explorer で ADODB.Stream オブジェクトを無効にする方法 ---http://www.microsoft.com/japan/security/incident/adostream.mspx -結果 <xml xmlns:s='uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882' xmlns:dt='uuid:C2F41010-65B3-11d1-A29F-00AA00C14882' xmlns:rs='urn:schemas-microsoft-com:rowset' xmlns:z='#RowsetSchema'> <s:Schema id='RowsetSchema'> <s:ElementType name='row' content='eltOnly' rs:updatable='true'> <s:AttributeType name='Field1' rs:number='1' rs:nullable='true' rs:write='true'> <s:datatype dt:type='string' rs:dbtype='str' dt:maxLength='80' rs:precision='0' rs:maybenull='false'/> </s:AttributeType> <s:AttributeType name='Field2' rs:number='2' rs:nullable='true' rs:write='true'> <s:datatype dt:type='string' rs:dbtype='str' dt:maxLength='80' rs:precision='0' rs:maybenull='false'/> </s:AttributeType> <s:extends type='rs:rowbase'/> </s:ElementType> </s:Schema> <rs:data> <rs:insert> <z:row Field1='aaa' Field2='bbb'/> <z:row Field1='CCC' Field2='DDD'/> </rs:insert> </rs:data> </xml> ***ADO でテキストファイルをソートするサンプル [#h50bf7ba] -コード Option Explicit Main Sub Main() Dim named Set named = WScript.Arguments.Named Dim strTable If named.Exists("file") Then strTable = named("file") WScript.Echo "Table = [" & strTable & "]" Else WScript.Echo "Usage : CScript //Nologo SortFile.vbs /File:<filename.txt>" WScript.Quit() End If Dim cn Set cn = CreateObject("ADODB.Connection") With cn .Provider = "Microsoft.Jet.OLEDB.4.0" .Properties("Data Source") = "." .Properties("Extended Properties") = "TEXT;HDR=No;" End With cn.Open Dim strSQL strSQL = "SELECT F1 FROM " & strTable & " ORDER BY F1 ASC" Dim rs Set rs = cn.Execute( strSQL ) While Not rs.BOF And Not rs.EOF WScript.Echo rs(0) rs.MoveNext Wend End Sub **CDO [#i203e2d3] -Windows標準機能とWSHを使ってメールを送信する --http://www.atmarkit.co.jp/fwin2k/win2ktips/428wshmail/wshmail.html -Creating and Sending a Message --http://msdn.microsoft.com/library/en-us/cdosys/html/_cdosys_messaging_examples_creating_and_sending_a_message.asp -アプリケーションで電子メールを利用する --http://www.microsoft.com/japan/msdn/library/ja/jpcdo/html/CdoGetMessage__Session_.asp ***CDO でメールを送るサンプル。 [#h8667629] -コード 'SendMailTest.vbs Option Explicit 'On Error Resume Next const cdoBasic = 1 const cdoPostUsingPort = 2 const cdoSendUsingMethod = "http://schemas.microsoft.com/cdo/configuration/sendusing" const cdoSMTPServer = "http://schemas.microsoft.com/cdo/configuration/smtpserver" const cdoSMTPConnectionTimeout = "http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout" const cdoSMTPAuthenticate = "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate" const cdoSendUserName = "http://schemas.microsoft.com/cdo/configuration/sendusername" const cdoSendPassword = "http://schemas.microsoft.com/cdo/configuration/sendpassword" Dim iMsg Set iMsg = CreateObject("CDO.Message") Dim iConf Set iConf = CreateObject("CDO.Configuration") Dim Flds Set Flds = iConf.Fields With Flds ' assume constants are defined within script file .Item(cdoSendUsingMethod) = 2 ' cdoSendUsingPort .Item(cdoSMTPServer) = "smtp.example.co.jp" .Item(cdoSMTPConnectionTimeout) = 10 ' quick timeout .Item(cdoSMTPAuthenticate) = cdoBasic .Item(cdoSendUserName) = "username" .Item(cdoSendPassword) = "password" .Update End With With iMsg Set .Configuration = iConf .From = "sender@example.co.jp" .To = "receiver@example.co.jp" .Subject = "test" .TextBody = "This is a test." .Send End With **WMI [#s064c73a] -WMI による Windows の管理 --http://www.microsoft.com/japan/msdn/library/ja/jpdnwmi/htm/wmi.asp -MSDN Library - WMI --http://msdn.microsoft.com/library/en-us/dnanchor/html/anch_wmi.asp -WMI スクリプト入門 : 第 1 部 --http://www.microsoft.com/japan/msdn/columns/scripting/scripting06112002.asp -Windows 2000: スクリプト天国 --http://www.microsoft.com/japan/msdn/columns/scripting/scripting02142000.asp -システム管理は楽ですか? (前編) 〜 Windows Management Instrumentation 〜 --http://www.microsoft.com/japan/msdn/thisWeek/wmi/koj990730.asp -システム管理は楽ですか? (後編) 〜 Windows Management Instrumentation 〜 --http://www.microsoft.com/japan/msdn/thisweek/wmi/koj990811.asp -WMI Fun !! --http://wmifun.atinfinity.net/ -WMIいぢり 第1回:WMIって? --http://www.showg.jp/tamper/wmi/wmi001.htm -WMIいぢり 番外編:サンプル集 - WMIをいぢる - ものいぢり - いつまでも構想中 --http://www.showg.jp/tamper/wmi/wmi101.htm -演習 1 : 簡単な WMI タスク --http://www.microsoft.com/japan/technet/prodtechnol/windowsserver2003/proddocs/entserver/prog_wmi_tut_03_01.asp -827227 - Visual Basic スクリプトを使用してリモート ホスト コンピュータにセキュリティ修正プログラムをインストールする方法 --http://support.microsoft.com?kbid=827227 -お前ら、wsh使ってますか? --http://pc.2ch.net/win/kako/1009/10091/1009175619.html -リモートコンピュータへアクセス --http://wmifun.atinfinity.net/wmistep/wmistep06.html ---XP以降ではリモートコンピュータに画面は出せない仕様になったらしい。 --Administering Windows through Windows Script Host ---http://www.microsoft.com/mind/1199/adminWSH/adminWSH.asp -WMIサンプル集 - オペレーティングシステム関連 --http://www2.noritz.co.jp/anchor/ashp/netmon/samples/wmi_os.html ***メモ帳を終了させるプログラム [#v4e3e130] -コード ' KillNotepad.vbs Main Sub Main() For Each Process In GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery("select * from Win32_Process where Name='NOTEPAD.EXE'") Set oProcess = Process WScript.Echo oProcess.ExecutablePath Process.Terminate Next End Sub ***隣のPCでプログラムを実行させる方法。 [#rd8a3f6a] -コード Set args = Wscript.Arguments If args.Count < 1 Then Wscript.StdOut.WriteLine "xstart <command> [<computer>]" Else '--- コマンド指定 strCommand = args(0) '--- コンピュータ指定 strComputerPath = "" If args.Count = 2 Then strComputer = args(1) If InStr(strComputer, "\\") = 1 Then strComputer = Mid(strComputer, 3) End If strComputerPath = "\\" & strComputer & "\root\cimv2:" End If '--- WMI に接続 Wim32_Process クラスを取り出す Set clsProcess = GetObject("winmgmts:{impersonationLevel=impersonate}" _ & "!" & strComputerPath & "Win32_Process") '--- プロセスの作成 lngResult = clsProcess.Create(strCommand) Wscript.StdOut.WriteLine strCommand & " :" & lngResult End If --http://www.users.gr.jp/ml/archive/viewer.aspx?name=wsh&no=120 **XML [#y573fad2] -無料のVBScriptでXMLプログラミング --http://www.atmarkit.co.jp/fxml/rensai/msxml01/msxml01.html **正規表現 [#f42d6561] -正規表現による Visual Basic Scripting Edition (VBScript) の機能強化 --http://www.microsoft.com/japan/msdn/columns/scripting/scripting051099.asp -Windows Script 5.5 の新機能 --http://www.microsoft.com/japan/msdn/columns/scripting/scripting121399.asp -Regular Expression (RegExp) オブジェクト --http://www.microsoft.com/japan/msdn/library/ja/script56/html/vsobjregexp.asp ***URIを解析するサンプル [#k3ef9775] -コード Option Explicit Dim strData Dim regExp Set regExp = New RegExp strData = "http://www.hondarer-soft.com/cx/" regExp.Pattern = "(\w+):\/\/([^/:]+)(:\d*)?([^# ]*)" Dim matches Dim match Dim strValue Dim i Set matches = regExp.Execute( strData ) For Each match In matches For i = 0 To match.SubMatches.Count -1 strValue = match.SubMatches(i) WScript.Echo "match.SubMatches(" & i & ") = [" & strValue & "]" Next Next -実行結果 match.SubMatches(0) = [http] match.SubMatches(1) = [www.hondarer-soft.com] match.SubMatches(2) = [] match.SubMatches(3) = [/cx/] **関連リンク [#qd192127] -VBScript Tools by Bill James --http://www.billsway.com/vbspage/ *その他 [#d7da8493] **コマンド [#k64e2551] ***Runas [#bcebc3e6] -Runas --http://www.microsoft.com/japan/technet/prodtechnol/windowsserver2003/proddocs/entserver/runas.asp **関連リンク [#n2d3620a] -Windows Scripting Solutions --http://www.winnetmag.com/WindowsScripting/ #access //#access