PowerShell: Automating Administrative Tasks

Book description

Learn PowerShell from the inside out, right from basic scripting all the way to becoming a master at automating, managing, and maintaining your Windows environment

About This Book

  • Use proven best practices to optimize code and automate redundant tasks

  • Get to grips with PowerShell’s advanced functions and effectively administer your system

  • Create a variety of PowerShell scripts to automate your environment

  • Who This Book Is For

    If you are a system administrator who wants to become an expert in automating and managing your Windows environment, then this course is for you. Some basic understanding of PowerShell would be helpful.

    What You Will Learn

  • Discover PowerShell commands and cmdlets and understand PowerShell formatting

  • Solve common problems using basic file input/output functions

  • Use .NET classes in Windows PowerShell and C# and manage Exchange Online

  • Use PowerShell in C# to manage Exchange Online and work with .NET classes in PowerShell

  • Automate LYNC clients, consuming client-side object models to administrate SharePoint Online

  • Optimize code through the use of functions, switches, and looping structures

  • Manage files, folders, and registries through the use of PowerShell

  • Discover best practices to manage Microsoft systems

  • In Detail

    Are you tired of managing Windows administrative tasks manually and are looking to automate the entire process? If yes, then this is the right course for you.

    This learning path starts your PowerShell journey and will help you automate the administration of the Windows operating system and applications that run on Windows. It will get you up and running with PowerShell, taking you from the basics of installation to writing scripts and performing web server automation. You will explore the PowerShell environment and discover how to use cmdlets, functions, and scripts to automate Windows systems.

    The next installment of the course focuses on gaining concrete knowledge of Windows PowerShell scripting to perform professional-level scripting. The techniques here are packed with PowerShell scripts and sample C# code to automate tasks. You will use .NET classes in PowerShell and C# to manage Exchange Online.

    In the final section, you will delve into real-world examples to learn how to simplify the management of your Windows environment. You will get to grips with PowerShell’s advanced functions and how to most effectively administer your system.

    This Learning Path combines some of the best that Packt has to offer in one complete, curated package. It includes content from the following Packt products:

  • Getting Started with PowerShell by Michael Shepard

  • Windows PowerShell for .Net Developers Second Edition by Chendrayan Venkatesan and Sherif Talaat

  • Mastering Windows PowerShell Scripting by Brenton J.W. Blawat

  • Style and approach

    This is a step-by-step course to help you effectively administer and maintain your development environment with PowerShell.

    Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

    Table of contents

    1. PowerShell: Automating Administrative Tasks
      1. Table of Contents
      2. PowerShell: Automating Administrative Tasks
      3. PowerShell: Automating Administrative Tasks
      4. Credits
      5. Preface
        1. What this learning path covers
        2. What you need for this learning path
        3. Who this learning path is for
        4. Reader feedback
        5. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
        6. Questions
      6. 1. Module 1
        1. 1. First Steps
          1. Determining the installed PowerShell version
            1. Using the registry to find the installed version
            2. Using PowerShell to find the installed version
          2. Installing/upgrading PowerShell
          3. Starting a PowerShell session
            1. PowerShell hosts
            2. 64-bit and 32-bit PowerShell
            3. PowerShell as an administrator
          4. Simple PowerShell commands
          5. PowerShell aliases
          6. Summary
          7. For further reading
        2. 2. Building Blocks
          1. What can you do?
          2. The scripter's secret weapon – tab completion
          3. How does that work?
          4. Interpreting the cmdlet syntax
          5. Summary
          6. For further reading
        3. 3. Objects and PowerShell
          1. Objects all the way down
            1. Digging into objects
            2. Types, classes, and objects
          2. What are members?
            1. The DOS DIR command
            2. The IPCONFIG command
            3. PowerShell for comparison
          3. The Get-Member cmdlet
          4. Where did these all come from?
          5. Summary
          6. For further reading
        4. 4. Life on the Assembly Line
          1. The pipeline as an assembly line
            1. This isn't your DOS or Linux pipeline
            2. Objects at your disposal
          2. Dealing with pipeline data
            1. The Sort-Object cmdlet
            2. The Where-Object cmdlet
            3. The Select-Object cmdlet
              1. Limiting the number of objects returned
              2. Limiting the properties of the objects returned
              3. Retrieving the values of a single property
            4. The Measure-Object cmdlet
            5. The Group-Object cmdlet
          3. Putting them together
          4. Summary
          5. For further reading
        5. 5. Formatting Output
          1. When does formatting occur?
          2. The rules of automatic formatting
            1. Formatting files
            2. Formatting decisions are based on the first object
            3. Small objects go in a table
            4. Large objects go in a list
          3. Cmdlets that control formatting
            1. Format-Table and Format-List
          4. The dangers of formatting
          5. Best practices of formatting
          6. Summary
          7. For further reading
        6. 6. Scripts
          1. Packaging commands
            1. Execution policy
            2. Types of scripts
            3. Scopes and scripts
          2. Parameters add flexibility
          3. Adding some logic
            1. Conditional logic (IF)
            2. Looping logic
            3. More logic
          4. Profiles
          5. Summary
          6. For further reading
        7. 7. Functions
          1. Another kind of container
            1. Comparing scripts and functions
          2. Executing and calling functions
          3. Naming conventions
          4. Comment-based help
          5. Parameters revisited
            1. Typed parameters
            2. Switches
          6. Default values for parameters
          7. Output
          8. Summary
          9. For further reading
        8. 8. Modules
          1. Packaging functions
          2. Script modules
            1. The Export-ModuleMember cmdlet
          3. Where do modules live?
          4. Removing a module
          5. PowerShell module autoloading
          6. The #Requires statement
          7. Removing a module – take two
            1. Manifest modules
          8. Listing modules
          9. Summary
        9. 9. File I/O
          1. Reading and writing text files
            1. Writing text files
          2. Working with CSV files
            1. Output to CSV for quick reports
            2. The Invoke-Item cmdlet
            3. Import from CSV for quick objects
          3. PowerShell streams and redirection
            1. Other types of redirection operators
            2. The out-file cmdlet
          4. CLIXML – a special type of XML
          5. Summary
          6. For further reading
        10. 10. WMI and CIM
          1. What is WMI?
            1. WMI organization
          2. Finding WMI classes
          3. Retrieving objects with Get-WMIObject
            1. Getting the right instance
            2. WQL syntax
          4. Calling methods
          5. WMI and CIM
            1. The CIM cmdlets
          6. CDXML modules
          7. Summary
          8. For further reading
        11. 11. Web Server Administration
          1. Installing IIS
            1. Detecting and installing IIS in Windows 8.1
            2. Detecting and installing IIS in Server 2012R2
          2. Verifying IIS
          3. The WebAdministration module
          4. Starting, stopping, and restarting IIS
          5. Creating virtual directories and web applications
          6. Working with application pools
            1. Creating application pools
          7. Summary
          8. For further reading
        12. A. Next Steps
      7. 2. Module 2
        1. 1. Getting Started with Windows PowerShell
          1. Scripting the cmdlet style
          2. Introducing Windows PowerShell
            1. Installing Windows Management Framework 5.0
            2. The Windows PowerShell consoles
              1. The Windows PowerShell console host
                1. Setting up the console host using GUI
                2. Setting up the console host using PowerShell
                3. Exploring the Windows PowerShell ISE host using GUI
                4. Benefits of an ISE
                5. The PowerShell ISE script browser
          3. Using an interactive shell
            1. Windows PowerShell cmdlets
            2. Getting help
            3. Understanding aliases
            4. Understanding expressions
            5. Understanding objects
            6. Understanding pipelines
              1. Exporting a running process to a CSV file
            7. Understanding filtering and formatting
              1. PowerShell formatting
              2. Exploring snippets in the PowerShell ISE
            8. Getting started with PowerShell scripting
          4. Summary
        2. 2. Unleashing Development Skills Using Windows PowerShell 5.0
          1. Basics of WMI and CIM
          2. Working with XML and COM
            1. Exploring COM and Automation
          3. Exploring .NET objects
            1. Creating .NET objects
            2. Extending .NET objects for Administrations and Development tasks
            3. Extending the .NET Framework types
            4. Building advanced scripts and modules
          4. Exploring Windows PowerShell 5.0
            1. The basics of Desired State Configuration
              1. The Authoring phase
              2. The Staging phase
              3. The "Make it so" phase
            2. Use case of classes in WMF 5.0
              1. Constructors
            3. Parsing structured objects using PowerShell
            4. Exploring Package Management
            5. Exploring PowerShellGet
          5. Understanding PowerShell modules
            1. Introduction to modules
              1. Script modules
              2. Binary modules
              3. Manifest modules
              4. Dynamic modules
          6. Script debugging
            1. Managing breakpoints
              1. Line breakpoints
              2. Variable breakpoints
              3. Command breakpoints
            2. Debugging scripts
          7. Summary
        3. 3. Exploring Desired State Configuration
          1. Prerequisites
          2. Installing the WMF 5.0 April 2015 preview
          3. Imperative versus declarative programming
          4. Getting started with DSC
            1. The Authoring phase
            2. The Staging phase
            3. The "Make it so" phase
            4. Local Configuration Manager
            5. Parameterizing the configuration script
            6. Understanding MOF
          5. Exploring Windows Remote Management and CIM
            1. Windows PowerShell remoting
            2. Exploring WSMan cmdlets
            3. HTTP/HTTPS Listener
            4. Exploring CIM commands
            5. Exploring CIM methods
            6. Querying the remote machines using CIM
          6. Creating configuration scripts
            1. Creating a configuration with MOF
            2. Creating a Class-defined DSC resource
          7. Types of deployment modes
            1. The push mode
            2. The pull mode
              1. Creating a pull server using the SMB share
              2. Creating the pull server using HTTP and HTTPS
          8. Summary
        4. 4. PowerShell and Web Technologies
          1. PowerShell Web Access
            1. Installing PowerShell Web Access
            2. Configuring PowerShell Web Access
            3. Applying authorization rules
          2. Management OData IIS Extensions
            1. Creating the Management OData web service
          3. Exploring web requests
            1. Downloading files from the Internet
            2. Reading a file from the Internet
          4. Exploring web services
            1. Using web services
            2. Building web services
          5. Exploring the REST API
            1. Using the Azure REST API in PowerShell
          6. Exploring JSON
          7. Summary
        5. 5. Exploring Application Programming Interface
          1. Exploring API using PowerShell
            1. The EWS API for managing Exchange Online
            2. Purging items in the mailbox folder
            3. Deleting items from the mailbox folder
          2. The Lync 2013 client-side API
            1. Installation of LYNC SDK
            2. Exploring client settings
            3. Automating test calls
            4. IM with contacts
          3. Client-side object model – SharePoint Online
            1. How does CSOM Work?
            2. Creating and deleting list
            3. Making PowerShell modules with SDKs
          4. Summary
      8. 3. Module 3
        1. 1. Variables, Arrays, and Hashes
          1. Variables
            1. Objects stored in variables
          2. Arrays
            1. Single-dimension arrays
            2. Jagged arrays
            3. Updating array values
          3. Hashes
          4. Deciding the best container for your scripts
          5. Summary
        2. 2. Data Parsing and Manipulation
          1. String manipulation
            1. Replacing and splitting strings
            2. Counting and trimming strings
            3. The Trim method
            4. The Substring method
            5. The string true and false methods
          2. Number manipulation and parsing
            1. Formatting numbers
            2. Formatting bytes
          3. Date and time manipulation
          4. Forcing data types
          5. Piping variables
          6. Summary
        3. 3. Comparison Operators
          1. Comparison operator basics
          2. Equal and not equal comparison
          3. Greater than and less than comparison
          4. Contains, like, and match operators
          5. And / OR comparison operators
          6. Best practices for comparison operators
          7. Summary
        4. 4. Functions, Switches, and Loops Structures
          1. Functions
          2. Looping structures
          3. Switches
          4. Combining the use of functions, switches, and loops
          5. Best practices for functions, switches, and loops
            1. Best practices for functions
            2. Best practices for looping structures and switches
          6. Summary
        5. 5. Regular Expressions
          1. Getting started with regular expressions
          2. Regular expression grouping constructs and ranges
          3. Regular expression quantifiers
          4. Regular expression anchors
          5. Regular expressions examples
          6. Summary
        6. 6. Error and Exception Handling and Testing Code
          1. Error and exception handling – parameters
          2. Error and exception handling – Try/Catch
            1. Error and exception handling –Try/Catch with parameters
          3. Error and exception handling – legacy exception handling
          4. Methodologies for testing code
            1. Testing the –WhatIf argument
            2. Testing the frequency
            3. Hit testing containers
            4. Don't test in production
          5. Summary
        7. 7. Session-based Remote Management
          1. Utilizing CIM sessions
            1. Creating a session
            2. Creating a session with session options
            3. Using sessions for remote management
            4. Removing sessions
          2. Summary
        8. 8. Managing Files, Folders, and Registry Items
          1. Registry provider
          2. Creating files, folders, and registry items with PowerShell
          3. Adding named values to registry keys
          4. Verifying files, folders, and registry items
          5. Copying and moving files and folders
          6. Renaming files, folders, registry keys, and named values
          7. Deleting files, folders, registry keys, and named values
          8. Summary
        9. 9. File, Folder, and Registry Attributes, ACLs, and Properties
          1. Retrieving attributes and properties
          2. Viewing file and folder extended attributes
          3. Setting the mode and extended file and folder attributes
          4. Managing file, folder, and registry permissions
            1. Copying access control lists
            2. Adding and removing ACL rules
          5. Summary
        10. 10. Windows Management Instrumentation
          1. WMI structure
          2. Using WMI objects
          3. Searching for WMI classes
          4. Creating, modifying, and removing WMI property instances
            1. Creating property instances
            2. Modifying property instances
            3. Removing property instances
          5. Invoking WMI class methods
          6. Summary
        11. 11. XML Manipulation
          1. XML file structure
            1. Reading XML files
            2. Adding XML content
            3. Modifying XML content
            4. Removing XML content
          2. Summary
        12. 12. Managing Microsoft Systems with PowerShell
          1. Managing local users and groups
            1. Managing local users
            2. Managing local groups
            3. Querying for local users and groups
          2. Managing Windows services
          3. Managing Windows processes
          4. Installing Windows features and roles
          5. Summary
        13. 13. Automation of the Environment
          1. Invoking programs for automation
          2. Using desired state configuration
            1. Authoring phase
            2. Staging and remediation phase
          3. Detecting and restoring drifting configurations
          4. Summary
        14. 14. Script Creation Best Practices and Conclusion
          1. Best practices for script management
          2. # commenting headers
            1. Commenting code
          3. Best practices for script creation
            1. Script structure
            2. Other important best practices for script creation
            3. Controlling source files
          4. Best practices for software automation
          5. Summary
            1. Mastering Windows PowerShell Scripting – conclusion
            2. Staying connected with the author
      9. Bibliography
      10. Index

    Product information

    • Title: PowerShell: Automating Administrative Tasks
    • Author(s): Michael Shepard, Chendrayan Venkatesan, Sherif Talaat, Brenton J.W. Blawat
    • Release date: February 2017
    • Publisher(s): Packt Publishing
    • ISBN: 9781787123755