Windows Server 2012 Automation with PowerShell Cookbook Over 110 recipes to automate Windows Server administrative tasks by using PowerShell Ed Goad BIRMINGHAM - MUMBAI Windows Server 2012 Automation with PowerShell Cookbook Copyright © 2013 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: March 2013 Production Reference: 1150313 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-84968-946-5 www.packtpub.com Cover Image by Abhishek Pandey ([email protected]) Credits Author Project Coordinator Ed Goad Anugya Khurana Reviewers Proofreaders Anderson Patricio Mario Cecere Donabel Santos Dirk Manuel Acquisition Editor Indexer Kevin Colaco Hemangini Bari Commissioning Editor Graphics Shreerang Deshpande Valentina D'silva Lead Technical Editor Production Coordinator Azharuddin Sheikh Conidon Miranda Technical Editors Cover Work Ankita Meshram Conidon Miranda Kirti Pujari Varun Pius Rodrigues About the Author Ed Goad is a Systems Architect who has been working in various roles in the IT field for 16 years. He first became interested in scripting and automation when presented with the task to uninstall software from over 1,000 systems with limited time and resources. He has worked with scripting and automation on multiple platforms and languages including PowerShell, VBscript, C#, and BASH scripting. Ed currently holds multiple Microsoft certifications, most recently including the Microsoft Certified IT Professional—Enterprise Administrator. Additional non-Microsoft certifications include VMware Certified Professional (VCP), Red Hat Certified System Administrator (RHCSA), EMC Proven Professional, Brocade Certified Network Engineer (BCNE), and Cisco Certified Network Associate (CCNA). Ed is currently on sabbatical, and is volunteering full time at the Amor Fe y Esperanza school in Honduras (http://www.afehonduras.org). There he is teaching computer and math classes to children who live and work in the garbage dump outside of the capital city of Tegucigalpa. I would like to thank my parents for always encouraging me when I was younger by telling me that I could be anything that I wanted, as long as I had good math skills. They bought our first computer before I even started school, and then let me break it and repair it over and over, driving my interest in computers. I want to thank my wife for loving me and encouraging me to grow and be more than I was. Without her love and encouragement my life wouldn't be nearly as full as it is now. And lastly, I would like to thank God for his blessings and the opportunities he has given me. As much as I have learned and accomplished, it is nothing compared to knowing his love. About the Reviewers Anderson Patricio is an Exchange Server MVP and a Messaging consultant based in Toronto, Canada, designing and deploying solutions in clients located in North and South America. He has been working with Exchange since Version 5 and has had the opportunity to use PowerShell since its beta release (code name Monad at that time). Anderson contributes to the Microsoft communities in several ways. In English, his blog www.andersonpatricio.ca is updated regularly with content for Exchange, PowerShell, and Microsoft in general. In Portuguese, he has an Exchange resource site (www. andersonpatricio.org). He is also a TechEd presenter in South America and also the creator of a couple of Exchange trainings in the Brazilian Microsoft Virtual Academy (MVA). You can follow him on Twitter at http://twitter.com/apatricio. He is the reviewer of several books such as Windows Powershell in Action by Bruce Payette, PowerShell in Practice by Richard Siddaway, and Microsoft Exchange 2010 PowerShell Cookbook by Mike Pfeiffer. Donabel Santos is a SQL Server MVP and is the senior SQL Server Developer/DBA/ Trainer at QueryWorks Solutions, a consulting and training company in Vancouver, BC. She has worked with SQL Server since Version 2000 on numerous development, tuning, reporting, and integration projects with ERPs, CRMs, SharePoint, and other custom applications. She holds MCITP certifications for SQL Server 2005/2008, and an MCTS for SharePoint. She is a Microsoft Certified Trainer (MCT), and is also the lead instructor for SQL Server Administration, Development, Tableau, and SSIS courses at the British Columbia Institute of Technology (BCIT). Donabel is a proud member of PASS (Professional Association of SQL Server), and a proud BCIT alumna (CST diploma and degree). Donabel blogs (www.sqlmusings.com), tweets (@sqlbelle), speaks and presents (SQLSaturday, VANPASS, Vancouver TechFest, and many more), trains (BCIT, QueryWorks Solutions), and writes (Packt, Idera, SSWUG, and so on). She is the author of Packt's SQL Server 2012 with PowerShell V3 Cookbook, and a contributing author of Manning's PowerShell Deep Dives. Thank you Eric, for all the support and love. Thank you for cooking the delicious dinners that invigorate me after a long day's work. You are my home. Thank you to my family—Papa, Mama, JR, RR, Lisa—you all give me strength and I am very blessed to have you in my life. Special shout out to my Tito Boy, who proudly told people in his network about my first book – thank you Tito Boy. Thank you to my BCIT family—Kevin Cudihee, Elsie Au, Joanne Atha, Charlie Blattler, Paul Mills, Bob Langelaan, Benjamin Yu, Brian Pidcock, Albert Wei and so many others—to all of my mentors, colleagues, and students, who never fail to inspire me to do better, be better. It's been a great ten years teaching at BCIT—and I look forward to a lot more wonderful years of learning, inspiring, and sharing. Special thanks to the Microsoft team and Microsoft communities, especially #sqlfamily. You guys are awesome and so many of you continuously and selflessly share your knowledge and expertise to a lot of people. I've been on the receiving end so many times, and I hope I can continue to pay it forward. I am so proud to be part of this community. Thank you to the PowerShell community, for the awesome blogs, books, and tweets, which immensely helped folks to learn, understand, and get excited about PowerShell. Most importantly, thank you Lord, for all the miracles and blessings in my life. www.PacktPub.com Support files, eBooks, discount offers and more You might want to visit www.PacktPub.com for support files and downloads related to your book. Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at service@ packtpub.com for more details. At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks. TM http://PacktLib.PacktPub.com Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can access, read and search across Packt's entire library of books. Why Subscribe? f Fully searchable across every book published by Packt f Copy and paste, print and bookmark content f On demand and accessible via web browser Free Access for Packt account holders If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view nine entirely free books. Simply use your login credentials for immediate access. Instant Updates on New Packt Books Get notified! Find out when new books are published by following @PacktEnterprise on Twitter, or the Packt Enterprise Facebook page. Table of Contents Preface 1 Chapter 1: Understanding PowerShell Scripting 7 Introduction 8 Managing security on PowerShell scripts 8 Creating and using functions 11 Creating and using modules 15 Creating and using PowerShell profiles 20 Passing variables to functions 22 Validating parameters in functions 24 Piping data to functions 30 Recording sessions with transcripts 32 Signing PowerShell scripts 33 Sending e-mail 36 Sorting and filtering 38 Using formatting to export numbers 40 Using formatting to export data views 42 Using jobs 44 Dealing with errors in PowerShell 46 Tuning PowerShell scripts for performance 49 Creating and using Cmdlets 51 Chapter 2: Managing Windows Network Services with PowerShell 59 Introduction 60 Configuring static networking 60 Installing domain controllers 67 Configuring zones in DNS 70 Configuring DHCP scopes 75 Configuring DHCP server failover 77 Converting DHCP addresses to static 78