文章导航PC6首页软件下载单机游戏安卓资源苹果资源

pc软件新闻网络操作系统办公工具编程服务器软件评测

安卓新闻资讯应用教程刷机教程安卓游戏攻略tv资讯深度阅读综合安卓评测

苹果ios资讯苹果手机越狱备份教程美化教程ios软件教程mac教程

单机游戏角色扮演即时战略动作射击棋牌游戏体育竞技模拟经营其它游戏游戏工具

网游cf活动dnf活动lol周免英雄lol礼包

手游最新动态手游评测手游活动新游预告手游问答

您的位置:首页精文荟萃软件资讯 → Script经典文章

Script经典文章

时间:2004/10/7 18:18:00来源:本站整理作者:蓝点我要评论(0)

开篇:读取环境变量 

让我们以一个在许多环境中都非常实用的示例脚本作为开篇。当您使用登录脚本或批处理文件时,读取环境变量是一项非常普遍的操作。您可以通过诸如%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中打开用户管理器时,我们可以轻松提取一份用户列表并统一对其属性进行编辑。现在,与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脚本中心)并查找那里所提供的示例脚本。祝您早日享受脚本编程所带来的便利! 

相关阅读 Mac访问Windows共享文件夹Windows 7正版系统验证方法windows 8.1系统版本号查看方法Windows 8.1系统电话激活时无法输入微软返回代码解决方法Windows 8如何调整屏幕分辨率windows8.1磁盘占用100%解决方法Mac双系统如何删除Boot Camp安装的Windows分区Apple教你如何在Mac 上运行 Windows

文章评论
发表评论

热门文章 360随身Wifi 4G版什么怎样提高origin下载速百度收购PPS已敲定!3

最新文章 伊森卡特的消失通关流千牛怎么设置自动回复 增加新功能,S版Moto G喜获Android 4.4.4更鸡肋?谷歌Play Music发布更新版本千牛怎么设置自动回复​千牛云盘怎么用

人气排行 xp系统停止服务怎么办?xp系统升级win7系统方office2013安装教程图解:手把手教你安装与同步推是什么?同步推使用方法介绍QQ2012什么时候出 最新版下载VeryCD镜像网站逐个数,电驴资料库全集利用PHP程序设定防止MySQL注入或HTML表单滥web服务器和应用服务器的区别安卓android 系统支持什么视频格式