• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            C++ Programmer's Cookbook

            {C++ 基礎} {C++ 高級} {C#界面,C++核心算法} {設計模式} {C#基礎}

            windows腳本實例(二)


            常用腳本實例:(以下都使用VBScript)

            (主要講解腳本宿主和腳本運行時,腳本創建com和腳本引擎的擴展先不講,以后再講)

             1  Creating and Controlling Files with FSO (Scripting.FileSystemObject)

            創建folder且collect 錯誤
             
             

            ' NewFolderEC.vbs
            '
             Free example VBScript to create a folder with error-correcting Code.
            '
             Author Guy Thomas http://computerperformance.co.uk/
            '
             Version 2.6 - May 2005
            '
             ---------------------------------------------------------------' 

            Option Explicit
            Dim objFSO, objFolder, objShell, strDirectory
            strDirectory 
            = "c:\logs"

            ' Create the File System Object
            Set objFSO = CreateObject("Scripting.FileSystemObject")

            ' Note If..Exists. Then, Else  End If construction
            If objFSO.FolderExists(strDirectory) Then
               
            Set objFolder = objFSO.GetFolder(strDirectory)
               WScript.Echo strDirectory 
            & " already created "
            Else
               
            Set objFolder = objFSO.CreateFolder(strDirectory)
            WScript.Echo 
            "Just created " & strDirectory
            End If

            If err.number = vbEmpty then
               
            Set objShell = CreateObject("WScript.Shell")
               objShell.run (
            "Explorer" &" " & strDirectory & "\" )
            Else WScript.echo "VBScript Error: " & err.number
            End If

            WScript.Quit

            ' End of Sample VBScript to create a folder with error-correcting Code


            創建文件且檢查文件是否已經存在
             
             

            ' NewFileEC.vbs
            '
             Sample VBScript to create a file with error-correcting Code
            '
             Author Guy Thomas http://computerperformance.co.uk/
            '
             Version 1.6 - June 2005
            '
             ---------------------------------------------------------------' 

            Option Explicit
            Dim objFSO, objFolder, objShell, objFile
            Dim strDirectory, strFile
            strDirectory 
            = "e:\logs"
            strFile 
            = "\Summer.txt"

            ' Create the File System Object
            Set objFSO = CreateObject("Scripting.FileSystemObject")

            ' Check that the strDirectory folder exists
            If objFSO.FolderExists(strDirectory) Then
               
            Set objFolder = objFSO.GetFolder(strDirectory)
            Else
            Set objFolder = objFSO.CreateFolder(strDirectory)
            WScript.Echo 
            "Just created " & strDirectory
            End If

            If objFSO.FileExists(strDirectory & strFile) Then
            Set objFolder = objFSO.GetFolder(strDirectory)
            Else
            Set objFile = objFSO.CreateTextFile(strDirectory & strFile)
            Wscript.Echo 
            "Just created " & strDirectory & strFile
            End If 

            set objFolder = nothing
            set objFile = nothing

            If err.number = vbEmpty then
            Set objShell = CreateObject("WScript.Shell")
            objShell.run (
            "Explorer" & " " & strDirectory & "\" )
            Else WScript.echo "VBScript Error: " & err.number
            End If

            WScript.Quit

            ' End of VBScript to create a file with error-correcting Code


            文件的末尾增加內容
             

            ' NewTextEC.vbs
            '
             Sample VBScript to write to a file. With added error-correcting
            '
             Author Guy Thomas http://computerperformance.co.uk/
            '
             Version 1.5 - August 2005
            '
             ---------------------------------------------------------------' 

            Option Explicit
            Dim objFSO, objFolder, objShell, objTextFile, objFile
            Dim strDirectory, strFile, strText
            strDirectory 
            = "e:\logs3"
            strFile 
            = "\Summer.txt"
            strText 
            = "Book Another Holiday"

            ' Create the File System Object
            Set objFSO = CreateObject("Scripting.FileSystemObject")

            ' Check that the strDirectory folder exists
            If objFSO.FolderExists(strDirectory) Then
               
            Set objFolder = objFSO.GetFolder(strDirectory)
            Else
               
            Set objFolder = objFSO.CreateFolder(strDirectory)
               WScript.Echo 
            "Just created " & strDirectory
            End If

            If objFSO.FileExists(strDirectory & strFile) Then
               
            Set objFolder = objFSO.GetFolder(strDirectory)
            Else
               
            Set objFile = objFSO.CreateTextFile(strDirectory & strFile)
               Wscript.Echo 
            "Just created " & strDirectory & strFile
            End If 

            set objFile = nothing
            set objFolder = nothing
            ' OpenTextFile Method needs a Const value
            '
             ForAppending = 8 ForReading = 1, ForWriting = 2
            Const ForAppending = 8

            Set objTextFile = objFSO.OpenTextFile _
            (strDirectory 
            & strFile, ForAppending, True)

            ' Writes strText every time you run this VBScript
            objTextFile.WriteLine(strText)
            objTextFile.Close

            ' Bonus or cosmetic section to launch explorer to check file
            If err.number = vbEmpty then
               
            Set objShell = CreateObject("WScript.Shell")
               objShell.run (
            "Explorer" &" " & strDirectory & "\" )
            Else WScript.echo "VBScript Error: " & err.number
            End If

            WScript.Quit

            ' End of VBScript to write to a file with error-correcting Code


            寫log data到文件
            ' EventLogFSOvbs
            '
             Example VBScript to interogate the Event Log and create a file 
            '
             Author Guy Thomas http://computerperformance.co.uk/
            '
             Version 1.8 - June 2005
            '
             ---------------------------------------------------------' 
            Option Explicit

            Dim objFso, objFolder, objWMI, objItem, objShell, strEventLog
            Dim strFile, strComputer, strFolder, strFileName, strPath
            Dim intEvent, intNumberID, intRecordNum, colLoggedEvents

            ' --------------------------------------------------------
            '
             Set the folder and file name
            '
             Set numbers
            intNumberID = 17 ' Event ID Number
            intRecordNum = 0

            strComputer 
            = "."
            strFileName 
            = "\Event" & intNumberID & ".txt"
            strFolder 
            = "e:\logs\eventlog"
            strPath 
            = strFolder & strFileName
            strEventLog 
            = "'System' "

            ' -----------------------------------------------------
            '
             Section to create folder and hold file.
            Set objFso = CreateObject("Scripting.FileSystemObject")
            If objFSO.FolderExists(strFolder) Then
            Set objFolder = objFSO.GetFolder(strFolder)
            Else
            Set objFolder = objFSO.CreateFolder(strFolder)
            Wscript.Echo 
            "Folder created " & strFolder
            End If

            Wscript.Echo 
            " Press OK and Wait 30 seconds (ish)"
            Set strFile = objFso.CreateTextFile(strPath, True)
            Set objWMI = GetObject("winmgmts:" _
            & "{impersonationLevel=impersonate}!\\" _
            & strComputer & "\root\cimv2")
            Set colLoggedEvents = objWMI.ExecQuery _
            (
            "Select * from Win32_NTLogEvent Where Logfile = " & strEventLog)

            ' -----------------------------------------
            '
             Next section loops through ID properties

            For Each objItem in colLoggedEvents
            If objItem.EventCode = intNumberID Then

            ' Second Loop to filter only if they tried Administrator
            strFile.WriteLine("Category: " & objItem.Category _
            & " string " & objItem.CategoryString) 
            strFile.WriteLine(
            "ComputerName: " & objItem.ComputerName) 
            strFile.WriteLine(
            "Logfile: " & objItem.Logfile _ 
            & " source " & objItem.SourceName) 
            strFile.WriteLine(
            "EventCode: " & objItem.EventCode) 
            strFile.WriteLine(
            "EventType: " & objItem.EventType) 
            strFile.WriteLine(
            "Type: " & objItem.Type) 
            strFile.WriteLine(
            "User: " & objItem.User) 
            strFile.WriteLine(
            "Message: " & objItem.Message)
            strFile.WriteLine (
            " ")
            intRecordNum 
            = intRecordNum +1
            End If
            Next

            ' Confirms the script has completed and opens the file
            Set objShell = CreateObject("WScript.Shell")
            objShell.run (
            "Explorer" &" " & strPath & "\" )

            WScript.Quit

            ' End of Guy's Sample FSO VBScript
            拷貝文件

            ' VBScript.
            Dim FSO
            Set FSO = CreateObject("Scripting.FileSystemObject")
            FSO.CopyFile 
            "c:\COMPlusLog.txt""c:\x\"
            拷貝文件夾
            ' VBScript.
            Dim FSO
            Set FSO = CreateObject("Scripting.FileSystemObject")
            FSO.CopyFolder 
            "c:\x""c:\y"


            2 讀寫注冊表
            ' VBScript.
            Set Sh = CreateObject("WScript.Shell")
            key 
            =  "HKEY_CURRENT_USER\"
            Sh.RegWrite key 
            & "WSHTest\""testkeydefault"
            Sh.RegWrite key 
            & "WSHTest\string1""testkeystring1"
            Sh.RegWrite key 
            & "WSHTest\string2""testkeystring2""REG_SZ"
            Sh.RegWrite key 
            & "WSHTest\string3""testkeystring3""REG_EXPAND_SZ"
            Sh.RegWrite key 
            & "WSHTest\int"123"REG_DWORD"
            WScript.Echo Sh.RegRead(key 
            & "WSHTest\")
            WScript.Echo Sh.RegRead(key 
            & "WSHTest\string1")
            WScript.Echo Sh.RegRead(key 
            & "WSHTest\string2")
            WScript.Echo Sh.RegRead(key 
            & "WSHTest\string3")
            WScript.Echo Sh.RegRead(key 
            & "WSHTest\int")
            Sh.RegDelete key 
            & "WSHTest\"

            3 創建快捷方式
             VBScript.
            Dim Shell, DesktopPath, URL
            Set Shell = CreateObject("WScript.Shell")
            DesktopPath 
            = Shell.SpecialFolders("Desktop")
            Set URL = Shell.CreateShortcut(DesktopPath & "\MSDN Scripting.URL")
            URL.TargetPath 
            = "HTTP://MSDN.Microsoft.com/scripting/"
            URL.Save

            ' VBScript.
            Set Shell = CreateObject("WScript.Shell")
            DesktopPath 
            = Shell.SpecialFolders("Desktop")
            Set link = Shell.CreateShortcut(DesktopPath & "\test.lnk")
            link.Arguments 
            = "1 2 3"
            link.Description 
            = "test shortcut"
            link.HotKey 
            = "CTRL+ALT+SHIFT+X"
            link.IconLocation 
            = "app.exe,1"
            link.TargetPath 
            = "c:\blah\app.exe"
            link.WindowStyle 
            = 3
            link.WorkingDirectory 
            = "c:\blah"
            link.Save

            以下主要使用WMI
            4 列舉系統進程
            ' Process.vbs
            '
             Free Sample VBScript to discover which processes are running
            '
             Author Guy Thomas http://computerperformance.co.uk/
            '
             Version 1.4 - December 2005
            '
             -------------------------------------------------------' 
            Option Explicit
            Dim objWMIService, objProcess, colProcess
            Dim strComputer, strList

            strComputer 
            = "."

            Set objWMIService = GetObject("winmgmts:" _
            & "{impersonationLevel=impersonate}!\\" _ 
            & strComputer & "\root\cimv2"

            Set colProcess = objWMIService.ExecQuery _
            (
            "Select * from Win32_Process")

            For Each objProcess in colProcess
            strList 
            = strList & vbCr & _
            objProcess.Name
            Next

            WSCript.Echo strList
            WScript.Quit

            ' End of List Process Example VBScript


            看看它的結果嗎:
            r_listprocess.jpg

            start stop 進程
            ' StartProcessLocal.vbs
            '
             Free example VBScript to start a process (not interactive)
            '
             Author Guy Thomas http://computerperformance.co.uk/
            '
             Version 1.8 - December 2005
            '
             -------------------------------------------------------' 
            Option Explicit
            Dim objWMIService, objProcess, objCalc
            Dim strShell, objProgram, strComputer, strExe 

            strComputer 
            = "."
            strExe 
            = "Calc.exe"
            ' Connect to WMI
            set objWMIService = getobject("winmgmts://"_
            & strComputer & "/root/cimv2"

            ' Obtain the Win32_Process class of object.
            Set objProcess = objWMIService.Get("Win32_Process")
            Set objProgram = objProcess.Methods_( _
            "Create").InParameters.SpawnInstance_
            objProgram.CommandLine 
            = strExe 

            'Execute the program now at the command line.
            Set strShell = objWMIService.ExecMethod( _
            "Win32_Process""Create", objProgram) 

            WScript.echo 
            "Created: " & strExe & " on " & strComputer
            WSCript.Quit 

            ' End of free example of a Process VBScript 



            ' ProcessKillLocal.vbs
            '
             Sample VBScript to kill a program
            '
             Author Guy Thomas http://computerperformance.co.uk/
            '
             Version 2.7 - December 2005
            '
             ------------------------ -------------------------------' 
            Option Explicit
            Dim objWMIService, objProcess, colProcess
            Dim strComputer, strProcessKill 
            strComputer 
            = "."
            strProcessKill 
            = "'calc.exe'" 

            Set objWMIService = GetObject("winmgmts:" _
            & "{impersonationLevel=impersonate}!\\" _ 
            & strComputer & "\root\cimv2"

            Set colProcess = objWMIService.ExecQuery _
            (
            "Select * from Win32_Process Where Name = " & strProcessKill )
            For Each objProcess in colProcess
            objProcess.Terminate()
            Next 
            WSCript.Echo 
            "Just killed process " & strProcessKill _
            & " on " & strComputer
            WScript.Quit 
            ' End of WMI Example of a Kill Process 

            物理和邏輯disks
            ' LogicalDisk.vbs
            '
             Sample VBScript to interrogate a Logical disk with WMI
            '
             Author Guy Thomas http://computerperformance.co.uk/
            '
             Version 1.8 - November 2005
            '
             -------------------------------------------------------------' 
            Option Explicit
            Dim objWMIService, objItem, colItems, strComputer

            On Error Resume Next
            strComputer 
            = "."

            Set objWMIService = GetObject _
            (
            "winmgmts:\\" & strComputer & "\root\cimv2")
            Set colItems = objWMIService.ExecQuery _
            (
            "Select * from Win32_LogicalDisk")

            For Each objItem in colItems
            Wscript.Echo 
            "Computer: " & objItem.SystemName & VbCr & _ 
            " ==================================" & VbCr & _ 
            "Drive Letter: " & objItem.Name & vbCr & _ 
            "Description: " & objItem.Description & vbCr & _ 
            "Volume Name: " & objItem.VolumeName & vbCr & _ 
            "Drive Type: " & objItem.DriveType & vbCr & _ 
            "Media Type: " & objItem.MediaType & vbCr & _ 
            "VolumeSerialNumber: " & objItem.VolumeSerialNumber & vbCr & _
            "Size: " & Int(objItem.Size /1073741824& " GB" & vbCr & _ 
            "Free Space: " & Int(objItem.FreeSpace /1073741824& _
            " GB" & vbCr & _ 
            "Quotas Disabled: " & objItem.QuotasDisabled & vbCr & _ 
            "Supports DiskQuotas: " & objItem.SupportsDiskQuotas & vbCr & _
            "Supports FileBasedCompression: " & _
            objItem.SupportsFileBasedCompression 
            & vbCr & _ 
            "Compressed: " & objItem.Compressed & vbCr & _ 
            "" 
            Next

            WSCript.Quit

            ' End of Sample DiskDrive VBScript


            ' DiskDriveLogical.vbs
            '
             Sample VBScript to interrogate a physical disk with WMI
            '
             Author Guy Thomas http://computerperformance.co.uk/
            '
             Version 2.3 - November 2005
            '
             --------------------------------------------------------------' 
            Option Explicit
            Dim objWMIService, objItem, colItems, strComputer

            ' On Error Resume Next
            strComputer = "."

            Set objWMIService = GetObject("winmgmts:\\" _
            & strComputer & "\root\cimv2")
            Set colItems = objWMIService.ExecQuery(_
            "Select * from Win32_DiskDrive")

            For Each objItem in colItems
            Wscript.Echo 
            "Computer: " & objItem.SystemName & VbCr & _ 
            "Status: " & objItem.Status & VbCr & _ 
            " ==================================" & VbCr & _ 
            "Name: " & objItem.Name & VbCr & _ 
            "Description: " & objItem.Description & VbCr & _ 
            "Signature: " & objItem.Signature & VbCr & _
            "Manufacturer: " & objItem.Manufacturer & VbCr & _ 
            "Model: " & objItem.Model & VbCr & _ 
            "Size: " & Int(objItem.Size /(1073741824)) & " GB" & VbCr & _ 
            "Number of Partitions: " & objItem.Partitions & VbCr & _ 
            "Total Cylinders: " & objItem.TotalCylinders & VbCr & _ 
            "Tracks PerCylinder: " & objItem.TracksPerCylinder & VbCr & _ 
            "Total Heads: " & objItem.TotalHeads & VbCr & _ 
            "Total Sectors: " & objItem.TotalSectors & VbCr & _ 
            "Bytes PerSector: " & objItem.BytesPerSector & VbCr & _
            "Sectors PerTrack: " & objItem.SectorsPerTrack & VbCr & _ 
            "Total Tracks: " & objItem.TotalTracks & VbCr & _
            " -------- SCSI Info ---------- "& VbCr & _
            "SCSI TargetId: " & objItem.SCSITargetId & VbCr & _
            "SCSI Bus: " & objItem.SCSIBus & VbCr & _
            "SCSI Logical Unit: " & objItem.SCSILogicalUnit & VbCr & _
            "SCSI Port: " & objItem.SCSIPort 
            Next
            WSCript.Quit

            ' End of Sample DiskDrive VBScript

            獲得memory
            ' Memory.vbs
            '
             Sample VBScript to discover how much RAM in computer
            '
             Author Guy Thomas http://computerperformance.co.uk/
            '
             Version 1.3 - August 2005
            '
             -------------------------------------------------------' 
            Option Explicit
            Dim objWMIService, objComputer, colComputer 
            Dim strLogonUser, strComputer 

            strComputer 
            = "." 

            Set objWMIService = GetObject("winmgmts:" _
            & "{impersonationLevel=impersonate}!\\" _ 
            & strComputer & "\root\cimv2"
            Set colComputer = objWMIService.ExecQuery _
            (
            "Select * from Win32_ComputerSystem"

            For Each objComputer in colComputer 
            Wscript.Echo 
            "System Name: " & objComputer.Name _
            & vbCr & "Total RAM " & objComputer.TotalPhysicalMemory
            Next 

            WScript.Quit 

            ' End of free example of Memory WMI / VBScript

            打開百度:
            Dim objIE 
            Set objIE = WScript.CreateObject ("InternetExplorer.Application"
            objIE.AddressBar 
            = true 
            objIE.Visible 
            = true 
            objIE.Navigate(
            "www.baidu.com")

            刪除你制定的services:
            strComputer = "."
            Set objWMIService = GetObject("winmgmts:" _
            & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
            Set colListOfServices = objWMIService.ExecQuery _
            (
            "Select * from Win32_Service Where Name = 'DbService'")
            For Each objService in colListOfServices
            objService.StopService()
            objService.Delete()
            Next

            internet 和server active directory 還有創建com等沒有講哦!



            主要參考:
            MS的document
            http://computerperformance.co.uk/

            posted on 2006-09-26 15:28 夢在天涯 閱讀(2933) 評論(4)  編輯 收藏 引用 所屬分類: Windows Script

            評論

            # re: windows腳本技術(二) 2006-09-26 17:39 ^_^

            夢在天涯兄蠻高產的啊,去當作家吧,很有前途的。不會比你講的腳本語言差。  回復  更多評論   

            # re: windows腳本技術(二) 2006-09-27 12:28 shaker

            恩 在來個續 講講怎么和編程語言交互  回復  更多評論   

            # re: windows腳本技術(二) 2006-10-12 08:52 漆黑的夜晚

            不錯,但怎么用來實現喲
              回復  更多評論   

            # re: windows腳本實例(二) 2008-07-26 03:26 冷不防

            謝謝!  回復  更多評論   

            公告

            EMail:itech001#126.com

            導航

            統計

            • 隨筆 - 461
            • 文章 - 4
            • 評論 - 746
            • 引用 - 0

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1804603
            • 排名 - 5

            最新評論

            閱讀排行榜

            99久久精品免费国产大片| 伊人久久五月天| 久久久久亚洲爆乳少妇无| 中文字幕无码免费久久| 久久精品视频免费| 一本久久知道综合久久| 精品久久久久久久久中文字幕| 久久久久女教师免费一区| 久久久久久久久无码精品亚洲日韩 | 久久久久亚洲av成人无码电影| 欧美亚洲国产精品久久高清 | 欧美精品丝袜久久久中文字幕| 久久人人妻人人爽人人爽| 久久久久亚洲AV无码去区首| 成人久久综合网| 久久久一本精品99久久精品66 | 久久久久人妻一区二区三区vr| 日韩AV毛片精品久久久| 国产精品无码久久综合网| 婷婷五月深深久久精品| 四虎国产精品成人免费久久| 国内精品久久久久久久亚洲| 999久久久国产精品| 久久国产精品一区二区| 国产成年无码久久久久毛片| 久久精品九九亚洲精品| 久久久久国产精品熟女影院 | 久久免费小视频| 9191精品国产免费久久| 99久久伊人精品综合观看| 亚洲国产精久久久久久久| 青青国产成人久久91网| 香蕉久久夜色精品国产小说| 久久久久四虎国产精品| 国产精品美女久久久| 99久久精品国产综合一区 | 久久99精品免费一区二区| 精品久久久久久无码中文野结衣| 国产L精品国产亚洲区久久| 国产成人AV综合久久| 日韩电影久久久被窝网|