1 / 23

Windows Scripting 與系統管理

Windows Scripting 與系統管理. 姓名 hfcheng@cis.nctu.edu.tw 交通大學資訊科學系 系計算機中心. Outline. 為何要在 Windows 下使用 script? Windows Scripting 相關的基礎知識 VB Script 初步 WMI 和 ADSI 常使用的物件 範例 參考資料. 為何要在 Windows 下使用 script ?. Windows 並不是只有 GUI 的管理介面可用 什麼情況下使用 script 會比使用 GUI 更方便? 將工作批次處理

chanton
Télécharger la présentation

Windows Scripting 與系統管理

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Windows Scripting與系統管理 姓名 hfcheng@cis.nctu.edu.tw 交通大學資訊科學系 系計算機中心

  2. Outline • 為何要在 Windows 下使用 script? • Windows Scripting 相關的基礎知識 • VB Script 初步 • WMI 和 ADSI • 常使用的物件 • 範例 • 參考資料 交通大學資訊科學系 系計算機中心

  3. 為何要在 Windows 下使用 script? • Windows 並不是只有 GUI 的管理介面可用 • 什麼情況下使用 script 會比使用 GUI 更方便? • 將工作批次處理 • 一次執行大量性質類似的工作 • 讓許多使用者執行相同的工作 • 為什麼要使用 Script 做系統管理? • 減少管理上所需的時間和複雜度 • Windows 下面的 Script 能夠做些什麼? • 絕對比你想的還要多 • 大部分 GUI 能完成的工作都可以用 script 完成 交通大學資訊科學系 系計算機中心

  4. Windows Scripting 相關的基礎知識 • 可使用的語言 • VBScript and Jscript • 副檔名 • .vbs / .js / .wsf • .wsf 具有類似 XML 的結構,可使用多種 script 語言 • .wsf 可以遠端執行 • Script Host • Wscript • Window based script interpreter • Cscript • Command line based script interpreter • 系統預設是以 Wscript 作為標準的解譯器 交通大學資訊科學系 系計算機中心

  5. 執行 Script 的方法 • 使用命令提示字元 • 使用檔案總管 • Double-click • 遠端執行 • 透過 WSH • 需要更改 Registry,預設為關閉 • Drag and drop 交通大學資訊科學系 系計算機中心

  6. Wscript / Cscript 參數 • //H:CScript or //H:WScript • 更改預設的 Script Host • //nologo • 不顯示 banner • //T:nn • 設定執行時的 timeout 時間 • //S • 儲存目前的設定 • //B • 批次模式,關閉所有輸入及錯誤訊息 • //I • 互動模式,與 //B 相反 交通大學資訊科學系 系計算機中心

  7. VB Script 初步 (1/4) • 類似 VisualBasic 的語法 • 使用 ‘_’ 字元連接多行 • 如同 C 語言使用 ‘\’ • 物件 • Set objWMIService = GetObject("winmgmts:") • 變數和常數 • Filesize = 1024 • Const MaxFileSize = 4096 • 字串 • Filepath = “C:\Temp\” • 使用 ‘&’ 來連接字串 • Filename = Filepath & “test.txt” 交通大學資訊科學系 系計算機中心

  8. VB Script 初步 (2/4) • IF Else 判斷句 • If conditions then … Else if conditions then … Else … End if • Select Case 判斷句 • Select case variable Case condition … Case condition … End select • For Next 迴圈 • For i=1 to n … Next • For Each object in container … Next • Do Loop 迴圈 • Do While condition … Loop • Do … Loop While condition 交通大學資訊科學系 系計算機中心

  9. VB Script 初步 (3/4) • 陣列 • Computers = Array(“cismpc01”, “cismpc02”, “cismpc03”) • 使用 For Each 來列舉所有陣列內的元素 • For Each computer in Computers…. • Procedures / Functions • Sub procedurename() … End Sub • Function funcname() … End Function • 錯誤處理 • 預設情況下遇到錯誤會立刻中斷執行 • On Error Resume Next • Err 物件 • Err.Number / Err.Description / Err.Clear 交通大學資訊科學系 系計算機中心

  10. VB Script 初步 (4/4) On Error Resume Next Const CONVERSION_FACTOR = 1048576 Const WARNING_THRESHOLD = 100 If WScript.Arguments.Count = 0 Then _ Wscript.Echo "Usage: FirstScript.vbs server1 [server2] [server3] ...“ WScript.Quit End If For Each Computer In WScript.Arguments Set objWMIService = GetObject("winmgmts://" & Computer) If Err.Number <> 0 Then Wscript.Echo Computer & " " & Err.Description Err.Clear Else Set colLogicalDisk = objWMIService.InstancesOf("Win32_LogicalDisk") For Each objLogicalDisk In colLogicalDisk FreeMegaBytes = objLogicalDisk.FreeSpace / CONVERSION_FACTOR If FreeMegaBytes < WARNING_THRESHOLD Then _ Wscript.Echo Computer & " " & objLogicalDisk.DeviceID & _ " is low on disk space.“ End If Next End If Next 交通大學資訊科學系 系計算機中心

  11. ADSI(ActiveDirectory Service Interface) • 用來存取 ActiveDirectory 的軟體介面 • 使用標準 LDAP 路徑來存取目錄服務中的元件 • 撰寫 ADSI Scripts 時常用的流程 • 連接到 ActiveDirectory 物件 • 處理物件(更新/複製/刪除) • 儲存所更改的設定 • Object.SetInfo 交通大學資訊科學系 系計算機中心

  12. WMI(Windows Management Instrumentation) • 透過網路存取及共享管理資訊的一個機制 • 提供 event subscription 和 notification 功能 • 如同 SNMP 中的 traps • 可以透過 WMI 控制其他 Microsoft 應用程式 • Exchange Server, SQL Server • WMI Providers • ActiveDirectory, EventLog, PerfMon, Registry, SNMP, Win32 • 然而使用 script 並無法完全發揮 WMI 的能力 交通大學資訊科學系 系計算機中心

  13. 常用的物件 (1/3) • Wscript • Wscript.Arguments(n) • Wscript.Echo • Wscript.Quit • WshNetwork (WScript.Network) • WshNetwork.ComputerName • WshNetwork.AddWindowsPrinterConnection • WshNetwork.MapNetworkDrive • WshShell (WScript.Shell) • WshShell.Exec • WshShell.Run • WshShell.Popup • WshShell.RegRead 交通大學資訊科學系 系計算機中心

  14. 常用的物件 (2/3) • FileSystemObject • 為一個 runtime 函式庫 • 使用方法 • Set objFSO = CreateObject(“Scripting.FileSystemObject”) • Object • File, Folder, Drive • Properties • Drives, Files, Name, SubFolder, AtEndOfLine • Methods • Copy, Delete, Move, Read, Write • http://www.microsoft.com/technet/scriptcenter/scrguide/sas_scr_xfxi.asp 交通大學資訊科學系 系計算機中心

  15. 常用的物件 (3/3) • ADSI objects • IADs, IADsContainer • IADsUser, IADsOU, IADsGroup, IADsComputer • http://msdn.microsoft.com/library/en-us/netdir/adsi/adsi_reference.asp • WMI objects • SWbemLocator, SWbemService • Win32_Computer, Win32_Services, Win32_OperatingSystem, Win32_NetworkConnection • http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/wmi_start_page.asp 交通大學資訊科學系 系計算機中心

  16. 範例列舉 OU 下的所有使用者並修改其設定 Const ADS_PROPERTY_CLEAR = 1 Set objOU = GetObject _ ("LDAP://ou=" & WScript.Arguments(0) & ",ou=CIS,dc=nt,dc=cis,dc=nctu,dc=edu,dc=tw") ObjOU.Filter=("User") For Each objUser in objOU Set objUser2 = GetObject (“LDAP://” & objUser.Name & “,ou=” & WScript.Arguments(0) & _ ",ou=CIS,dc=nt,dc=cis,dc=nctu,dc=edu,dc=tw") 'objUser2.GetInfo 'objUser2.Put "scriptPath", "logon.cmd" objUser2.PutEx ADS_PROPERTY_CLEAR, "scriptPath", 0 'objUser2.Put "profilePath", "\\ADSERV\Home\" & WScript.Arguments(0) & "\" & _ objUser2.CN & "\profile\" 'objUser2.Put "homeDrive", "Z:" 'objUser2.Put "homeDirectory", "\\ADSERV\Home\" & WScript.Arguments(0) & "\" & objUser2.CN objUser2.SetInfo Next 交通大學資訊科學系 系計算機中心

  17. 範例檢查是否有使用者 login 在該台電腦上 On Error resume next strComputer = Wscript.Arguments.Item(0) strLoggedOnUser = "" WScript.Echo strComputer Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colComputer = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem") if not err.number = 0 then Wscript.echo err.number & " " & Err.helpfile quit end if For Each objComputer in colComputer strLoggedOnUser = objComputer.UserName Next if not strLoggedOnUser = "" then Wscript.Echo "User " & strLoggedOnUser & " is logged on the destination client machine," 交通大學資訊科學系 系計算機中心

  18. 範例新增/刪除印表機連線 strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colInstalledPrinters = objWMIService.ExecQuery _ ("Select * from Win32_Printer") For Each objPrinter in colInstalledPrinters objPrinter.Delete_ Next Set WshNetwork = CreateObject("WScript.Network") WshNetwork.AddWindowsPrinterConnection "\\ADSERV\HPLaserJet 4050 (EC229)" WshNetwork.AddWindowsPrinterConnection "\\ADSERV\HPLaserJet 4050 (EC235)" 交通大學資訊科學系 系計算機中心

  19. 範例更改電腦名稱 strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colComputers = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem") For Each objComputer in colComputers err = ObjComputer.Rename(strComputerName) If err = 0 WScript.Echo "Change Name successfully !" Next 交通大學資訊科學系 系計算機中心

  20. 範例將電腦加入網域 Const JOIN_DOMAIN = 1 Const ACCT_CREATE = 2 Const ACCT_DELETE = 4 Const WIN9X_UPGRADE = 16 Const DOMAIN_JOIN_IF_JOINED = 32 Const JOIN_UNSECURE = 64 Const MACHINE_PASSWORD_PASSED = 128 Const DEFERRED_SPN_SET = 256 Const INSTALL_INVOCATION = 262144 strDomain = “CISAD" strPassword = “******" strUser = "Administrator" Set objNetwork = CreateObject("WScript.Network") strComputer = objNetwork.ComputerName Set objComputer = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\" & _ strComputer & "\root\cimv2:Win32_ComputerSystem.Name='" & strComputer & "'") ReturnValue = objComputer.JoinDomainOrWorkGroup(strDomain, strPassword, _ strDomain & "\" & strUser, NULL, JOIN_DOMAIN + ACCT_CREATE) 交通大學資訊科學系 系計算機中心

  21. 範例執行外部程式 Dim WshShell, oExec Set WshShell = CreateObject("WScript.Shell") Set oExec = WshShell.Exec("go.cmd ") Dim WshShell, oExec Set WshShell = CreateObject("WScript.Shell") Set oExec = WshShell.Run("go2.cmd ") 交通大學資訊科學系 系計算機中心

  22. 實用的工具 • For easy scripting • ADSI Scriptomatic • WMI Scriptomatic • Tweakomatic • WMI Tools 交通大學資訊科學系 系計算機中心

  23. 參考資料 • Microsoft TechNet Script Center • http://www.microsoft.com/technet/treeview/default.asp?url=/technet/scriptcenter/default.asp • Microsoft Windows2000 Scripting Guide • http://www.microsoft.com/technet/treeview/default.asp?url=/technet/scriptcenter/scrguide/sas_vbs_overview.asp • Active Directory Service Interface on MSDN • http://msdn.microsoft.com/library/default.asp?url=/library/en-us/netdir/adsi/active_directory_service_interfaces_adsi.asp • MSDN Scripting Site • http://msdn.microsoft.com/scripting 交通大學資訊科學系 系計算機中心

More Related