vbscript_VBScript,系统管理员的语言

vbscript

Real Visual Basic veterans might remember how to code clever little DOS batch programs that would automate your PC. Before Windows (Can anybody remember that now?) there were whole books written about DOS batch files because they were simple and anyone could whip out one of these little text files with Edit. (Edit is what programmers used before NotePad and it's still available if you want to try it. Just enter "Edit" at a DOS command prompt.)

真正的Visual Basic资深人士可能还记得如何编写聪明的DOS批处理程序 ,这些程序可以使您的PC自动化。 在Windows之前(有人还能记得吗?),有整本关于DOS批处理文件的书,因为它们很简单,任何人都可以使用Edit剔除这些小的文本文件之一。 (编辑是程序员在NotePad之前使用的工具,如果您想尝试的话,它仍然可用。只需在DOS命令提示符下输入“编辑”。)

You weren’t any kind of techie unless you had written your own batch file to start your favorite programs from a DOS menu. "Automenu" was one of those kitchen table startup companies back then. Knowing that we could get excited over - "Gee Whiz" - the ability to start programs from a menu should help you understand why Windows was so revolutionary.

除非您编写了自己的批处理文件以从DOS菜单启动喜欢的程序,否则您不是任何技术人员。 “ Automenu”是当时的厨房餐桌创业公司之一。 知道我们可能会为“ Gee Whiz”而感到兴奋-从菜单启动程序的能力应该可以帮助您了解Windows为什么如此革命性。

But in fact, the early versions of Windows took a step backward precisely because they didn’t give us a "Windows" way to create this type of desktop automation. We still had batch files - if we were willing to ignore Windows. But if we wanted to use Windows, the joy of writing a simple piece of code that made your computer more personal just wasn't there.

但是实际上,Windows的早期版本倒退了一步,正是因为它们没有给我们提供创建此类桌面自动化的“ Windows”方式。 我们仍然有批处理文件-如果我们愿意忽略Windows。 但是,如果我们要使用Windows,那么编写简单代码使您的计算机更具个性的乐趣就不存在了。

All that changed when Microsoft released WSH – Windows Script Host. It’s a lot more than just a way to write simple programs. This short tutorial will show you how to use WSH, and we'll dig into how WSH is much, much more than DOS batch files ever dreamed of being by showing how to use WSH for hard-core computer administration.

当Microsoft发布WSH – Windows Script Host时,一切都改变了。 它不仅仅是编写简单程序的一种方法。 这个简短的教程将向您展示如何使用WSH,并且通过展示如何使用WSH进行核心计算机管理,我们将探究WSH的数量远远超过DOS批处理文件曾经梦ever以求的东西。

VBScript“主机” ( VBScript "Hosts" )

Internet Explorer 9 icon

Microsoft/Wikimedia Commons/Public Domain

Microsoft / Wikimedia Commons /公共领域

If you're just learning about VBScript, it can be kind of confusing to figure out where it "fits in" in the Microsoft world. For one thing, Microsoft currently offers three different 'host' for VBScript.

如果您只是在学习VBScript,那么弄清楚它在Microsoft世界中的“位置”可能会让人感到困惑。 一方面,Microsoft当前为VBScript提供了三种不同的“主机”。

  • Internet Explorer (IE)

    Internet Explorer(IE)
  • Internet Information Server (IIS)

    Internet信息服务器(IIS)
  • Windows Script Host (WSH)

    Windows脚本主机(WSH)

Since VBScript is interpreted, there must be another program that provides the interpretation service for it. With VBScript, this program is called the 'host'. So, technically, VBScript is three different languages because what it can do depends entirely on what the host supports. (Microsoft makes sure that they are virtually identical, however.) WSH is the host for VBScript that works directly in Windows.

由于已解释VBScript,因此必须有另一个程序为其提供解释服务。 使用VBScript,此程序称为“主机”。 因此,从技术上讲,VBScript是三种不同的语言,因为它的作用完全取决于主机支持的内容。 (但是,Microsoft确保它们实际上是相同的。)WSH是直接在Windows中运行的VBScript的主机。

You might be familiar with using VBScript in Internet Explorer. Although nearly all HTML on the web uses Javascript since VBScript is only supported by IE, the use if VBScript in IE is just like Javascript except that instead of using the HTML statement ...

您可能熟悉Internet Explorer中的VBScript。 尽管由于IE仅支持VBScript,所以网络上几乎所有HTML都使用Javascript ,但IE中的VBScript的用法与Javascript相同,只是不使用HTML语句...

SCRIPT language=JavaScript

脚本语言= JavaScript

... you use the statement ...

...您使用语句...

SCRIPT language=VBScript

脚本语言= VBScript

... and then code your program in VBScript. This is only a good idea if you can guarantee that only IE will be used. And the only time you can do this is usually for a corporate system where only one type of browser is allowed.

...,然后用VBScript编写程序。 如果您可以保证仅使用 IE,这只是一个好主意。 通常,只有在只有一种类型的浏览器被允许的公司系统中,您才能执行此操作。

清除一些“混淆点” ( Clearing up Some "Points of Confusion" )

Another point of confusion is that there are three versions of WSH and two implementations. Windows 98 and Windows NT 4 implemented version 1.0. Version 2.0 was released with Windows 2000 and the current version is numbered 5.6.

另一个困惑点是,WSH有三个版本和两个实现。 Windows 98和Windows NT 4实现版本1.0。 Windows 2000发行了2.0版,当前版本号为5.6。

The two implementations are one that works from a DOS command line (called "CScript" for Command Script) and one that works in Windows (called "WScript"). You can use CScript only in a DOS command window, but it's interesting to note that much of the real world computer systems administration still works that way. It might also be confusing to discover that the WScript object is essential to a lot of code that is normally run in CScript. The example shown later uses the WScript object, but you can run it with CScript. Just accept it as maybe being slightly odd, but that's the way it works.

这两种实现是一种可在DOS命令行上运行的命令(对于命令脚本称为“ CScript”),而一种可在Windows中使用的实现(称为“ WScript”)。 您只能在DOS命令窗口中使用CScript,但是有趣的是,现实世界中的许多计算机系统管理仍然可以这种方式工作。 发现WScript对象对于通常在CScript中运行的许多代码必不可少,这也可能令人困惑。 稍后显示的示例使用WScript对象,但是您可以使用CScript运行它。 只是接受它可能有点奇怪,但这就是它的工作方式。

If WSH is installed, you can run a VBScript program by simply double-clicking on any file that has the vbs extension and that file will be executed by WSH. Or, for even more convenience, you can schedule when a script will run with Windows Task Scheduler. In partnership with Task Scheduler, Windows can run WSH and a script automatically. For example, when Windows starts, or every day at a particular time.

如果安装了WSH,则可以通过双击任何具有vbs扩展名的文件来运行VBScript程序,并且该文件将由WSH执行。 或者,为了更加方便,您可以计划使用Windows Task Scheduler运行脚本的时间。 与Task Scheduler合作,Windows可以自动运行WSH和脚本。 例如,Windows启动时或每天的特定时间。

WSH对象 ( WSH Objects )

WSH is even more powerful when you use objects for things like managing a network or updating the registry.

当您将对象用于管理网络或更新注册表之类的事情时,WSH甚至会更加强大。

On the next page, you'll see a short example of a WSH script (adapted from one supplied by Microsoft) that uses WSH to create a desktop shortcut to the Office program, Excel. (There are certainly easier ways to do this - we're doing it this way to demonstrate scripting.) The object this script uses is 'Shell'. This object is useful when you want to run a program locally, manipulate the contents of the registry, create a shortcut, or access a system folder. This particular piece of code simply creates a desktop shortcut to Excel. To modify it for your own use, create a shortcut to some other program you want to run. Note that the script also shows you how to set all the parameters of the desktop shortcut.

在下一页上,您将看到一个WSH脚本的简短示例(改编自Microsoft提供的脚本),该脚本使用WSH为Office程序Excel创建桌面快捷方式。 (当然,有更简单的方法可以执行此操作-我们通过这种方式来演示脚本。)此脚本使用的对象是“ Shell”。 当您要在本地运行程序,操纵注册表的内容,创建快捷方式或访问系统文件夹时,此对象很有用。 这段特殊的代码只是创建了Excel的桌面快捷方式。 要对其进行修改以供自己使用,请创建要运行的其他程序的快捷方式。 请注意,该脚本还向您显示了如何设置桌面快捷方式的所有参数。

示例代码 ( The Example Code )

~~~~~~~~~~~~~~~~~~~~~~~~~set WshShell = WScript.CreateObject("WScript.Shell")strDesktop = WshShell.SpecialFolders("Desktop")set oShellLink = WshShell.CreateShortcut(strDesktop _& "\MyExcel.lnk")oShellLink.TargetPath = _"C:\Program Files\Microsoft Office\OFFICE11\EXCEL.EXE"oShellLink.WindowStyle = 1oShellLink.Hotkey = "CTRL+SHIFT+F"oShellLink.IconLocation = _"C:\Program Files\Microsoft Office\OFFICE11\EXCEL.EXE, 0"oShellLink.Description = "My Excel Shortcut"oShellLink.WorkingDirectory = strDesktopoShellLink.Save~~~~~~~~~~~~~~~~~~~~~~~~~

~~~~~~~~~~~~~~~~~~~~~~~~ set WshShell = WScript.CreateObject(“ WScript.Shell”)strDesktop = WshShell.SpecialFolders(“ Desktop”)set oShellLink = WshShell.CreateShortcut(strDesktop _&“ \ MyExcel.lnk”)oShellLink.TargetPath = _“ C:\ Program Files \ Microsoft Office \ OFFICE11 \ EXCEL.EXE” oShellLink.WindowStyle = 1oShellLink.Hotkey =“ CTRL + SHIFT + F” oShellLink.IconLocation = _“ C:\ Program Files \ Microsoft Office \ OFFICE11 \ EXCEL.EXE,0” oShellLink.Description =“我的Excel快捷方式” oShellLink.WorkingDirectory = strDesktopoShellLink.Save ~~~~~~~~~~~~~ ~~~~~~~~~~~~~~

运行示例...以及下一步 ( Running the Example ... and What's Next )

To try out this script, simply copy and paste it into Notepad. Then save it using any name ... such as "CreateLink.vbs". Remember that Notepad will add ".txt" to files automatically in some cases and the file extension must be ".vbs" instead. Then double click the file. A shortcut should appear on your desktop. If you do it again, it just recreates the shortcut. You can also start the DOS Command Prompt and navigate to the folder that the script was saved in and run it with the command ...

要试用此脚本,只需将其复制并粘贴到记事本中即可。 然后使用任何名称保存它,例如“ CreateLink.vbs”。 请记住,在某些情况下,记事本会在文件中自动添加“ .txt”,文件扩展名必须改为“ .vbs”。 然后双击文件。 快捷方式应出现在桌面上。 如果再次执行此操作,它将重新创建快捷方式。 您也可以启动DOS命令提示符,并导航到保存脚本的文件夹,然后使用命令运行它。

cscript scriptfilename.vbs

cscript scriptfilename.vbs

... where "scriptfilename" is replaced with the name you used to save it. See the example shown in the screenshot above.

...其中“ scriptfilename”被替换为您用来保存它的名称。 请参阅上面的屏幕快照中显示的示例。

Give it a try!

试试看!

One caution: Scripts are used a great deal by viruses to do bad things to your computer. To combat that, your system may have software (such as Norton AntiVirus) that will flash a warning screen when you try to run this script. Just select the option that allows this script to run.

注意事项:病毒大量使用脚本来对计算机造成不良影响。 为此,您的系统可能装有软件(例如Norton AntiVirus),当您尝试运行此脚本时,该软件将闪烁警告屏幕。 只需选择允许该脚本运行的选项即可。

Although using VBScript in this mode is great, the real payoff for most people comes in using it to automate systems like WMI (Windows Management Instrumentation) and ADSI (Active Directory Service Interfaces). 

尽管在此模式下使用VBScript很棒,但对大多数人来说,真正的收益在于使用它来自动化WMI(Windows管理规范)和ADSI(Active Directory服务接口)之类的系统。

翻译自: https://www.thoughtco.com/learn-about-vbscript-3424162

vbscript

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值