Tyson Kopczynski

Total Page:16

File Type:pdf, Size:1020Kb

Tyson Kopczynski www.it-ebooks.info Tyson Kopczynski Windows® PowerShell UNLEASHED 800 East 96th Street, Indianapolis, Indiana 46240 USA www.it-ebooks.info Windows® PowerShell Unleashed Editor-in-Chief Copyright © 2007 by Sams Publishing Karen Gettman All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or transmitted by any means, electronic, mechanical, photocopying, recording, Senior Acquisitions or otherwise, without written permission from the publisher. No patent liability is Editor assumed with respect to the use of the information contained herein. Although every Neil Rowe precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions. Nor is any liability assumed for Development Editor damages resulting from the use of the information contained herein. Mark Renfrow International Standard Book Number: 0-672-32953-0 Managing Editor Library of Congress Cataloging-in-Publication Data Gina Kanouse Kopczynski, Tyson. Project Editor Microsoft PowerShell unleashed / Tyson Kopczynski. George E. Nedeff p. cm. ISBN 0-672-32953-0 Copy Editor 1. Microsoft Windows (Computer file) 2. Operating systems (Computers) I. Title. Lisa M. Lord QA76.76.O63K66 2007 Senior Indexer 005.4’46—dc22 Cheryl Lenser 2007008894 Proofreader Printed in the United States of America Water Crest First Printing: Publishing 10090807 4321 Contributing Authors Trademarks Pete Handley, Mark All terms mentioned in this book that are known to be trademarks or service marks Weinhardt, and have been appropriately capitalized. Sams Publishing cannot attest to the accuracy of this information. Use of a term in this book should not be regarded as affecting the Josh Tolle validity of any trademark or service mark. Technical Editor Warning and Disclaimer Pawam Bhardwaj Every effort has been made to make this book as complete and as accurate as possi- ble, but no warranty or fitness is implied. The information provided is on an “as is” Publishing basis. The authors and the publisher shall have neither liability nor responsibility to any Coordinator person or entity with respect to any loss or damages arising from the information Cindy Teeters contained in this book. Bulk Sales Book Designer Sams Publishing offers excellent discounts on this book when ordered in quantity for Gary Adair bulk purchases or special sales. For more information, please contact Page Layout U.S. Corporate and Government Sales Jake McFarland 1-800-382-3419 Nonie Ratcliff [email protected] For sales outside of the U.S., please contact International Sales [email protected] www.it-ebooks.info Contents at a Glance Introduction .................................................................................. 1 Part I Introduction to PowerShell 1 Introduction to Shells and PowerShell ................................................ 7 2 PowerShell Basics .......................................................................... 19 3 PowerShell: A More In-Depth Look .................................................. 57 4 Code Signing................................................................................ 93 5 PowerShell Scripting Best Practices ................................................. 107 Part II Translating Your Existing Knowledge into PowerShell 6 PowerShell and the File System .......................................................125 7 PowerShell and the Registry ...........................................................157 8 PowerShell and WMI.................................................................... 183 9 PowerShell and Active Directory .................................................... 205 Part III Using PowerShell to Meet Your Automation Needs 10 Using PowerShell in the Real-World................................................ 235 11 Using PowerShell to Manage Exchange ........................................... 261 Index ........................................................................................ 295 www.it-ebooks.info Table of Contents Introduction 1 Part I Introduction to PowerShell 1 Introduction to Shells and PowerShell 7 What Is a Shell?...............................................................................7 Basic Shell Use.........................................................................8 Basic Shell Scripts ...................................................................10 A Shell History ..............................................................................14 Enter PowerShell ............................................................................16 Summary ......................................................................................17 2 PowerShell Basics 19 Introduction .................................................................................19 Getting Started ..............................................................................19 Accessing PowerShell ......................................................................21 Understanding the Command-Line Interface (CLI) ..............................22 Navigating the CLI .................................................................24 PowerShell Command Types ....................................................26 Calling PowerShell from Other Shells ........................................30 Understanding cmdlets ...................................................................33 Common Parameters ..............................................................34 Useful cmdlets ...............................................................................35 Get-Help ...............................................................................35 Get-Command.......................................................................38 Expressions ...................................................................................40 Understanding Variables .................................................................41 Built-in Variables ....................................................................42 Understanding Aliases.....................................................................45 Discovering Alias cmdlets ........................................................47 Creating Persistent Aliases........................................................48 Escape Sequences ...........................................................................49 Understanding Scopes.....................................................................50 Global ..................................................................................50 Local ....................................................................................51 Script ...................................................................................51 www.it-ebooks.info Table of Contents v Private..................................................................................52 Your First Script .............................................................................53 Summary ......................................................................................56 3 PowerShell: A More In-Depth Look 57 Introduction .................................................................................57 Object Based .................................................................................57 Understanding the Pipeline......................................................58 .NET Framework Tips ..............................................................60 Understanding Providers .................................................................72 Accessing Drives and Data .......................................................74 Mounting a Drive...................................................................77 Understanding Errors......................................................................78 Error Handling ..............................................................................80 Method One: cmdlet Preferences...............................................80 Method Two: Trapping Errors ...................................................81 Method Three: The Throw Keyword ..........................................83 PowerShell Profiles .........................................................................84 The All Users Profile ...............................................................84 The All Users Host-Specific Profile .............................................84 The Current User’s Profile ........................................................85 The Current User’s Host-Specific Profile......................................85 Understanding Security ...................................................................85 Execution Policies ..................................................................86 Setting the Execution Policy.....................................................88 Additional Security Measures....................................................91 The PowerShell Language ................................................................91 Summary ......................................................................................91 4 Code Signing 93 Introduction .................................................................................93 What Is Code Signing?....................................................................94 Obtaining a Code-Signing Certificate.................................................95 Method One: Self-Signed Certificate ..........................................96
Recommended publications
  • Vbscript Programmer's Reference
    Table of Contents VBScript Programmer's Reference...................................................................................................................1 Introduction.........................................................................................................................................................6 Who is this Book For?............................................................................................................................6 How to Use this Book.............................................................................................................................6 What is VBScript?..................................................................................................................................7 What Can You Do With VBScript?......................................................................................................11 What Tools Do You Need to Use VBScript?.......................................................................................14 What's New in VBScript 5?..................................................................................................................15 Code Conventions.................................................................................................................................17 Tell Us What You Think.......................................................................................................................17 Customer Support.................................................................................................................................18
    [Show full text]
  • Software Product Description and Quickspecs
    VSI OpenVMS Alpha Version 8.4-2L2 Operating System DO-DVASPQ-01A Software Product Description and QuickSpecs PRODUCT NAME: VSI OpenVMS Alpha Version 8.4-2L2 DO-DVASPQ-01A This SPD and QuickSpecs describes the VSI OpenVMS Alpha Performance Release Operating System software, Version 8.4-2L2 (hereafter referred to as VSI OpenVMS Alpha V8.4-2L2). DESCRIPTION OpenVMS is a general purpose, multiuser operating system that runs in both production and development environments. VSI OpenVMS Alpha Version 8.4-2L2 is the latest release of the OpenVMS Alpha computing environment by VMS Software, Inc (VSI). VSI OpenVMS Alpha V8.4-2L2 is compiled to take advantage of architectural features such as byte and word memory reference instructions, and floating-point improvements, which are available only in HPE AlphaServer EV6 or later processors. This optimized release improves performance by taking advantage of faster hardware-based instructions that were previously emulated in software. NOTE: VSI OpenVMS Alpha V8.4-2L2 does not work on, and is not supported on, HPE AlphaServer pre-EV6 systems. OpenVMS Alpha supports HPE’s AlphaServer series computers. OpenVMS software supports industry standards, facilitating application portability and interoperability. OpenVMS provides symmetric multiprocessing (SMP) support for multiprocessing systems. The OpenVMS operating system can be tuned to perform well in a wide variety of environments. This includes combinations of compute-intensive, I/O-intensive, client/server, real-time, and other environments. Actual system performance depends on the type of computer, available physical memory, and the number and type of active disk and tape drives. The OpenVMS operating system has well-integrated networking, distributed computing, client/server, windowing, multi-processing, and authentication capabilities.
    [Show full text]
  • Openvms: an Introduction
    The Operating System Handbook or, Fake Your Way Through Minis and Mainframes by Bob DuCharme VMS Table of Contents Chapter 7 OpenVMS: An Introduction.............................................................................. 7.1 History..........................................................................................................................2 7.1.1 Today........................................................................................................................3 7.1.1.1 Popular VMS Software..........................................................................................4 7.1.2 VMS, DCL................................................................................................................4 Chapter 8 Getting Started with OpenVMS........................................................................ 8.1 Starting Up...................................................................................................................7 8.1.1 Finishing Your VMS Session...................................................................................7 8.1.1.1 Reconnecting..........................................................................................................7 8.1.2 Entering Commands..................................................................................................8 8.1.2.1 Retrieving Previous Commands............................................................................9 8.1.2.2 Aborting Screen Output.........................................................................................9
    [Show full text]
  • Getting Started with Windows Scripting
    Getting Started with Windows Scripting art I of the PowerShell, VBScript, and JScript Bible intro- IN THIS PART duces you to the powerful administrative tool that is Windows scripting. You’ll get an overview of Windows Chapter 1 P Introducing Windows Scripting scripting and its potential, and an introduction to three tech- nologies you can use for Windows scripting: VBScript, JScript, Chapter 2 and PowerShell. VBScript Essentials Chapter 3 JScript Essentials Chapter 4 PowerShell Fundamentals COPYRIGHTED MATERIAL 886804c01.indd6804c01.indd 1 11/21/09/21/09 11:16:17:16:17 PPMM 86804c01.indd 2 1/21/09 1:16:18 PM Introducing Windows Scripting indows scripting gives everyday users and administrators the ability to automate repetitive tasks, complete activities while IN THIS CHAPTER away from the computer, and perform many other time-saving W Introducing Windows scripting activities. Windows scripting accomplishes all of this by enabling you to create tools to automate tasks that would otherwise be handled manually, Why script Windows? such as creating user accounts, generating log files, managing print queues, or examining system information. By eliminating manual processes, you Getting to know can double, triple, or even quadruple your productivity and become more Windows Script Host effective and efficient at your job. Best of all, scripts are easy to create and Understanding the Windows you can rapidly develop prototypes of applications, procedures, and utili- scripting architecture ties; and then enhance these prototypes to get exactly what you need, or just throw them away and begin again. This ease of use gives you the flex- ibility to create the kinds of tools you need without a lot of fuss.
    [Show full text]
  • HP Openvms Utility Routines Manual
    HP OpenVMS Utility Routines Manual Order Number: BA554-90019 June 2010 This manual describes the OpenVMS utility routines, a set of routines that provide a programming interface to various OpenVMS utilities. Revision/Update Information: This manual supersedes the HP OpenVMS Utility Routines Manual, OpenVMS Alpha Version 8.3. Software Version: OpenVMS Version 8.4 for Integrity servers OpenVMS Alpha Version 8.4 Hewlett-Packard Company Palo Alto, California © Copyright 2010 Hewlett-Packard Development Company, L.P. Confidential computer software. Valid license from HP required for possession, use or copying. Consistent with FAR 12.211 and 12.212, Commercial Computer Software, Computer Software Documentation, and Technical Data for Commercial Items are licensed to the U.S. Government under vendor’s standard commercial license. The information contained herein is subject to change without notice. The only warranties for HP products and services are set forth in the express warranty statements accompanying such products and services. Nothing herein should be construed as constituting an additional warranty. HP shall not be liable for technical or editorial errors or omissions contained herein. Intel and Itanium are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States and other countries. ZK4493 The HP OpenVMS documentation set is available on CD. This document was prepared using DECdocument, Version 3.3-1B. Contents Preface ............................................................ xvii 1 Introduction to Utility Routines 2 Access Control List (ACL) Editor Routine 2.1 Introduction to the ACL Editor Routine ........................... ACL–1 2.2 Using the ACL Editor Routine: An Example ....................... ACL–1 2.3 ACL Editor Routine . ........................................ ACL–2 ACLEDIT$EDIT ...........................................
    [Show full text]
  • Blocked Attachments in Outlook Page 1 of 5
    Blocked attachments in Outlook Page 1 of 5 Outlook > Attachments Blocked attachments in Outlook To help protect your computer, Microsoft Office Outlook does not allow you to receive files of certain types (such as .exe files) as attachments, because of their potential for introducing a virus into your computer. Outlook blocks these files by default. If your organization uses Microsoft Exchange, only the e-mail server administrator can unblock these file types. End users of Outlook cannot unblock these file types. In this article Background - The Internet is a dangerous neighborhood How to share your files safely Attachment file types blocked by Outlook Background - The Internet is a dangerous neighborhood When Microsoft Outlook 97 first arrived on the market, the Internet still seemed like a relatively safe place, and sending attachments in e-mail seemed like a great idea. But then — beginning in March of 1999 when the Melissa virus struck, only to be followed by the ILOVEYOU virus — reality hit hard. These were the fastest-spreading viruses ever seen up to that time, and they forced a number of companies to shut down their e-mail systems, at significant cost. In order to spread, these attacks relied heavily on fooling people into believing that infected e-mail messages were coming from someone familiar, and even many computer experts fell victim to them and became infected. It is no secret that hackers like to attack the largest available target — and today, that is Microsoft Office Outlook. World wide, more than 300 million people use Outlook. Therefore, the industry and Microsoft customers look to Microsoft to provide protection against security threats.
    [Show full text]
  • RSX - 11 M-PLUS Mini-Reference
    RSX - 11 M-PLUS Mini-Reference Order No. AV-H435F-TC RSX - 11M-PLUS Mini-Reference Order Number. AV-H435F-TC RSX-ll M-PLUS Version 4.2 Digital Equipment Corporation Maynard, Massachusetts First Printing, September 1977 Revised, April 1982 Revised, April 1983 Revised, July 1985 R~vised, .September 1987 Revised, January 1989 The information in this document is subject to change without notice and should not be construed as a commitment by Digital Equipment Corporation. Digital Equipment Corporation assumes no responsibility for any errors that may appear in this document. The software described in this document is furnished under a license and may be used or copied only in accordance with the terms of such license. No responsibility is assumed for the use or reliability of software on equipment that is not supplied by Digital Equipment Corporation or its affiliated companies. © Digital Equipment Corporation 1977, 1982, 1983, 1985, 1987, 1989. All Rights Reserved. Printed in U.S.A. The postpaid Reader's Comments forms at the end of this document request your critical evaluation to assist in preparing future documentation. The following are trademarks of Digital Equipment Corporation: DEC DIBOL UNIBUS DEC/CMS EduSystem VAX DEC/MMS lAS VAXcluster DECnet MASSBUS VMS DECsystem-lO PDP VT DECSYSTEM-20 PDT DECUS RSTS DECwriter RSX ~U~UIl~DTM ZK5077 Contents Preface vii Conventions ............................................... viii Online Help Files Online Help Files ............................................. 3 Command Line Interpreters Monitor Console Routine (MCR) Commands ......................... 7 Digital Command Language (DCL) ............................... 21 utilities BAD Command Summary ...................................... 67 iii BRU Command Summary ...................................... 69 CMP Command Summary ...................................... 74 DMP Command Summary .....................................
    [Show full text]
  • Software Product Description
    Software Product Description PRODUCT NAME: VSI OpenVMS Version 8.4-1H1 for Integrity servers DO-VIBHAA-005 This SPD describes the VSI OpenVMS Operating System software for the Integrity server computer families. DESCRIPTION OpenVMS is a general purpose, multiuser operating system that runs in both production and development environments. Starting with OpenVMS Version 8.2, HP introduced support for OpenVMS for Integrity servers. VSI OpenVMS Version 8.4-1H1 is the first release of the OpenVMS computing environment by VMS Software, Inc (VSI). VSI OpenVMS Version 8.4-1H1 adds support for HP Integrity server models based on the Intel® Itanium® 9500 i4 series processors. OpenVMS software supports industry standards, facilitating application portability and interoperability. OpenVMS provides symmetric multiprocessing (SMP) support for multiprocessing systems. The OpenVMS operating system can be tuned to perform well in a wide variety of environments. This includes combinations of compute-intensive, I/O-intensive, client/server, real-time, and other environments. Actual system performance depends on the type of computer, available physical memory, and the number and type of active disk and tape drives. The OpenVMS operating system has well-integrated networking, distributed computing, client/server, multi-processing, and windowing capabilities. It contains extensive features that promote ease-of-use, improve the productivity of programmers, and facilitate system management. For information about the OpenVMS Version 8.4-1H1 new features, see the VSI OpenVMS Version 8.4-1H1 Cover Letter and Release Notes document at: http://www.vmssoftware.com USER ENVIRONMENT Users can access the OpenVMS software by using the English-like DIGITAL Command Language (DCL), the command language for OpenVMS that is supplied with the system.
    [Show full text]
  • Vmware Scripting API User's Manual
    VERSION 2.3 VMware Scripting API User’s Manual Please note that you can always find the most up-to-date technical documentation on our Web site at http://www.vmware.com/support/. VMware, Inc. The VMware Web site also provides the latest product updates Copyright © 1998-2006 VMware, Inc. All rights reserved. Protected by one or more of U.S. Patent Nos. 3145 Porter Drive 6,397,242, 6,496,847, 6,704,925, 6,711,672, 6,725,289, 6,735,601, 6,785,886, 6,789,156 6,795,966, 6, 880, 022, Palo Alto, CA 94304 6,961,941, 6,961,806 and 6,944,699; patents pending. VMware, the VMware “boxes” logo and design, Virtual www.vmware.com SMP and VMotion are registered trademarks or trademarks of VMware, Inc. in the United States and/or other jurisdictions. All other marks and names mentioned herein may be trademarks of their respective companies. Revision 20060925 Item: SDK-ENG-Q306-114 Table of Contents Introduction ___________________________________________________7 Introducing the VMware Scripting APIs _______________________________8 Supported Products ____________________________________________8 Intended Audience ____________________________________________9 Getting Support from VMware ___________________________________9 What’s New in This Release ________________________________________10 Using the VMware Scripting APIs ___________________________________13 Installing the VMware Scripting API _________________________________14 GSX Server __________________________________________________14 ESX Server __________________________________________________14
    [Show full text]
  • Openvms Migration & Modernization
    OpenVMS Migration & Modernization How to reduce financial pressures and risks while modernizing your IT system Transoft OpenVMS Application Migration & Modernization Migrating from OpenVMS in a cost-effective manner Contents Executive Summary ................................................................................................................... 1 Introduction ................................................................................................................................ 2 Implement package, re-develop or migrate?......................................................................................... 2 Transoft Application Migration & Modernization Solutions..................................................................... 3 Transoft OpenVMS Migration Project Approach...................................................................... 5 Pathfinder............................................................................................................................................ 5 Transoft Legacy Liberator – OpenVMS migration toolset...................................................................... 6 Migrating application code – COBOL, FORTRAN, Pascal, BASIC and C.............................................. 7 OpenVMS Run-Time & System Services replacement library............................................................... 8 Screen and Forms Management.......................................................................................................... 8 Migrating the data sources..................................................................................................................
    [Show full text]
  • Openvms DCL Dictionary: N–Z
    OpenVMS DCL Dictionary: N–Z Order Number: AA–PV5LH–TK June 2002 This manual provides detailed reference information and examples for Compaq OpenVMS DCL commands and lexical functions. Revision/Update Information: This manual supersedes the OpenVMS DCL Dictionary, Version 7.3. Software Version: OpenVMS Alpha Version 7.3-1 OpenVMS VAX Version 7.3 Compaq Computer Corporation Houston, Texas © 2002 Compaq Information Technologies Group, L.P. COMPAQ, the Compaq logo, Alpha, OpenVMS, Tru64, VAX, VMS, and the DIGITAL logo are trademarks of Compaq Information Technologies Group, L.P., in the U.S. and/or other countries. Microsoft, MS-DOS, Visual C++, Windows, and Windows NT are trademarks of Microsoft Corporation in the U.S. and/or other countries. Intel, Intel Inside, and Pentium are trademarks of Intel Corporation in the U.S. and/or other countries. Motif, OSF/1, and UNIX are trademarks of The Open Group in the U.S. and/or other countries. Java and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries. All other product names mentioned herein may be trademarks of their respective companies. Confidential computer software. Valid license from Compaq required for possession, use, or copying. Consistent with FAR 12.211 and 12.212, Commercial Computer Software, Computer Software Documentation, and Technical Data for Commercial Items are licensed to the U.S. Government under vendor’s standard commercial license. Compaq shall not be liable for technical or editorial errors or omissions contained herein. The information in this document is provided "as is" without warranty of any kind and is subject to change without notice.
    [Show full text]
  • Computer Virus Tutorial
    Computer Virus Tutorial Computer Virus Tutorial License Copyright 1996-2005, Computer Knowledge. All Rights Reserved The Computer Knowledge Virus Tutorial is a copyright product of Computer Knowledge. It also contains copyrighted material from others (used with permission). Please honor the copyrights. Read the tutorial, learn from the tutorial, download and run the PDF version of the tutorial on your computer, link to the tutorial. But, please don't copy it and claim it as your own in whole or part. The PDF version of the Computer Knowledge Virus Tutorial is NOT in the public domain. It is copyrighted by Computer Knowledge and it and all accompanying materials are protected by United States copyright law and also by international treaty provisions. The tutorial requires no payment of license fees for its use as an educational tool. If you are paying to use the tutorial please advise Computer Knowledge (PO Box 5818,www.co-bw.com Santa Maria, CA 93456 USA). Please provide contact information for those charging the fee; even a distribution fee. License for Distribution of the PDF Version No royalties are required for distribution. No fees may be charged for distribution of the tutorial. You may not use, copy, rent, lease, sell, modify, decompile, disassemble, otherwise reverse engineer, or transfer the licensed program except as provided in this agreement. Any such unauthorized use shall result in immediate and automatic termination of this license. In no case may this product be bundled with hardware or other software without written permission from Computer Knowledge (PO Box 5818, Santa Maria, CA 93456 USA).
    [Show full text]