Windows PowerShell for .NET Developers - Second Edition
4/5
()
About this ebook
Related to Windows PowerShell for .NET Developers - Second Edition
Related ebooks
Mastering Windows PowerShell Scripting Rating: 4 out of 5 stars4/5Getting Started with PowerShell Rating: 0 out of 5 stars0 ratingsMastering PowerShell: Unleashing the Power of Automation: The IT Collection Rating: 5 out of 5 stars5/5Learning Flask Framework Rating: 4 out of 5 stars4/5Learning PowerShell DSC Rating: 0 out of 5 stars0 ratingsConfigMgr - An Administrator's Guide to Deploying Applications using PowerShell Rating: 5 out of 5 stars5/5Mastering Linux Shell Scripting Rating: 4 out of 5 stars4/5Mastering Sharepoint Framework Rating: 0 out of 5 stars0 ratingsMicrosoft System Center PowerShell Essentials Rating: 0 out of 5 stars0 ratingsPowerShell: A Comprehensive Guide to Windows PowerShell Rating: 4 out of 5 stars4/5Building Web Services with Microsoft Azure Rating: 0 out of 5 stars0 ratingsBuilding RESTful Python Web Services Rating: 5 out of 5 stars5/5Learning PHP 7 Rating: 4 out of 5 stars4/5PowerShell: A Beginner's Guide to Windows PowerShell Rating: 4 out of 5 stars4/5Mastering Cloud Development using Microsoft Azure Rating: 0 out of 5 stars0 ratingsActive Directory with PowerShell Rating: 4 out of 5 stars4/5Microsoft Hyper-V PowerShell Automation Rating: 0 out of 5 stars0 ratingsPowerShell in Practice Rating: 0 out of 5 stars0 ratingsPowerShell for SQL Server Essentials Rating: 0 out of 5 stars0 ratingsPowerShell in Depth Rating: 0 out of 5 stars0 ratingsWindows PowerShell in Action Rating: 4 out of 5 stars4/5Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS Rating: 5 out of 5 stars5/5Learn PowerShell Scripting in a Month of Lunches Rating: 0 out of 5 stars0 ratingsGitLab Cookbook Rating: 0 out of 5 stars0 ratingsPostgreSQL Administration Cookbook, 9.5/9.6 Edition Rating: 0 out of 5 stars0 ratingsHigh Availability MySQL Cookbook Rating: 0 out of 5 stars0 ratingsApache Tomcat 7 Essentials Rating: 0 out of 5 stars0 ratingsPowerShell Deep Dives Rating: 0 out of 5 stars0 ratingsLearn Windows PowerShell in a Month of Lunches Rating: 0 out of 5 stars0 ratings
Networking For You
Networking For Dummies Rating: 5 out of 5 stars5/5Cybersecurity: The Beginner's Guide: A comprehensive guide to getting started in cybersecurity Rating: 5 out of 5 stars5/5Microsoft Azure For Dummies Rating: 0 out of 5 stars0 ratingsComputer Networking: An introductory guide for complete beginners: Computer Networking, #1 Rating: 5 out of 5 stars5/5AWS Certified Cloud Practitioner Study Guide: CLF-C01 Exam Rating: 5 out of 5 stars5/5Windows Command Line Administration Instant Reference Rating: 0 out of 5 stars0 ratingsHTTP/2 in Action Rating: 0 out of 5 stars0 ratingsThe Compete Ccna 200-301 Study Guide: Network Engineering Edition Rating: 5 out of 5 stars5/5Networking All-in-One For Dummies Rating: 5 out of 5 stars5/5ITIL 4: Digital and IT strategy: Reference and study guide Rating: 5 out of 5 stars5/5Quantum Computing For Dummies Rating: 0 out of 5 stars0 ratingsLinux Bible Rating: 0 out of 5 stars0 ratingsLearning Linux Shell Scripting Rating: 4 out of 5 stars4/5Networking Fundamentals: Develop the networking skills required to pass the Microsoft MTA Networking Fundamentals Exam 98-366 Rating: 0 out of 5 stars0 ratingsFortinet FCP - FortiGate 7.4 Administrator Exam Preparation Rating: 0 out of 5 stars0 ratingsAmazon Web Services (AWS) Interview Questions and Answers Rating: 5 out of 5 stars5/5Learning Python Network Programming Rating: 5 out of 5 stars5/5Docker: A Quick-Start Beginner's Guide Rating: 4 out of 5 stars4/5Cisco Networking All-in-One For Dummies Rating: 4 out of 5 stars4/5Rust In Practice Rating: 0 out of 5 stars0 ratingsCisco Networking Essentials Rating: 0 out of 5 stars0 ratingsTCP/IP for Everyone Rating: 4 out of 5 stars4/5Rereading Marx in the Age of Digital Capitalism Rating: 0 out of 5 stars0 ratingsITIL® 4 Direct, Plan and Improve (DPI): Your companion to the ITIL 4 Managing Professional and Strategic Leader DPI certification Rating: 0 out of 5 stars0 ratingsITIL® 4 Drive Stakeholder Value (DSV): Your companion to the ITIL 4 Managing Professional DSV certification Rating: 0 out of 5 stars0 ratingsMicrosoft Certified Azure Fundamentals Study Guide: Exam AZ-900 Rating: 0 out of 5 stars0 ratingsProgramming Arduino: Getting Started with Sketches Rating: 4 out of 5 stars4/5Rust for Network Programming and Automation Rating: 0 out of 5 stars0 ratings
Reviews for Windows PowerShell for .NET Developers - Second Edition
1 rating0 reviews
Book preview
Windows PowerShell for .NET Developers - Second Edition - Venkatesan Chendrayan
Table of Contents
Windows PowerShell for .NET Developers Second Edition
Credits
Foreword
About the Authors
About the Reviewers
www.PacktPub.com
Support files, eBooks, discount offers, and more
Why subscribe?
Free access for Packt account holders
Instant updates on new Packt books
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Downloading the color images of this book
Errata
Piracy
Questions
1. Getting Started with Windows PowerShell
Scripting the cmdlet style
Introducing Windows PowerShell
Installing Windows Management Framework 5.0
The Windows PowerShell consoles
The Windows PowerShell console host
Setting up the console host using GUI
Setting up the console host using PowerShell
Exploring the Windows PowerShell ISE host using GUI
Benefits of an ISE
The PowerShell ISE script browser
Using an interactive shell
Windows PowerShell cmdlets
Getting help
Understanding aliases
Understanding expressions
Understanding objects
Understanding pipelines
Exporting a running process to a CSV file
Understanding filtering and formatting
PowerShell formatting
Exploring snippets in the PowerShell ISE
Getting started with PowerShell scripting
Summary
2. Unleashing Development Skills Using Windows PowerShell 5.0
Basics of WMI and CIM
Working with XML and COM
Exploring COM and Automation
Exploring .NET objects
Creating .NET objects
Extending .NET objects for Administrations and Development tasks
Extending the .NET Framework types
Building advanced scripts and modules
Exploring Windows PowerShell 5.0
The basics of Desired State Configuration
The Authoring phase
The Staging phase
The Make it so
phase
Use case of classes in WMF 5.0
Constructors
Parsing structured objects using PowerShell
Exploring Package Management
Exploring PowerShellGet
Understanding PowerShell modules
Introduction to modules
Script modules
Binary modules
Manifest modules
Dynamic modules
Script debugging
Managing breakpoints
Line breakpoints
Variable breakpoints
Command breakpoints
Debugging scripts
Summary
3. Exploring Desired State Configuration
Prerequisites
Installing the WMF 5.0 April 2015 preview
Imperative versus declarative programming
Getting started with DSC
The Authoring phase
The Staging phase
The Make it so
phase
Local Configuration Manager
Parameterizing the configuration script
Understanding MOF
Exploring Windows Remote Management and CIM
Windows PowerShell remoting
Exploring WSMan cmdlets
HTTP/HTTPS Listener
Exploring CIM commands
Exploring CIM methods
Querying the remote machines using CIM
Creating configuration scripts
Creating a configuration with MOF
Creating a Class-defined DSC resource
Types of deployment modes
The push mode
The pull mode
Creating a pull server using the SMB share
Creating the pull server using HTTP and HTTPS
Summary
4. PowerShell and Web Technologies
PowerShell Web Access
Installing PowerShell Web Access
Configuring PowerShell Web Access
Applying authorization rules
Management OData IIS Extensions
Creating the Management OData web service
Exploring web requests
Downloading files from the Internet
Reading a file from the Internet
Exploring web services
Using web services
Building web services
Exploring the REST API
Using the Azure REST API in PowerShell
Exploring JSON
Summary
5. Exploring Application Programming Interface
Exploring API using PowerShell
The EWS API for managing Exchange Online
Purging items in the mailbox folder
Deleting items from the mailbox folder
The Lync 2013 client-side API
Installation of LYNC SDK
Exploring client settings
Automating test calls
IM with contacts
Client-side object model – SharePoint Online
How does CSOM Work?
Creating and deleting list
Making PowerShell modules with SDKs
Summary
Index
Windows PowerShell for .NET Developers Second Edition
Windows PowerShell for .NET Developers Second Edition
Copyright © 2015 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: January 2014
Second published: October 2015
Production reference: 1161015
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78528-743-5
www.packtpub.com
Credits
Authors
Chendrayan Venkatesan
Sherif Talaat
Reviewers
Richard Gibson
Mayur Arvind Makwana
Laxmikant P Patil
Commissioning Editor
Dipika Gaonkar
Acquisition Editors
Nikhil Karkal
Vinay Argekar
Content Development Editor
Sumeet Sawant
Technical Editor
Manthan Raja
Copy Editor
Shruti Iyer
Project Coordinator
Shweta H Birwatkar
Proofreader
Safis Editing
Indexer
Monica Ajmera Mehta
Production Coordinator
Arvindkumar Gupta
Cover Work
Arvindkumar Gupta
Foreword
There is no doubt that Windows PowerShell is a great tool that you must have in your toolbox, whether you are a developer or an IT Pro. Windows PowerShell is not just another scripting language, but a complete management and automation platform with endless possibilities.
For a very long time, there has been a myth about scripting languages and who should use them. This myth states that automation tools, such as scripting tools and automation languages, are meant to be used by IT Pros. I think the reason for this misconception about scripting and automation is the limited functionalities and capabilities of those tools back then. However, this is no longer the case with PowerShell. For instance, Windows PowerShell is the first object-based scripting language because it is built on top of the .NET framework, which makes it easy to access all the powerful libraries underneath. Also, out of the box, it easily works with XML, ADSI, WMI, COM, and so on.
Today, for developers, Windows PowerShell is a core component of development tools such as Visual Studio and Team Foundation Server. You can use PowerShell to build your own automated test cases, provision and deploy applications, extend your software by building a PowerShell automation and management interface, and more interesting stuff that will be discussed in this book.
As the author of the first edition of this title, I was asked to write the second edition too. However, due to the fact the I was busy with another writing project I had to nominate someone to write it. Honestly, I couldn't think of anyone but Chendrayan Venkatesan (also known as Chen V), not only because he is a fellow MVP but also because of his enthusiasm and passion for PowerShell that allow him to always come up with great ideas and unique ways to do impressive things (you will find some of it in this book) with PowerShell.
Windows PowerShell for .NET developers is the jump-start reference for any developer looking for a consolidated resource to learn about Windows PowerShell. It is a fast-paced guide with short, crisp, and to the point real-world examples. This book takes you through a learning journey that starts with an overview of Windows PowerShell capabilities, components, and architecture. Then it walks you through the PowerShell syntax (which is pretty close to C#), working with objects, how to write scripts, functions, workflows, and so on.
Then you will learn how to use your .NET development experience to benefit from PowerShell. The book will show you how to work and manipulate .NET objects, COM, XML, WMI, CMI, and so on. After that, you will be introduced to PowerShell Desired State Configuration (DSC) and how to use it. DSC is a very useful component when it comes to standardizing your environments, which is important when you are deploying an application or even building a development and test environment that has to be identical to the production one.
Next the journey gets more interesting with cool stuff about using Windows PowerShell with the web technologies such as RESTful Web services, OData, and so many more. And finally, you will explore PowerShell API and SDK.
I am sure that you will enjoy the time you spend reading this book and learning these cool things brought to you by the one-and-only Chen V.
Ladies and gentlemen, get ready. The fun is about to start.
Sherif Talaat
PowerShell MVP
About the Authors
Chendrayan Venkatesan (Chen V) is a SharePoint IT Professional who has worked for the Information, Computer and Technology industry since 2005. Chen V started his career as Windows XP technical support engineer and became a SharePoint IT Pro in the year 2007. He mainly focuses on automating Microsoft Technologies such as SharePoint, LYNC, and Exchange. He was awarded the Microsoft Most Valuable Professional (MVP) in 2014. He speaks on Windows PowerShell, SharePoint Servers, Content Management, and IT Process Automations. He blogs in http://chen.about-powershell.com and is mentoring three IT professionals in PowerShell. He is a TechNet Wiki addict and has introduced Windows PowerShell as a category in the TechNet Wiki Guru award competition. To connect with Chen V you can visit his web site http://about-powershell.com or choose your favourite social media twitter @ChendrayanV or LinkedIn nl.linkedin.com/in/chendrayanv.
Sherif Talaat is a young computer science addict. He holds many technology certificates. Sherif has been working in the ICT industry since 2005. He has worked on the Microsoft's core infrastructure platforms and solutions with more focus on the cloud and data center management, IT Process Automation (ITPA), and scripting techniques.
Sherif is one of the early adopters of Windows PowerShell in Middle East and Africa. He speaks about Windows PowerShell at technical events and user groups' gatherings; he is also the founder of Egypt PowerShell User Group and the author of the first and only Arabic PowerShell blog. He has been awarded the Egyptian MCIT Shield and has won the Microsoft Most Valuable Professional (MVP) award for PowerShell seven times in a row since 2009. He is also the author of three different Windows PowerShell titles for IT professionals and developers. Last but not least, he is also a TED conference speaker.
You can learn more about him at http://sheriftalaat.com and follow him on Twitter @SherifTalaat.
About the Reviewers
Richard Gibson lives in London and has worked as a .NET developer for eight years. His work has taken him into the world of DevOps and Powershell, which has become a necessary skill for the automation of everyday tasks.
He currently works for http://www.asos.com/ as a senior developer, spending most of his Powershell time providing continuous deployment for the business through TeamCity.
Richard blogs on various issues related to .NET and PowerShell at http://richiban.uk/.
Mayur Makwana is a software professional holding a Computer Engineering degree with more than six years experience in the information technology field covering Microsoft, Citrix, and VMware technologies. Presently he leads the infrastructure operations on Citrix (XenApp/Xendesktop) and Windows (WSUS/SCCM) projects in one of the leading Fortune 500 companies. He is a huge believer in certifications and holds the following titles:
Citrix Certified Administrator (CCA) for Citrix XenApp 6.5
Microsoft Certified Professional (MCP)
Microsoft Specialist (Microsoft Server Virtualization with Windows Server Hyper-V and System Center)
VMware Certified Associate-Data Center Virtualization (VCA-DCV)
Information Technology Infrastructure Library (ITIL) V3 Foundation
ChangeBASE AOK (application compatibility testing and remediation)
Oracle Certified Associate (OCA)
He reviews technical books and writes technical blogs (www.all-about-software-applications-repackaging.com).
He has attended several courses and conducted training in the following fields:
Licensing Windows Server
Advanced Tools and Scripting with PowerShell 3.0 Jump Start
Deploying Windows 8
Licensing Windows 8
Migrating from Windows XP to Windows 7
Networking Fundamentals
Introduction to Hyper-V Jump Start
I would like to thank my mother Beena Makwana who has always encouraged me to utilize my potential and help people by sharing my expertise and knowledge. Thanks to the Packt Publishing team for giving me this opportunity.
Laxmikant Patil is a Senior Technical Architect on various Microsoft Technologies. He has 13 years of experience in building complex software systems from devices running Linux and BerklyDB to Cloud-based highly scalable systems and SharePoint-based Portal and Content systems. He has written many papers on Cloud adoption, technology migrations, ERP integrations, knowledge management, and cost-effective designing areas. His passion is to apply technology to business problems to supply solutions that will fit into customers' budgets. His dream project is to develop a platform for children that will help them develop analytical and logical thinking abilities. During his spare time, he enjoys watching cartoon movies. He writes on his blog site http://laxmikantpatil.com/.
I would like to thank my family, especially my lovely wife Vasudha for her understanding and sacrifices during this period. Love you!
My thanks to the PacktPub team for believing in me. It was a great experience working with you. Thank you, team!
www.PacktPub.com
Support files, eBooks, discount offers, and more
For support files and downloads related to your book, please visit www.PacktPub.com.
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
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.
https://www2.packtpub.com/books/subscription/packtlib
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books.
Why subscribe?
Fully searchable across every book published by Packt
Copy and paste, print, and bookmark content
On demand and accessible via a 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 9 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.
Preface
Windows PowerShell is no longer a secret in the world of IT. Starting in 2006, it's been growing and has now turned out to be a massive automation platform especially for Microsoft products and technologies.
Windows PowerShell 5.0 for .NET Developers is your self-start guide to performing automation using Windows PowerShell. This book will help you to understand the PowerShell syntax and grammar and will also teach you techniques to remove the rough edges of manual deployments. Packed with PowerShell scripts and sample C# codes to automate tasks, it also includes real-world scenarios, such as administrating office servers to help you save time and perform deployments swiftly and efficiently.
What this book covers
Chapter 1, Getting Started with Windows PowerShell, covers the basics of Windows PowerShell, the importance of object-based shells, and how ISE helps us in building scripts.
Chapter 2, Unleashing Development Skills Using Windows PowerShell 5.0, explores XML and COM automation and NET objects for Admin and Development tasks. You will also get acquainted with the PowerShell modules and how script debugging works.
Chapter 3, Exploring Desired State Configuration, gets you started with Desired State Configuration (DSC) concepts such as installing WMF 5.0 April 2015 Preview, Imperative versus declarative programming and their significant features. It also explores Windows Remote Management and CIM, and teaches you to create configurations scripts.
Chapter 4, PowerShell and Web Technologies, starts with the installation and configuration of Web Access, and explores Web requests, services, and REST API.
Chapter 5, Exploring Application Programming Interface, covers the key benefits of the API and using it in Windows PowerShell, Exchange Web Services, LYNC Client-side API, and SharePoint Client Side Object Model; you'll walk through a demo to create a PowerShell module in C# for office servers.
What you need for this book
The following is a list of software required and supported operating systems for this book:
Windows Management Framework 3.0 and advanced (Windows 7)
Windows Management Framework 5.0 (Windows 7, Windows Server 2008 R2 SP1 and advanced)
Windows Management Framework 5.0 (Windows 7, Windows Server 2012 R2)
Who this book is for
If you are