Managing Windows with VBscript and WMI

Paperback
from $0.00

Author: Don Jones

ISBN-10: 0321213343

ISBN-13: 9780321213341

Category: Windows / Windows 95 & 98

"Finally, a step-by-step VBScripting book to make you look like a programmer without the time and sweat! Don't waste your time searching the Internet for examples--this book does it for you!"\ --Greg A. Marino, Senior Systems Engineer/Consultant, Westtown Consulting Group, Inc.\ Visual Basic Scripting (VBScript) and Windows Management Instrumentation (WMI) are vital tools for systems administrators grappling with the increasing complexity of Windows technologies. However, busy admins have...

Search in google:

"Finally, a step-by-step VBScripting book to make you look like a programmer without the time and sweat! Don't waste your time searching the Internet for examples—this book does it for you!" —Greg A. Marino, Senior Systems Engineer/Consultant, Westtown Consulting Group, Inc.Visual Basic Scripting (VBScript) and Windows Management Instrumentation (WMI) are vital tools for systems administrators grappling with the increasing complexity of Windows technologies. However, busy admins have been without a straightforward guide to scripting...until now.Managing Windows® with VBScript and WMI explains how Windows administrators can effectively use VBScript to automate common administrative tasks and simplify complex ones. Detailed coverage of security concerns provides admins with the means for safely using VBScript in Windows environments. The book is organized around the problems you face daily, with reusable examples and coverage of Windows NT, Windows 2000, Windows XP, and Windows 2003.This user-friendly reference demystifies scripting and then shows you how to produce new scripts from scratch. You will be producing useful scripts right away as you study the VBScript language and learn how to control nearly every aspect of the Windows operating system with WMI and the Active Directory Services Interface (ASDI). You will be able to build your own administrative Web pages and use advanced scripting technologies such as script encryption, scripting components, and script security. The book closes with still more ready-made example scripts accompanied by complete line-by-line explanations. The CD includes all the code from the book and trial versions of PrimalScript3.0 and VbsEdit. A companion Web site provides updates and errata.Inside you will find answers to such questions as: How do you write effective logon scripts? Chapter 11 How do you write scripts that query and modify user and group information? Chapter 16 How can you query the IP addresses from multiple network adapters in multiple remote computers? Chapter 19 How can you design, write, run, test, and debug your own administrative Web pages? Chapter 24 How can you reuse code between various scripts? Chapter 25

Microsoft introduced Visual Basic, Scripting Edition—commonly known as VBScript—in the mid-'90s, positioning it as a native replacement for Windows' aging command-line batch language, which was based on Microsoft's earliest operating system, MS-DOS. VBScript was intended to be easy to learn, powerful, and flexible. The language was included as an add-on to Windows 95 and Windows NT 4.0, was an optional installation component included in Windows 98, and was included in all editions of Windows Me, Windows 2000, Windows XP, and Windows Server 2003.\ Software developers immediately seized upon VBScript for Web programming, particularly in Active Server Pages, Microsoft's rapid-development programming framework for the Web. However, Windows administrators—one of VBScript's initial target audiences—were left cold. VBScript seemed to be much more complicated than administrators' beloved MS-DOS-based batch language, and many didn't see the need to learn an entirely new batch language.\ When Windows 2000 and Active Directory came along, however, administrators found that Windows administration had become a great deal more complex. Suddenly, administrators were searching for Resource Kit and other utilities that offered automated administration, especially for repetitive tasks. Active Directory enabled the use of VBScript for logon and logoff scripts, which seemed to promise more advanced use environment manipulation. At around the same time, Microsoft's naivete in releasing a powerful language like VBScript with absolutely no security controls resulted in a huge wave of high-impact VBScript-based viruses, forcing administrators to lock down their environments andremove VBScript as an option both for viruses and for administrative tools.\ As a regular speaker at some of the country's top technical conferences that focus on Windows technologies, including MCP TechMentor, the past few years I've given half- and full-day sessions on VBScripting for Windows administrators, and the sessions have been incredibly popular. In these sessions, I try to provide just enough VBScript experience to make scripting possible, and then concentrate on accomplishing common administrative tasks with VBScript. I also cover the security concerns of VBScript and provide administrators with the means for safely using VBScript in their environments. This book is essentially a written form of those sessions, greatly expanded with more coverage of Windows Management Instrumentation and other advanced topics, and with more coverage of VBScript security issues and resolutions.\ I'm not out to turn you into a programmer. In fact, one of the real successes of VBScript is that you don't need to be a programmer to use it. Most of what you'll be doing in this book involves using VBScript to tell Windows to do things for you; you'll be able to ignore much of VBScript's complexity, using it as a sort of electronic glue to combine various operating system functions.Who Should Read This Book?\ The only assumption I have about you is that you already know how to administer some version of Microsoft Windows. You'll find that most of the material in this book is suitable for Windows NT, Windows 2000, and Windows Server 2003 environments, and it will continue to be useful through future versions of Windows. I do not assume that you have any background in programming, and I'm not going to give you a programming background.\ You should have a desire to learn how to use what I call "the batch language of the twenty-first century" and a wish to move away from clumsier—and often more complex—batch files based on the MS-DOS batch language. Although some folks like to refer to batch files as scripts, I don't; and when you see how easy and flexible VBScript is, you'll understand why!How to Use This Book\ You can read this book in order from the Introduction to the Appendix. However, if you already have some experience with VBScript, or if you just want to dive right into the more complete example scripts, you can skip around as much as you like. I've organized this book in the same way that I organize my live VBScripting sessions at conferences, so you may feel that it's some time before you really get into the meat of scripting. I assure you, though, that each example in this book—starting in Chapter 1—is focused on Windows administration. You'll get your feet wet right away!\ I've also included In This Chapter elements at the start of each chapter and Coming Up elements at the end of each chapter. These are brief paragraphs that are intended to help set the stage and help you decide if you need to read a particular chapter or not. They'll also help you decide which chapter to read next based on your individual needs and interests. I hope that these elements—along with the cross-references I've included in each chapter—will help you zip straight to the scripting information that you need most.\ To help you decide where to start, here's a brief overview of each chapter.Part I: Introduction to Windows Administrative Scripting\ Part I serves as an introduction to the world of scripting and provides you with a methodology for approaching administrative tasks from a scripting standpoint. One of the most difficult parts about producing new scripts from scratch is the "Where do I start?" factor, and I'll provide you with a framework for figuring that out every time.Chapter 1: Scripting Concepts and Terminology\ As I've already implied, administrative scripting isn't hard-core programming. Instead, it's using VBScript as a sort of electronic glue to secure various bits of the Windows operating system together. In this chapter, I'll introduce you to those various bits and set the stage with some basic terminology that you'll use throughout this book.Chapter 2: Running Scripts\ Writing a script isn't much fun if you can't run the script, and so this chapter will focus on the technologies used to execute scripts. You might be surprised to learn how many different Microsoft products support scripting. In this chapter, I'll show you how far your scripting skills can really take you. I'll also introduce you to some scripting tools that can make writing and debugging scripts a bit easier.Chapter 3: The Components of a Script\ In this chapter, I'll present a complete administrative script, and then break it down line-by-line to explain its various components. Although this chapter isn't necessary to learning administrative scripting, it will help you write scripts that are more reliable and easier to troubleshoot. Chapter 4: Designing a Script\ As I've mentioned already, one of the toughest aspects about scripting can be figuring out where to start. In this chapter, I'll provide you with a framework that you can use as a starting point for every new scripting project. I'll also introduce you to some concepts that many scripting books ignore, such as planning for errors and creating a useful "resource kit" of script components that you can reuse throughout your scripting projects.Part II: VBScript Tutorial\ Here's your official crash course to the VBScript language: just enough to make administration via script a possibility! The best part is that I won't use the trite "Hello, world" examples that books for software developers often start out with. Instead, I'll make every example useful to you as a Windows administrator. That means you'll be producing simple, useful scripts at the same time you're learning VBScript. What could be better?Chapter 5: Functions, Objects, Variables, and More\ In this chapter, I'll show you the basic building blocks of any script and introduce you to some sample scripts that use each building block in a particular administrative task. This is really the meat of administrative scripting, and you'll be able to write useful scripts when you're finished with this chapter.Chapter 6: Input and Output\ You can make your scripts more flexible by adding the ability to dynamically change computer, user, and domain names, along with other information. In this chapter, I'll show you how your script can collect information it needs to run and dynamically alter itself to take advantage of that information.Chapter 7: Manipulating Numbers\ This chapter will explain how scripts can manipulate numbers, making it easier to create scripts that work with numeric data, such as user account data. I'll also introduce you to VBScript's numeric data handling and conversion commands, putting you on the path to some great scripting techniques. Chapter 8: Manipulating Strings\ Strings—a fancy word for text data—are at the heart of most scripting tasks. In this chapter, I'll show you how VBScript deals with strings and how you can easily integrate them into your scripts.Chapter 9: Manipulating Other Types of Data\ Aside from text and numbers, your scripts may need to deal with dates, times, bytes, and other forms of data to accomplish specific administrative tasks. In this chapter, I'll show you how VBScript handles these other data types and how you can use them in your own scripts.Chapter 10: Controlling the Flow of Execution\ The best administrative scripts can respond to changing conditions with internal logic, called control-of-flow. In this chapter, I'll show you how your scripts can be made to evaluate various conditions and respond accordingly, perform repetitive tasks, and much more.Chapter 11: Built-in Scripting Objects\ Much of VBScript's power comes from its capability to join various operating system objects, and in this chapter, I'll introduce you to your first set of those objects. You'll learn how to manipulate network information, map drives, and much more—pretty much everything you need to write effective logon scripts.Chapter 12: Working with the File System\ A common use of scripting is to manipulate files and folders, and in this chapter, I'll introduce you to the VBScript FileSystemObject, which provides a complete object model for working with the file system. You'll learn to build a utility that scans IIS log files for error messages, a useful script for any environment!Chapter 13: Putting It All Together: Your First Script\ This is where you'll put everything from Part II together. You'll be creating a script that rotates IIS log files, keeping the past 30 days' worth of files in a special archive folder. I'll guide you through the complete process of designing, writing, testing, and troubleshooting the script. In fact, I'll deliberately introduce some logic errors into the script so that you can see the debugging process in action.Part III: Windows Management Instrumentation and Active Directory Services Interface\ With the glue of VBScript under your belt, I'll dive into the two most powerful technologies for administering Windows: Windows Management Instrumentation (WMI) and the Active Directory Services Interface (ADSI). These technologies provide administrative access to, and control over, nearly every aspect of the Windows operating system, from Windows NT to Windows Server 2003. Chapter 14: Working with ADSI Providers\ Despite its name, ADSI isn't just for Active Directory. In this chapter, I'll show you how ADSI can be used to interface with NT, Active Directory, Novell NDS, Exchange Server, and other types of directory services. I'll provide some basic examples of the types of tasks you can perform with ADSI to get you started.Chapter 15: Manipulating Domains\ With the ADSI basics out of the way, I'll focus on manipulating domain information in a script. You'll learn how to query domain information, modify domain policies like password length, and much more. Chapter 16: Manipulating Users and Groups\ In this chapter, you'll learn how to write scripts that query and modify user and group information. This is one of the most common tasks you'll perform with VBScript, and I'll include plenty of useful examples. Chapter 17: Understanding WMI\ WMI provides a hook into just about every portion of the Windows operating system, making it an incredibly useful tool for administrative scripts. In this chapter, I'll introduce you to WMI and show you a preview of what you can use it for in your environment. Chapter 18: Querying Basic WMI Information\ Do you want to find out which users in your organization have a Pentium 4 computer? This chapter will show you how to write your own basic WMI queries, including those that involve remote machines. You'll also learn basic WMI manipulation, which lets you modify local and remote machine settings from within a script.Chapter 19: Querying Complex WMI Information\ Some WMI queries are more complex, such as querying the IP addresses from multiple network adapters in multiple remote computers. This chapter provides clear examples of these more complex WMI tasks, helping you learn to write enterprise management scripts.Chapter 20: Putting It All Together: Your First WMI/ADSI Script\ This is where it all comes together. I'll walk you through the process of designing, writing, testing, and debugging a complete WMI/ADSI script from scratch. You'll finish this chapter with a concrete example of the administrative capabilities of these technologies, and then you'll be ready to start writing your own scripts.Part IV: Creating Administrative Web Pages\ One popular use of Web technologies inside corporate networks is to provide user self-service pages, such as a simple Web page where users can reset their own passwords, if necessary. In Part IV, I'll give you a crash course on IIS and Active Server Pages (ASP) and show you how to start building your own administrative Web pages.Chapter 21: Active Server Pages Crash Course\ ASP is a great way to create effective administrative Web pages. I'm not going to try to make you a Web developer or an HTML expert; the Web pages you'll create in this chapter won't be pretty, but they'll be incredibly effective. Chapter 22: Adding Administrative Script to a Web Page\ After you have a basic Web page ready, you can start adding script to it to make the Web page perform administrative tasks. I'll provide examples of real-world tasks that you can start using in your environment right away.Chapter 23: Web Page Security Overview\ Web pages have special security concerns, which I'll focus on in this chapter. I'll explain how IIS 5.0 and 6.0 process Web pages, what you can and cannot easily accomplish from within a Web page, and how you can create a secure, stable environment for Web-based administrative scripts.Chapter 24: Putting It All Together: Your First Administrative Web Page\ You're now ready to design, write, run, test, and debug your own administrative Web pages, and in this chapter, I'll step you through the entire progression. When you're finished, you'll not only have a ready-to-run administrative Web page to use, you'll also be ready to start creating your own Web pages from scratch. Again, they might not be pretty, but they'll be incredibly functional and useful.Part V: Advanced Scripting Techniques\ As you become a more experienced scripter, you'll be ready to start saving time and be more secure, with advanced techniques like script encryption, scripting components, script security, and so forth. In this part of the book, I'll give you a comprehensive look at each of these technologies and show you how to put them into use in your own environment.Chapter 25: Modular Script Programming\ If you find yourself cutting and pasting code—or worse, retyping it—this is the chapter for you. I'll introduce you to modular scripting concepts, which make it easier to reuse code between various scripts, saving you time and effort! By way of example, we'll start with a complex script that contains lots of useful code, and then break it down into easily reused modules. Chapter 26: Using Script Components\ Windows Script Components are the easy way to create your own fully encapsulated chunks of script, each of which performs a useful task. Building on the examples in Chapter 25, I'll show you how to create and use script components to make administrative scripting faster and easier. You'll come away with a whole library of script components that you can start using immediately!Chapter 27: Encoded Scripts\ Are you worried that others will peek into your scripts and steal your ideas? Scripting encryption helps protect your scripts from both Peeping Toms and potential misuse, so I'll show you how to set up, deploy, and use script encryption within your environment in this chapter.Chapter 28: Scripting Security\ Some folks think Microsoft made a huge mistake when it included VBScript in the Windows operating system, but I disagree. Properly configured, scripting can be as safe as any other type of application. In this chapter, I'll explain scripting security concepts and introduce you to the tools that can make scripting a safe and valuable part of any computing environment.Part VI: Ready-to-Run Examples\ I figured a great way to wrap up the book would be a whole section on ready-made example scripts that you can start using in your own environment. Additionally, these scripts—like every other script in this book—will have complete, line-by-line explanations, making them a perfect reference guide as you start to create your own scripts from scratch.Chapter 29: Logon and Logoff Scripts\ I'll use this chapter to present more complex logon and logoff scripts and to give you some ideas for how scripting can make these important scripts more effective. Of course, the line-by-line explanations will make each script a useful reference for customizing your own scripts. Chapter 30: Windows and Domain Administration Scripts\ Automating domain administration is probably one of the big reasons you started looking at scripting in the first place, so I'll use this chapter to present a number of examples of tasks that scripts can perform. The detailed explanations with each script will help you rip them apart and customize them for your own use.Chapter 31: Network Administration Scripts\ Network administration is ideally suited for scripting, and in this chapter, I'll provide a handful of examples that will show you what's possible. The line-by-line explanations will make it easy to put these into use in your own environment.Chapter 32: WMI and ADSI Scripts\ These can be the toughest scripts to write due to the complexity and flexibility of WMI and ADSI. In this chapter, I'll provide you with several ready-to-use scripts for common tasks like querying WMI, creating users and groups, and more. These scripts can be easily modified and incorporated into your own scripts, saving you scripting time!Part VII: AppendixAppendix: Administrator's Quick Script Reference\ One of the toughest parts about VBScript is that it contains so much functionality. It's usually pretty easy to figure out what you want a script to do; the tough part is often figuring out how to make VBScript do it! In this appendix, I'll provide you with an alphabetical list of common tasks and give the VBScript commands that perform each task. You can use this reference along with the VBScript documentation to make designing and writing scripts much easier.Preparing to Use This Book\ Before you dive in, you should make sure that your computers are ready for VBScript. Fortunately, any computer with Windows 2000 or later is ready to go out of the box, and I'll assume that you're doing your development work on either a Windows 2000-, Windows XP-, or Windows Server 2003-based computer. Typographical Elements\ Books on programming can benefit a great deal from easy-to-understand typestyles and elements like the ones I'll explain here. These typestyles and elements are designed to make the text easier to follow and to call your attention to special concerns.\ Boldfaced type will be used to set off material that should be typed into the computer; boldfaced also will be used to signify onscreen menu and/or dialog box selections. For example, "select Run from the Start menu, type wbemtest, and click OK" sets off the menu selection, what you need to type onscreen, and what should be clicked from the dialog box. Blocks of code and code lines that appear within the text will appear in a monospaced font, as in, "to change the contents of a variable, you can use something like\ Var1 = Trim(Var1)."\ TIP Tips will provide shortcuts and other "insider advice" about scripting that you'll find valuable.\ NOTE Notes will provide cautions and other clarifications that will help you avoid problems or further clarify complex concepts.\ I'll also direct you to material that more thoroughly explains particular concepts, VBScript commands, and so forth. Although I'm not a big fan of flipping back and forth through a book, these cross-references will allow you to remain focused within each chapter and will guide you to more detailed explanations, when appropriate.\ Finally, there are times when it is necessary to present an extended explanation of something that isn't critical to the task at hand. In those cases, I'll use a sidebar. A sidebar is a cue that the information is useful, but it's not really key to the main text; you're welcome to skip the sidebar and come back to it later if you like.\ Sidebars\ Sidebars make it easier to cover slightly off-topic information without distracting you from the main text.>>Sample Scripts\ Obviously, a book on scripting is going to have many code listings. To make these as useful as possible, I introduce each example script with a special head style, and present it in a listing by itself with no comments.\ Listing P.1 A Sample Script. Accepts a name, and then redisplays that name.\ 'Get the user's name\ sName = InputBox("What is your name?")\ \ 'Display the user's name\ MsgBox "Your name is " & sName\ \ After I present each script, I'll briefly review any changes you might need to make to get the script running in your environment, such as changing computer or domain names. You'll find each complete script included on the CD that accompanies this book. I've created a separate folder for each chapter and named the script files by their listing number for easy reference.>>Sample Scripts—Explained\ For each script in this book, I'll include a line-by-line explanation of the script, so that you understand exactly what's going on. For example:\ First, the sample script will display a dialog box where the user can type his name. By default, this dialog box includes an OK and Cancel button; I'm not providing any way to detect the Cancel button in this script, so I'm assuming the user will type something and click OK.\ 'Get the user's name\ sName = InputBox("What is your name?")\ Finally, the script uses the MsgBox statement to redisplay the user's name. Notice the use of the ampersand operator (&) to tack on the contents of the variable sName, which stores whatever the user typed into the input box.\ 'Display the user's name\ MsgBox "Your name is " & sName\ \ Walk-throughs like this one will help you become more familiar with VBScript, what each command does, and exactly how each example script works.

PrefaceAcknowledgmentsAbout the AuthorPt. IIntroduction to Windows Administrative Scripting1Ch. 1Scripting Concepts and Terminology3Ch. 2Running Scripts11Ch. 3The Components of a Script29Ch. 4Designing a Script47Pt. IIVBScript Tutorial69Ch. 5Functions, Objects, Variables, and More71Ch. 6Input and Output99Ch. 7Manipulating Numbers117Ch. 8Manipulating Strings133Ch. 9Manipulating Other Types of Data147Ch. 10Controlling the Flow of Execution157Ch. 11Built-in Scripting Objects175Ch. 12Working with the File System193Ch. 13Putting It All Together: Your First Script223Pt. IIIWindows Management Instrumentation and Active Directory Services Interface253Ch. 14Working with ADSI Providers255Ch. 15Manipulating Domains265Ch. 16Manipulating Users and Groups281Ch. 17Understanding WMI293Ch. 18Querying Basic WMI Information311Ch. 19Querying Complex WMI Information331Ch. 20Putting It All Together: Your First WMI/ADSI Script351Pt. IVCreating Administrative Web Pages375Ch. 21Active Server Pages Crash Course377Ch. 22Adding Administrative Script to a Web Page393Ch. 23Web Page Security Overview419Ch. 24Putting It All Together: Your First Administrative Web Pages433Pt. VAdvanced Scripting Techniques461Ch. 25Modular Script Programming463Ch. 26Using Script Components475Ch. 27Encoded Scripts481Ch. 28Scripting Security487Pt. VIReady-to-Run Examples497Ch. 29Logon and Logoff Scripts499Ch. 30Windows and Domain Administration Scripts517Ch. 31Network Administration Scripts541Ch. 32WMI and ADSI Scripts567Pt. VIIAppendix577AppAdministrator's Quick Script Reference579Index591