PowerShell Core for Linux Administrators Cookbook

Book description

Use PowerShell 6.0 on Linux to automate complex, repetitive, and time consuming tasks

About This Book

  • Understand PowerShell's object-oriented approach, and truly cross-platform capabilities, which work equally well on Windows, Linux and macOS.
  • Extend your OMS capabilities to get rich insights about your Linux workloads to quickly remediate any issues.
  • A recipe-based guide that will enhance your native Linux capabilities with PowerShell 6.0.

Who This Book Is For

If you are system administrator who wants to learn to control and automate your Linux environment with PowerShell 6.0 then, this book is for you. Basic knowledge of PowerShell scripting would be necessary. Very basic knowledge required with respect to how an operating system is structured and how to use the command line interface to work with the operating system.

What You Will Learn

  • Understand the fundamentals of .NET Core and PowerShell
  • Understand the advanced concepts of .NET Core and PowerShell
  • Learn to write PowerShell scripts and functions with the best practices in mind
  • Take a deep dive into administering computers locally as well as remotely using PowerShell
  • Use PowerShell for advanced administration such as on the Cloud, Docker containers, VMware and SQL Server

In Detail

PowerShell is an implementation of .NET Core. .NET Core is a cross-platform open source management framework, which adheres to POSIX standards and makes available API calls that work well with all of the major operating systems: Windows, Linux and macOS. .NET Core for Linux has been a success, because of its adherence to standards, as well as for its lightweight implementation. PowerShell extends the capabilities towards management of Linux servers as well as using containerizers such as Docker.

This book will include real-world examples on how you can use core PowerShell functions to effectively administer Linux-based systems and environments. Later, this book will cover interesting recipes on how system administrators can make use of PowerShell 6.0 to quickly automate complex, repetitive and time-consuming tasks. This book will also teach you to develop scripts to automate tasks that involve systems and enterprise management. By the end of this book, you will also understand your OMS automation capabilities to apply, monitor, and update configurations, gaining real-time visibility into your Linux workloads to quickly get it resolved.

Style and approach

With a recipe-based approach, we will help you take advantage of PowerShell 6.0 to call all native Linux capabilities to automate your environment.

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. PowerShell Core for Linux Administrators Cookbook
  3. Dedication
  4. About Packt
    1. Why subscribe?
    2. Packt.com
  5. Contributors
    1. About the authors
    2. About the reviewers
    3. Packt is searching for authors like you
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Sections
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    5. Get in touch
      1. Reviews
  7. Introducing PowerShell Core
    1. Introduction
      1. A brief note on .NET Core
      2. The assumptions
      3. How to read this book
      4. An important note
      5. Writing the cmdlets (Linux admins note)
      6. Off we go
    2. Installing PowerShell
      1. Getting ready
      2. How to do it...
        1. Installation on Ubuntu
          1. Installing from the repository
          2. Installing via direct download
        2. Installation on CentOS
          1. Installing from the repository
          2. Installing via direct download
        3. Using the Snapcraft package
        4. Using the binary archives
        5. Bonus– using the Install-PowerShell script (including on macOS)
      3. How it works...
      4. See also
    3. Updating and using Help
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    4. Exploring about_ topics
      1. Getting ready
      2. How to do it...
      3. How it works...
    5. Discovering cmdlets
      1. How to do it...
      2. How it works...
    6. Finding and installing PowerShell modules
      1. How to do it...
      2. How it works...
      3. There's more...
      4. See also
    7. Listing the various providers in PowerShell
      1. How to do it...
      2. How it works...
      3. See also
    8. Understanding objects
      1. How to do it...
      2. How it works...
      3. There's more...
      4. See also
    9. Parsing input from text to object
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    10. Comparing the outputs of Bash and PowerShell
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    11. Comparing Windows PowerShell and PowerShell Core
      1. Support for snap-ins
      2. Convenience aliases
      3. PowerShell Workflows
      4. PowerShell Desired State Configuration
    12. Working with aliases
      1. How to do it...
      2. How it works...
      3. There's more...
      4. See also
    13. Dissecting a .NET Core object
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    14. Listing out the execution policies and setting a suitable one
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
  8. Preparing for Administration Using PowerShell
    1. Introduction
    2. Installing Visual Studio Code
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    3. Configuring automatic variables
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Changing shell behavior using variables
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    5. Enabling automated execution of commands for each load
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    6. Customizing the Terminal prompt
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    7. Understanding standard redirection in PowerShell
      1. How to do it...
      2. How it works...
        1. Out-File
        2. Tee-Object
        3. Accepting input from a file
      3. There's more...
      4. See also
    8. Calling native Linux commands from PowerShell
      1. Getting started
      2. How to do it...
      3. How it works...
      4. See also
    9. Understanding cmdlets and parameters
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    10. Running cmdlets with minimal keystrokes
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    11. Finding parameter aliases
      1. Getting ready
      2. How to do it...
      3. How it works...
    12. Calling a PowerShell script
      1. Getting ready
      2. How to do it...
      3. How it works...
    13. Dot-sourcing a PowerShell script
      1. How to do it...
      2. How it works...
      3. There's more...
    14. Calling a PowerShell cmdlet from outside of PowerShell
      1. How to do it...
      2. How it works...
    15. Recording the cmdlets run on the PowerShell console
      1. How to do it...
      2. How it works...
  9. First Steps in Administration Using PowerShell
    1. Introduction
    2. Working with date properties
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    3. Working with date and time methods
      1. How to do it...
      2. How it works...
    4. Working with currently-running processes to measure resource consumption
      1. How to do it...
      2. How it works...
    5. Launching and stopping a process
      1. Getting ready
      2. How to do it...
      3. How it works...
    6. Finding the owner of a process
      1. How to do it...
      2. How it works...
      3. There's more...
      4. See also
    7. Invoking an application based on the file type
      1. How to do it...
      2. How it works...
    8. Installing the CronTab PowerShell module
      1. How to do it...
      2. How it works...
    9. Scheduling jobs in PowerShell
      1. Getting ready
      2. How to do it...
      3. How it works...
    10. Removing scheduled jobs in PowerShell
      1. How to do it...
      2. How it works...
      3. See also
  10. Passing Data through the Pipeline
    1. Introduction
    2. Selecting columns from the output
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    3. Limiting the number of output objects
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Expanding the properties within properties
      1. How to do it...
      2. How it works...
    5. Filtering objects
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    6. Grouping the output
      1. How to do it...
      2. How it works...
      3. There's more...
      4. See also
    7. Sorting the output
      1. How to do it...
      2. How it works...
    8. Taking actions on the returned objects
      1. Getting ready
      2. How to do it...
      3. How it works...
    9. Understanding pipeline-enabled parameters
      1. How to do it...
      2. How it works...
    10. Importing content into PowerShell
      1. How to do it...
      2. How it works...
  11. Using Variables and Objects
    1. Introduction
    2. Using environment variables
      1. How to do it...
      2. How it works...
      3. There's more...
      4. See also
    3. Storing the output of an instance of a .NET Core object
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    4. Adding custom properties to an object
      1. How to do it...
      2. How it works...
      3. See also
    5. Creating a custom object from a returned object
      1. How to do it...
      2. How it works...
      3. See also
    6. Understanding the extension of type data
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    7. Retaining object modifications across sessions
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    8. Removing custom type data
      1. How to do it...
      2. How it works...
      3. See also
    9. Understanding variables
  12. Working with Strings
    1. Introduction
    2. Creating a literal string
      1. How to do it...
      2. How it works...
    3. Creating an expanding string
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Understanding Here-Strings
      1. How to do it...
      2. How it works...
    5. Using basic string methods
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    6. Performing string matching operations
      1. How to do it...
      2. How it works...
      3. There's more...
    7. Replacing substrings within strings
      1. How to do it...
      2. How it works...
      3. There's more...
    8. Splitting and combining strings
      1. How to do it...
      2. How it works...
    9. Using the formatting operator
      1. How to do it...
      2. How it works...
      3. There's more...
  13. Flow Control Using Branches and Loops
    1. Introduction
    2. Using If–ElseIf–Else conditions to control script flow
      1. Getting ready
      2. How to do it...
      3. How it works...
    3. Using Switch–Case conditions to control script flow
      1. How to do it...
      2. How it works...
    4. Learning to use delays
      1. How to do it...
      2. How it works...
    5. Writing a basic looping construct
      1. How to do it...
      2. How it works...
    6. Writing a more complex loop on a predefined array
      1. How to do it...
      2. How it works...
    7. Using the For loop construct
      1. How to do it...
      2. How it works...
    8. Using the While loop construct
      1. How to do it...
      2. How it works...
    9. Cleaning empty directories using the Do–While construct
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    10. Cleaning empty directories using the Do–Until construct
      1. Getting ready
      2. How to do it...
      3. How it works...
  14. Performing Calculations
    1. Introduction
    2. Performing arithmetic operations
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    3. Performing calculations on the output
      1. Getting ready
      2. How to do it
      3. How it works...
      4. There's more...
    4. Working with administrative constants
      1. Getting ready
      2. How to do it...
      3. How it works...
    5. Working with calculated properties
      1. Getting ready
      2. How to do it...
      3. How it works...
    6. Working with binary numbers
      1. How to do it...
      2. How it works...
    7. Performing base conversions
      1. How to do it...
      2. How it works...
      3. There's more...
  15. Using Arrays and Hashtables
    1. Introduction
    2. Creating and working with a simple array
      1. How to do it...
      2. How it works...
      3. There's more...
    3. Accessing and manipulating array items
      1. How to do it...
      2. How it works...
      3. There's more...
      4. See also
    4. Sorting an array
      1. How to do it...
      2. How it works...
      3. There's more...
      4. See also
    5. Searching for an item in an array
      1. How to do it
      2. How it works...
      3. See also
    6. Combining arrays
      1. How to do it...
      2. How it works...
    7. Item matching in an array
      1. How to do it...
      2. How it works...
      3. There's more...
    8. Removing elements from an array
      1. How to do it...
      2. How it works...
    9. Comparing arrays
      1. How to do it...
      2. How it works...
    10. Creating a simple hashtable
      1. How to do it...
      2. How it works...
    11. Performing simple tasks on a hashtable
      1. How to do it...
      2. How it works...
  16. Handling Files and Directories
    1. Introduction
    2. Reading content from a file
      1. How to do it...
      2. How it works...
      3. See also
    3. Sending output to a file
      1. Getting ready...
      2. How to do it...
      3. How it works...
      4. See also
    4. Adding and setting content to a file
      1. Getting ready
      2. How to do it...
      3. How it works...
    5. Searching for content
      1. Getting ready
      2. How to do it...
      3. How it works...
    6. Working with locations
      1. How to do it...
      2. How it works...
      3. See also
    7. Working with files and directories
      1. How to do it...
      2. How it works...
      3. There's more...
    8. Listing files and directories
      1. How to do it...
      2. How it works...
      3. There's more...
    9. Working with structured and unstructured files
      1. How to do it...
      2. How it works...
      3. There's more...
  17. Building Scripts and Functions
    1. Introduction
    2. Writing a simple script
      1. How to do it...
      2. How it works...
    3. Reading input from the host
      1. How to do it...
      2. How it works...
    4. Displaying an interactive menu
      1. How to do it...
      2. How it works...
      3. See also
    5. Showing progress of execution
      1. How to do it...
      2. How it works...
      3. See also
    6. Defining arguments for a script
      1. How to do it...
      2. How it works...
    7. Writing a simple function
      1. How to do it...
      2. How it works...
      3. See also
    8. Working with a script block
      1. How to do it...
      2. How it works...
    9. Measuring running duration
      1. How to do it...
      2. How it works...
      3. See also
    10. Important points to remember
      1. Using Host cmdlets
      2. Returning objects from functions
      3. Naming functions
  18. Advanced Concepts of Functions
    1. Introduction
    2. Defining parameters
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    3. Working with parameter aliases
      1. How to do it...
      2. How it works...
    4. Working with parameter sets
      1. How to do it...
      2. How it works...
    5. Adding default values to the parameter
      1. How to do it...
      2. How it works...
    6. Adding validation to parameter input
      1. How to do it...
      2. How it works...
    7. Handling dependencies and prerequisites
      1. How to do it...
      2. How it works...
    8. Adding safety switches to functions
      1. How to do it...
      2. How it works...
    9. Adding help to functions
      1. How to do it...
      2. How it works...
    10. Adding support for pipeline input
      1. How to do it...
      2. How it works...
      3. There's more...
    11. Using the Main function
      1. How to do it...
      2. How it works...
    12. Writing a script module
      1. How to do it...
      2. How it works...
      3. See also
    13. Handling module cleanup
      1. How to do it...
      2. How it works...
  19. Debugging and Error Handling
    1. Introduction
    2. The Debug tab of Visual Studio Code
    3. Writing debug output
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Running a script in Debug mode
      1. How to do it...
      2. How it works...
    5. Tracing errors
      1. How to do it...
      2. How it works...
    6. Using breakpoints
      1. How to do it...
      2. How it works...
    7. Using conditional breakpoints
      1. How to do it...
      2. How it works...
    8. Using exit codes
      1. How to do it...
      2. How it works...
    9. Working with the Error variable
      1. How to do it...
      2. How it works...
    10. Controlling flow based on errors
      1. How to do it...
      2. How it works...
      3. See also
    11. Sending verbose output to a file
      1. How to do it...
      2. How it works...
  20. Enterprise Administration Using PowerShell
    1. Introduction to OpenSSH
    2. Installing OpenSSH on Windows
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    3. Configuring OpenSSH using sshd_config on Windows
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    4. Installing and configuring OpenSSH on Linux
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    5. Testing PowerShell remoting using OpenSSH
      1. Getting ready
      2. How to do it...
      3. How it works...
    6. Testing PowerShell remoting with OpenSSH using keys between Linux machines
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    7. Testing PowerShell remoting with OpenSSH using keys between Linux and Windows machines
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    8. Running cmdlets on remote computers
      1. Getting ready
      2. How to do it...
      3. How it works...
    9. Managing the administration of remote machines with PowerShell Jobs
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    10. Generating HTML reports
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    11. Sending email messages
      1. Getting ready
      2. How to do it...
      3. How it works...
  21. PowerShell and Cloud Operations
    1. Introduction to PowerShell for Cloud operations on Azure and AWS
    2. Installation of the AzureRM.Netcore PowerShell module on Linux machines
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    3. Installation of the AWSPowerShell.NetCore module on Linux machines
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    4. Discovering AzureRM cmdlets discovery functions
      1. How to do it...
      2. How it works...
    5. Azure Cloud Shell in action
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    6. Provisioning a Ubuntu 18.04 LTS VM using AzureRM.Netcore cmdlets on the Azure Cloud
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    7. Provisioning Linux Docker containers using Docker Machine on Azure
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    8. Provisioning Linux VM using PowerShell script
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    9. Performing management tasks using ARM PowerShell cmdlets
      1. Getting ready
      2. How to do it...
      3. How it works...
    10. PowerShell remoting to Azure VM using OpenSSH
      1. Getting ready
      2. How to do it...
      3. How it works...
    11. Managing AWS credentials
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    12. Connecting an EC2 Linux instance using SSH from PowerShell
      1. Getting ready
      2. How to do it...
      3. How it works...
  22. Using PowerShell for SQL Database Management
    1. Introduction to PowerShell, SQL Server, and Python
    2. Technical requirements
    3. Installing .Net Core on Linux
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    4. Installing the SqlServer module
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    5. Overview of SQL Server SMO
      1. How to do it...
      2. See also
    6. Running cross-platform T-SQL queries
      1. Getting ready
      2. How to do it...
      3. How it works...
    7. Running cross-platform T-SQL queries on multiple servers
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    8. Creating a repository for Get-Process in the SQL database
      1. Getting ready
      2. How to do it...
      3. How it works...
    9. Data formatting examples with PowerShell
      1. Getting ready
      2. How to do it...
      3. How it works...
    10. Overview of PowerShell, Bash, and Python integration
      1. Getting ready
      2. How to do it...
      3. How it works...
  23. Using PowerShell with Docker
    1. Introduction to Docker
    2. Installation of Docker on Linux
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    3. Getting and listing the Docker management cmdlets
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    4. Overview of Docker Hub and its operations
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    5. Building an image from a Dockerfile using PowerShell
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    6. Pulling a Docker container image from the repository
      1. Getting ready
      2. How to do it...
      3. How it works...
    7. Listing Docker containers and the images
      1. How to do it...
      2. How it works...
    8. Starting and stopping Docker containers
      1. Getting ready
      2. How to do it...
      3. How it works...
    9. Removing containers and container images
      1. How to do it...
      2. How it works...
    10. Working with a container using miscellaneous Docker PowerShell cmdlets
      1. How to do it...
      2. How it works...
      3. See also
    11. Running a PowerShell script to set up Docker containers
      1. How to do it...
      2. How it works...
    12. Finding Docker tags using PowerShell cmdlets from the remote registry
      1. How to do it...
      2. How it works...
      3. See also
  24. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: PowerShell Core for Linux Administrators Cookbook
  • Author(s): Prashanth Jayaram, Ram Iyer
  • Release date: November 2018
  • Publisher(s): Packt Publishing
  • ISBN: 9781789137231