<form id="hz9zz"></form>
  • <form id="hz9zz"></form>

      <nobr id="hz9zz"></nobr>

      <form id="hz9zz"></form>

    1. 明輝手游網中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

      Script經典文章

      [摘要]開篇:讀取環境變量 讓我們以一個在許多環境中都非常實用的示例腳本作為開篇。當您使用登錄腳本或批處理文件時,讀取環境變量是一項非常普遍的操作。您可以通過諸如%COMPUTERNAME%和%WINDIR...

      開篇:讀取環境變量 

      讓我們以一個在許多環境中都非常實用的示例腳本作為開篇。當您使用登錄腳本或批處理文件時,讀取環境變量是一項非常普遍的操作。您可以通過諸如%COMPUTERNAME%和%WINDIR%之類的腳本來映射網絡驅動器,連接打印機或執行其它希望在登錄/注銷腳本中完成的相關操作。有關如何通過編程方式從腳本中訪問這些變量的示例之一是使用Wscript.Shell對象。 

      如果希望在本地運行一個應用程序、對注冊表內容進行操作、創建一個快捷方式或訪問某個系統文件夾,您可以隨時創建一個Wscript.Shell(WshShell)對象。WshShell對象能夠提供一個環境集合,這個集合允許您對各種環境變量(如WINDIR、PATH或PROMPT)進行處理。 

      例如(說明:如需對這個腳本進行測試,請根據您所處域環境中的服務器配置情況對腳本中的DC名稱進行修改): 

      ' –Start
      Dim wshShell
      ' Create a new Windows Scripting Host Shell object
      Set wshShell = CreateObject("Wscript.Shell")
      ' Set it to read the environment variables
      Set EnvVar = wshShell.Environment("PROCESS")
      ' Re-direct LPT1: to the appropriate printer according to the authenticating DC name 
      If EnvVar.Item("LogonServer") = "DC1" then 
      wshShell.Run "net use lpt1: \\DC1\Printer1"
      Else
      wshShell.Run "net use lpt1: \\DC2\Printer2"
      End If
      ' -End

      這個腳本將使用net use命令根據LogonServer變量取值將LPT1:端口連接到適當的打印機上。當負責執行身份驗證的DC為\\DC1時,LPT1:將被映射到printer1上,對于其它DC,LPT1:將被映射到printer2上。只需將包含EnvVar.Item的一行信息替換為您所希望的變量,您便可以通過這個腳本獲取任意一種環境變量(如Computername、TEMP、WinDir等)。舉例來說:如欲讀取TEMP目錄位置,您只需使用EnvVar.Item("TEMP")。 

      注冊表操作 

      注冊表操作是一項常見的工作,系統經常需要在注冊表中保存相關信息并根據需要對其進行讀取。從注冊表中刪除與寫入信息的操作可以通過Wscript.Shell對象所提供的RegWrite和RegDelete方法在.vbs文件中完成。讀取注冊表數據的操作則可通過RegRead方法實現。 

      在訪問注冊表時,您還可以使用WMI。乍看起來,使用WMI似乎不如使用Wscript.Shell對象那樣直觀,然而,這種方式能夠提供更加強大的功能特性與控制能力(例如對各種注冊表鍵及其取值進行列舉的能力)。 

      TechNet腳本中心為您提供了許多用以演示如何通過WSH和WMI對注冊表數據進行訪問與操作的實用腳本示例。如需獲取這些示例,請查看 http://www.microsoft.com/technet/scriptcenter/registry/default.asp。 

      利用WMI讀取操作系統信息 

      在前面列出的示例代碼中,我們看到了如何從環境變量中讀取信息,然而,您所需操作并使用的許多信息位于其它存儲機制或接口中。您可以通過WMI來獲取您所能想到的各種系統信息,這些信息包括磁盤與分區情況、事件查看器數據、服務項目、共享資源以及與操作系統環境和應用程序相關的其它任意內容。 

      如需了解更多關于WMI的信息,請查看 http://msdn.microsoft.com/library/en-us/dnclinic/html/scripting06112002.asp。 

      以下示例腳本將顯示從其所在計算機上獲取的各種不同信息。此腳本應在運行Windows 2000或更高版本操作系統的系統上執行: 

      ' –Start
      ' Using WMI to read Win32_OperatingSystem information
      For Each os in GetObject("winmgmts:").InstancesOf("Win32_OperatingSystem")
      ' Display the OS information in "chunks": each VBCRLF line 
      ' means go down one line to the next one, and each WScript.Echo statement means 
      ' display a new message box.
      WScript.Echo "Version Info:" & VBCRLF & _
      "============" & VBCRLF & _
      " Version: ", os.Caption, os.Version & VBCRLF & _
      " Build: ", os.BuildNumber, os.BuildType & VBCRLF & _
      " CSD Version: ", os.CSDVersion & VBCRLF & _
      " Serial Number: ", os.SerialNumber & VBCRLF & _
      " Manufacturer: ", os.Manufacturer

      WScript.Echo "Memory Info:" & VBCRLF & _
      "===========" & VBCRLF & _
      " Free Physical Memory: ", os.FreePhysicalMemory & VBCRLF & _
      " Free Space in Paging Files: ", os.FreeSpaceInPagingFiles & VBCRLF & _
      " Size Stored in Paging Files: ", os.SizeStoredInPagingFiles & VBCRLF & _
      " Free Virtual Memory: ", os.FreeVirtualMemory & VBCRLF & _
      " Total Virtual Memory Size: ", os.TotalVirtualMemorySize & VBCRLF & _
      " Total Visible Memory Size", os.TotalVisibleMemorySize

      WScript.Echo "Time Info:" & VBCRLF & _
      "=========" & VBCRLF & _
      " Current Time Zone: ", os.CurrentTimeZone & VBCRLF & _
      " Install Date: ", os.InstallDate & VBCRLF & _
      " Last Bootup Time: ", os.LastBootUpTime & VBCRLF & _
      " Local Date & Time: ", os.LocalDateTime

      WScript.Echo "Process Info:" & VBCRLF & _
      "============" & VBCRLF & _
      " Foreground App Boost: ", os.ForegroundApplicationBoost & VBCRLF & _
      " Maximum #Processes: ", os.MaxNumberOfProcesses & VBCRLF & _
      " Maximum Memory Size for Processes: ", os.MaxProcessMemorySize & VBCRLF & _
      " #Processes: ", os.NumberOfProcesses

      WScript.Echo "User Info:" & VBCRLF & _
      "=========" & VBCRLF & _
      "#Users: ", os.NumberOfUsers & VBCRLF & _
      "Registered User: ", os.RegisteredUser

      WScript.Echo "Locale Info:" & VBCRLF & _
      "===========" & VBCRLF & _
      "Code Set: ", os.CodeSet & VBCRLF & _
      "Country Code: ", os.CountryCode & VBCRLF & _
      "Locale: ", os.Locale

      WScript.Echo "System Info:" & VBCRLF & _
      "===========" & VBCRLF & _
      "Boot Device: ", os.BootDevice & VBCRLF & _
      "Name: ", os.CSName & VBCRLF & _
      "Status: ", os.Status & VBCRLF & _
      "System Device: ", os.SystemDevice & VBCRLF & _
      "System Directory: ", os.SystemDirectory & VBCRLF & _
      "Windows Directory: ", os.WindowsDirectory
      Next
      ' –End

      針對多個對象設置相關信息 

      最具利用價值的腳本操作之一是針對同一域中的多個對象設置相關信息。讓我們來考慮一下下面這個示例:當我們在NT中打開用戶管理器時,我們可以輕松提取一份用戶列表并統一對其屬性進行編輯,F在,與NT環境相比,Active Directory提供的許多改進功能,其在各個方面均遠遠勝過原有的管理工具。盡管如此,上述這種簡單操作卻需要由許多管理員來協同完成。令人振奮的消息是,多項選擇與編輯功能已經被添加到Windows XP/.NET Active Directory用戶與計算機MMC嵌入式單元中。下面,讓我們來看一看如何通過腳本方式完成這項操作。在以下示例中,我們將通過一個腳本為Windows 2000 Active Directory域中某一特定組織單元內的多個用戶復制主目錄和主驅動器盤符。 

      這個示例在整個腳本范圍內使用了ADSI。如需了解更多關于ADSI的信息,請查看 http://msdn.microsoft.com/library/en-us/netdir/adsi/adsi_scripting_tutorial.asp。 

      以下為示例腳本代碼: 

      ' -Start
      ' Setting Home Directory for multiple users in a specific OU
      Dim oContainer
      ' Bind to the destination Organizational Unit or container (modify this line according to 
      ' your own domain tree information)
      Set oContainer=GetObject("LDAP://OU=MyUsers,DC=yosdom,DC=com")
      ' Running the ModifyUsers Subroutine on the container 
      ModifyUsers(oContainer)
      ' Clean up
      Set oContainer = Nothing
      ' Display a message box upon operation complete
      MsgBox "Finished :O)"
      ' Close 
      WScript.Quit

      Sub ModifyUsers(oTopLevelContainer) ' oTopLevelContainer is the oContainer object
      Dim oObj
      ' Go into a loop for every object in the container
      For Each oObj in oTopLevelContainer
      ' We use "Select Case" to apply different code/actions to different values of an item. In 
      ' this case, we check every object's Class to apply the actions according to the Object's 
      ' class, e.g. If it's a user object, it goes into the 'Case "user"' section.
      Select Case oObj.Class
      ' if it's a user object, then set it's home directory and 
      ' home drive properties (modify the Server name and home drive 
      ' letter according to your needs.)
      Case "user"
      oObj.Put "HomeDirectory", "\\server1\users\" + oUser.SamAccountName
      oObj.Put "HomeDrive", "m:"
      ' Save changes to the user object
      oObj.Setinfo
      ' If it's a container/OU, go into the ModifyUsers loop for every object there
      Case "organizationalUnit" , "container"
      ModifyUsers(oObj)
      End select
      ' Goes into the next available child object under the container
      Next
      End Sub
      ' –End

      通過執行上述腳本,yosdom.com域中名為MyUsers的組織單元內的所有用戶都將被設置相應的主目錄與主驅動器盤符。其中,主驅動器為M:,主目錄則取決于用戶登錄名稱,例如\\Server1\users\YossiS。 

      在仔細閱讀這個腳本時,請注意,ModifyUsers子程序通過一個Select Case循環語句依次檢查對象類別并根據情況執行適當的操作。舉例來說,如果在MyUsers之下發現一個更低級別的容器或另一個OU,那么,它將進入到這個容器/OU中,并針對這一級別內的所有用戶執行相同的腳本代碼。如果當前元素是一個對象,那么,它將根據上面描述的方式為其設置適當的信息。您可以對下面這行信息進行修改并觀察腳本執行過程中會出現何種變化: 

      Set oContainer=GetObject("LDAP://DC=yosdom,DC=com") 

      修改后的腳本將在整個域樹上運行并為發現的所有用戶設置適當的主驅動器與主目錄。 

      您還可以在如何利用ADSI為域中所有用戶創建用戶共享資源(234746)一文中找到一個與此類似且更加高級的腳本示例,這個腳本示例將通過同種類型的子程序自動創建用戶共享資源。 

      向日志文件中追加信息 

      在運行一個較長的腳本程序時,維護一個文本日志文件對于后期分析非常重要,它不僅能夠幫助您了解腳本運行方式,同時還是一種用以確定腳本故障位置的調試機制。您可以使用Scripting.FileSystemObject對象創建自定義日志文件并向其中添加信息。 

      如需添加、移動、修改、創建、刪除文件夾(目錄)或文件,您可以使用FileSystemObject(FSO)對象模型,以下鏈接所對應的頁面對其進行了詳細解釋 http://msdn.microsoft.com/library/en-us/script56/html/FSOoriFileSystemObject.asp。 

      舉例來說,讓我們繼續使用前面那個讀取環境變量并執行‘net use’命令的腳本,并向其中加入用以記錄日志信息的子程序。(說明:如需對這個腳本進行測試,請根據您所處域環境中的服務器配置情況對腳本中的DC名稱進行修改): 

      ' –Start
      Dim wshShell
      ' Create a new Windows Scripting Host Shell object
      Set wshShell = CreateObject("Wscript.Shell")
      ' Set it to read the environment variables
      Set EnvVar = wshShell.Environment("PROCESS")
      ' Log/Append an entry into a local text file stating which DC authenticated the user
      ' Note: Calling 'Now' outputs the exact time/date on this machine in real-time
      Add2Log Now & ": User was authenticated by DC " & EnvVar.Item("LogonServer")
      ' Now back to the original script action: Re-direct LPT1 to the appropriate printer 
      ' according to the authenticating DC name 
      If EnvVar.Item("LogonServer") = "DC1" then 
      ' Map LPT1 to the printer \\DC1\Printer1 If the DC name is DC1
      wshShell.Run "net use lpt1: \\DC1\Printer1"
      Else
      ' If the DC name is not DC1, then Map LPT1 to the printer \\DC2\Printer2 
      wshShell.Run "net use lpt1: \\DC2\Printer2"
      End If
      ' Log/Append another entry into the local text file stating the script is completed
      Add2Log Now & ": Script completed running."
      ' Close
      wscript.quit

      ' This is the Log Appending Subroutine
      Sub Add2Log (txt) ' txt is the text we send into the subroutine
      ' Declare the log file name
      Const Myfile = "C:\MyLog.txt" ' Log file name
      ' Open it for Append
      Const ForAppending = 8 ' Append mode
      ' Declare the FileSystemObject and File variables
      Dim fso, file
      ' Create a new FileSystemObject object 
      Set fso = CreateObject("Scripting.FileSystemObject")
      ' Open the file and force creation, if it doesn't exist already
      Set file = fso.OpenTextFile(MyFile, ForAppending, TRUE)
      file.WriteLine (txt) ' append log
      ' Clean up
      Set file = Nothing
      End Sub
      ' –End


      前往C:\目錄下并打開MyLog.txt文件。您將看到類似于以下記錄的信息: 

      7/16/2002 6:43:35 PM: User was authenticated by DC \\DCName 

      7/16/2002 6:43:37 PM: Script completed running. 

      這個腳本程序由兩個主要部分組成:‘日志追加’子程序(Add2Log子程序)和開頭幾行用以觸發日志追加子程序的腳本代碼。當您在腳本中運行一行以‘Add2Log’開頭并在空格(“ ”)后緊跟文本信息的代碼時,這些文本信息將被傳入到Add2Log子程序中。該子程序將把您所指定的文本信息追加到文本文件(此種情況下為c:\MyLog.txt)的下一行中。 

      如果希望獲得更多示例腳本,我建議您訪問相關鏈接(特別是TechNet腳本中心)并查找那里所提供的示例腳本。祝您早日享受腳本編程所帶來的便利! 


      日韩精品一区二区三区高清