36,59 €
Make use of hands-on recipes for many tasks that are typically encountered in both the on-premises as well as the cloud world.
Key Features
Book Description
This book will follow a recipe-based approach and start off with an introduction to the fundamentals of PowerShell, and explaining how to install and run it through simple examples. Next, you will learn how to use PowerShell to access and manipulate data and how to work with different streams as well. You will also explore the object model which will help with regard to PowerShell function deployment. Going forward, you will get familiar with the pipeline in its different use cases. The next set of chapters will deal with the different ways of accessing data in PowerShell. You will also learn to automate various tasks in Windows and Linux using PowerShell Core, as well as explore Windows Server. Later, you will be introduced to Remoting in PowerShell Core and Just Enough Administration concept. The last set of chapters will help you understand the management of a private and public cloud with PowerShell Core. You will also learn how to access web services and explore the high-performance scripting methods.
By the end of this book, you will gain the skills to manage complex tasks effectively along with increasing the performance of your environment.
What you will learn
Who this book is for
This book will be for windows administrators who want to enhance their PowerShell scripting skills to the next level. System administrators wanting to automate common to complex tasks with PowerShell scripts would benefit from this book. Prior understanding on PowerShell would be necessary.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 353
Copyright © 2019 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 or its dealers and distributors, will be held liable for any damages caused or alleged to have been 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.
Commissioning Editor: Vijin BorichaAcquisition Editor: Rohit RajkumarContent Development Editor: Ronn KurienTechnical Editor: Prachi SawantCopy Editor:Safis EditingProject Coordinator: Jagdish PrabhuProofreader: Safis EditingIndexer: Priyanka DhadkeGraphics: Tom ScariaProduction Coordinator: Saili Kale
First published: April 2019
Production reference: 1170419
Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK.
ISBN 978-1-78980-330-3
www.packtpub.com
Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website.
Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals
Improve your learning with Skill Plans built especially for you
Get a free eBook or video every month
Mapt is fully searchable
Copy and paste, print, and bookmark content
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.packt.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details.
At www.packt.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.
Jan-Hendrik Peters is an automation and DevOps professional by day, and a developer for the AutomatedLab framework by night. After working at an international retailer automating distributed POS support systems, he started working as a premier field engineer for Microsoft Germany, where he helps customers automate their infrastructure on-premises and in the cloud.
When he is not working, he likes to spend his time brewing his own beer, curing his own bacon, and generally doing manual labor.
Jose Angel Muñoz is a systems engineer with more than 20 years' experience in Linux and Windows systems administration. He is currently working for RAET, where he has discovered the magic of automation and infrastructure as code. Jose Angel also collaborates with Ansible, creating new Python and PowerShell modules.
Previously, he collaborated with the Linux Magazine, where he published several technical articles on a variety of topics, including monitoring, security, virtualization, and development.
Friedrich Weinmann, a former Microsoft MVP for his engagement in the PowerShell community nowadays works as a Premier Field Engineer for Microsoft.
He speaks at international conferences on all things PowerShell, presents at usergroups and organizes a PowerShell usergroup himself. In his putatively spare time he contributes to open source projects and can be found hanging out in various PowerShell related communities.
If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.
Title Page
Copyright and Credits
Powershell Core 6.2 Cookbook
About Packt
Why subscribe?
Packt.com
Contributors
About the author
About the reviewers
Packt is searching for authors like you
Preface
Who this book is for
What this book covers
To get the most out of this book
Lab requirements
Recipe recommendations
Download the example code files
Download the color images
Conventions used
Sections
Getting ready
How to do it…
How it works…
There's more…
See also
Get in touch
Reviews
Introduction to PowerShell Core
Technical requirements
Installing PowerShell Core on Windows
Getting ready
How to do it...
How it works...
There's more...
See also
Installing PowerShell Core on Linux
Getting ready
How to do it...
How it works...
There's more...
See also
Running PowerShell Core
Getting ready
How to do it...
How it works...
There's more...
See also
Getting help
Getting ready
How to do it...
How it works...
There's more...
See also
Getting around
Getting ready
How to do it...
How it works...
There's more...
See also
How do cmdlets work?
Getting ready
How to do it...
How it works...
Performing read-only operations
Getting ready
How to do it...
How it works...
There's more...
Introducing change to systems
Getting ready
How to do it...
How it works...
There's more...
See also
Discovering the environment
Getting ready
How to do it...
How it works...
There's more...
Reading and Writing Output
Technical requirements
Working with output
Getting ready
How to do it...
How it works...
See also
Storing data
How to do it...
How it works...
Read-only and constant variables
How to do it...
How it works...
There's more...
Variable scoping
How to do it...
How it works...
There's more...
The six streams
How to do it...
How it works...
There's more...
Stream redirection
How to do it...
How it works...
Working with the filesystem provider
Getting ready
How to do it...
How it works...
Working with the registry provider
How to do it...
How it works...
There's more...
Working with the certificate provider
How to do it...
How it works...
Creating your own provider
How to do it...
How it works...
There's more...
See also
NTFS iternate data streams
How to do it...
How it works...
There's more...
Working with Objects
Technical requirements
Introduction to Object-Oriented Programming
Getting ready
How to do it...
How it works...
See also
Exploring object properties
Getting ready
How to do it...
How it works...
There's more...
See also
Exploring object methods
Getting ready
How to do it...
How it works...
There's more...
Using static class members
Getting ready
How to do it...
How it works...
There's more...
Creating classes
Getting ready
How to do it...
How it works...
There's more...
See also
Using custom objects
Getting ready
How to do it...
How it works...
See also
Manipulating and extending objects
Getting ready
How to do it...
How it works...
There's more...
See also
[ref], [out], and delegates
Getting ready
How to do it...
How it works...
See also
Mastering the Pipeline
Technical requirements
What is a pipeline?
Getting ready
How to do it...
How it works...
There's more...
See also
Sorting data efficiently
How to do it...
How it works...
See also
Selecting subsets of data
How to do it...
How it works...
Grouping large data sets
Getting ready
How to do it...
How it works...
Filtering data
Getting ready
How to do it...
How it works...
Processing data
Getting ready
How to do it...
How it works...
Enabling pipeline input
Getting ready
How to do it...
How it works...
High-performance pipelines
Getting ready
How to do it...
How it works...
There's more...
Importing, Using, and Exporting Data
Technical requirements
Working with CSV
Getting ready
How to do it...
How it works...
Making objects transportable with XML
Getting ready
How to do it...
How it works...
Modern data structures – JSON and YAML
Getting ready
How to do it...
How it works...
There's more...
See also
Interacting with RESTful APIs
Getting ready
How to do it...
How it works...
Interacting with MS SQL
Getting ready
How to do it...
How it works...
See also
Interacting with PostgreSQL and other database engines
Getting ready
How to do it...
How it works...
Encrypting data
Getting ready
How to do it...
How it works...
Windows and Linux Administration
Technical requirements
Enabling a Windows feature
Getting ready
How to do it...
How it works...
See also
Installing a Linux package group
Getting ready
How to do it...
How it works...
Creating Windows accounts
Getting ready
How to do it...
How it works...
Creating Linux accounts
Getting ready
How to do it...
How it works...
Modifying the Windows Registry
Getting ready
How to do it...
How it works...
Modifying Linux configuration files
Getting ready
How to do it...
How it works...
Registering a new Windows service
Getting ready
How to do it...
How it works...
There's more...
Enabling a new Linux daemon
Getting ready
How to do it...
How it works...
Planning a Windows scheduled task
Getting ready
How to do it...
How it works...
Planning a Linux Cron Job
Getting ready
How to do it...
How it works...
Creating a Windows storage pool
Getting ready
How to do it...
How it works...
See also
Creating a Linux software RAID device
Getting ready
How to do it...
How it works...
Accessing the Windows event log
Getting ready
How to do it...
How it works...
See also
Working with the system logs in Linux
Getting ready
How to do it...
How it works...
Windows Server Administration
Technical requirements
File services management
Getting ready
How to do it...
How it works...
Domain services management
Getting ready
How to do it...
How it works...
See also
IIS management – deploying and configuring
Getting ready
How to do it...
How it works...
IIS management – creating a website
Getting ready
How to do it...
How it works...
Configuring Storage Spaces Direct (S2D)
Getting ready
How to do it...
How it works...
Using Docker with Hyper-V containers
Getting ready
How to do it...
How it works...
Remote Desktop Services (RDS) deployment
Getting ready
How to do it...
How it works...
Integrating DSC configuration into a Windows Server image
Getting ready
How to do it...
How it works...
Remoting and Just Enough Administration
Technical requirements
Enabling PowerShell remoting on Windows
Getting ready
How to do it...
How it works...
Enabling PowerShell remoting on Linux
Getting ready
How to do it...
How it works...
Configuring PowerShell remoting
Getting ready
How to do it...
How it works...
See also
Securely connecting to remote endpoints
Getting ready
How to do it...
How it works...
See also
Remotely retrieving data
Getting ready
How to do it...
How it works...
Remote script debugging
Getting ready
How to do it...
How it works...
See also
Creating a JEA role
Getting ready
How to do it...
How it works...
Creating a JEA session configuration
Getting ready
How to do it...
How it works...
Connecting to endpoints using different session configuration
Getting ready
How to do it...
How it works...
Using the Common Information Model (CIM)
Getting ready
How to do it...
How it works...
Using PowerShell Direct in PowerShell Core
Getting ready
How to do it...
How it works...
Using PowerShell for Hyper-V and Azure Stack Management
Technical requirements
Setting up Hyper-V
Getting ready
How to do it...
How it works...
There's more...
Setting up Azure Stack
Getting ready
How to do it...
How it works...
Virtual networks in Hyper-V
Getting ready
How to do it...
How it works...
Virtual networks in Azure Stack
Getting ready
How to do it...
How it works...
Provisioning storage in Hyper-V
Getting ready
How to do it...
How it works...
Provisioning storage in Azure Stack
Getting ready
How to do it...
How it works...
See also
Provisioning compute resources in Hyper-V
Getting ready
How to do it...
How it works...
Provisioning compute resources in Azure Stack
Getting ready
How to do it...
How it works...
See also
Creating a compute cluster with DSC
Getting ready
How to do it...
How it works...
Configuring guests with DSC on Hyper-V
Getting ready
How to do it...
How it works...
Configuring guests with DSC on Azure Stack
Getting ready
How to do it...
How it works...
Using PowerShell with Azure and Google Cloud
Technical requirements
Connecting to Azure
Getting ready
How to do it...
How it works...
Connecting to Google
Getting ready
How to do it...
How it works...
Exploring Azure resource groups
Getting ready
How to do it...
How it works...
See also
Exploring Google Cloud projects
Getting ready
How to do it...
How it works...
See also
Deploying resource group templates
Getting ready
How to do it...
How it works...
See also
Deploying Google Cloud templates
Getting ready
How to do it...
How it works...
Using the Azure Container Registry
Getting ready
How to do it...
How it works...
Using the Google Cloud Container Registry
Getting ready
How to do it...
How it works...
Containers on Azure
Getting ready
How to do it...
How it works...
Containers on Google Cloud
Getting ready
How to do it...
How it works...
There's more...
SQL on Azure
Getting ready
How to do it...
How it works...
SQL on Google Cloud
Getting ready
How to do it...
How it works...
Accessing Web Services
Technical requirements
Exploring REST services
Getting ready
How to do it...
How it works...
Accessing Azure DevOps via REST
Getting ready
How to do it...
How it works...
See also
Creating new Azure resource groups via REST
Getting ready
How to do it...
How it works...
Calling Azure runbooks via webhooks
Getting ready
How to do it...
How it works...
Providing a RESTful web service through PowerShell
Getting ready
How to do it...
How it works...
There's more...
See also
Creating a new team project in Azure DevOps
Getting ready
How to do it...
How it works...
Creating a new build definition in Azure DevOps
Getting ready
How to do it...
How it works...
High-Performance Scripting
Technical requirements
Parallelization with background jobs
Getting ready
How to do it...
How it works...
Efficiently querying the event log
Getting ready
How to do it...
How it works...
See also
Comparing Where-Object and the Where method
Getting ready
How to do it...
How it works...
Using Active Directory cmdlets with performance in mind
Getting ready
How to do it...
How it works...
See also
Parallelization through runspace pools
Getting ready
How to do it...
How it works...
Exchanging variables with runspaces
Getting ready
How to do it...
How it works...
See also
Large-scale IO operations
Getting ready
How to do it...
How it works...
See also
Hashtables, dictionaries, and performance
Getting ready
How to do it...
How it works...
Other Books You May Enjoy
Leave a review - let other readers know what you think
This PowerShell Core cookbook is a recipe-based approach in terms of learning about, and using, PowerShell Core in your organization. Whether you are using Windows, Linux, or macOS, PowerShell Core can be installed and used.
This book aims to show you typical infrastructure administration scenarios by means of short and concise recipes. You will explore everything from automating a release pipeline to managing Linux systems with PowerShell in the upcoming chapters.
With the release of PowerShell 7 that is slated for May 2019 this book will still be accurate and greatly help you with day to day tasks.
This book is intended for system administrators who want to enhance their PowerShell scripting skills. Do you like to automate reoccurring tasks? Then this book is perfect for you. Do you manage Windows as well as Linux systems, but you are missing a common automation language? Then look no further. Do you want to work more with REST APIs but don't know where to start? Read this book.
To get the most out of this book, you should already be familiar with the basic terms of PowerShell. If necessary, read the book Learn PowerShell Core, by David das Neves and Jan-Hendrik Peters.
Chapter 1, Introduction to PowerShell Core, serves as an introduction and covers the very first steps with PowerShell Core.
Chapter 2, Reading and Writing Output, covers how to work with data and providers.
Chapter 3, Working with Objects, is an introduction to object-oriented programming and also shows you the benefits of using objects in your code.
Chapter 4, Mastering the Pipeline, is all about the pipeline and shows you how to work efficiently with large amounts of data in the pipeline.
Chapter 5, Importing, Using, and Exporting Data, is all about persisting and reading data from XML to JSON, and from REST to SQL.
Chapter 6, Windows and Linux Administration, compares different typical administrative tasks on Linux and Windows and demonstrates the benefits of PowerShell Core in these scenarios.
Chapter 7, Windows Server Administration, covers the pitfalls and the benefits of using PowerShell Core in a purely Windows environment. You will configure S2D, Docker, Active Directory Domain Services, and more.
Chapter 8, Remoting and Just Enough Administration, introduces remote debugging, Just Enough Administration, and the common information model.
Chapter 9, Using PowerShell for Hyper-V and Azure Stack Management, concentrates on the private cloud and shows you how to manage Hyper-V, deploy Azure Stack, and maintain your infrastructure workloads with PowerShell Core.
Chapter 10, Using PowerShell with Azure and Google Cloud, gives you two public cloud providers and compares the typical workloads, including storage and compute.
Chapter 11, Accessing Web Services, is all about REST. This chapter will not only show you how different actual REST APIs are accessed; it will also have you create your own!
Chapter 12, High-Performance Scripting, covers all the important concepts concerning the performance of your scripts. You will learn about true parallelization, efficient filtering, and more.
To get the most out of this book, I recommend deploying this book's lab environment, which consists of the following:
A Domain Controller/Router/Certificate Authority (recipes requiring Active Directory)
Three file servers (SOFS and S2D recipes)
Two Hyper-V hosts (Hyper-V and clustering recipes)
A web server (IIS recipes) with SQL
A CentOS 7.4 node (Linux recipes)
Should you decide to use your own infrastructure, please adapt the server names used in the recipes!
To deploy the lab for the entire book, you need a Hyper-V host (recommended) or an Azure subscription (caution: deploying the lab will incur substantial costs if you do not deallocate the machines!). The requirements for your Hyper-V host are as follows:
Windows 10/Server 2016
A virtualization-enabled, modern processor (i5 or similar)
At least 20 GiB RAM if all machines are up and running
At least 50 GiB of free space on an SSD (no spinning disks)
Deploy the lab using Windows PowerShell
The Hyper-V feature needs to be enabled (requires a restart)
One ISO file Windows Server 2016 Datacenter
One ISO file SQL Server 2017 (Standard or Datacenter)
One ISO file CentOS 7.4; a DVD image is recommended, since a network image requires an internet-connected host!
Deploying the lab will download the PowerShell module, AutomatedLab, which is used to aid rapid lab deployments. The entire lab will be built on the fly. If you are done with the lab and want to remove it, open an administrative PowerShell session and execute Remove-Lab -Name PSCookBook.
The lab deployment script can be found in the book's repository at https://github.com/PacktPublishing/Powershell-Core-6.2-Cookbook/tree/master/Chapter00.
To follow all the recipes in this book, I recommend the following:
Register for a free Azure DevOps account at
https://dev.azure.com
to follow the Azure DevOps recipes.
Register for a free Microsoft Azure account at
https://azure.microsoft.com/en-us/free
to follow all Azure recipes.
Register for a free Google Cloud account at
https://cloud.google.com
to follow all Google Cloud recipes.
Any code can be found at https://github.com/PacktPublishing/Powershell-Core-6.2-Cookbook.
Take care not to execute the scripts as a whole. I have added a throw statement to each recipe script so that you do not accidentally execute it entirely.
You can download the example code files for this book from your account at www.packt.com. If you purchased this book elsewhere, you can visit www.packt.com/support and register to have the files emailed directly to you.
You can download the code files by following these steps:
Log in or register at
www.packt.com
.
Select the
SUPPORT
tab.
Click on
Code Downloads & Errata
.
Enter the name of the book in the
Search
box and follow the onscreen instructions.
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
WinRAR/7-Zip for Windows
Zipeg/iZip/UnRarX for Mac
7-Zip/PeaZip for Linux
The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Powershell-Core-6.2-Cookbook. In case there's an update to the code, it will be updated on the existing GitHub repository.
We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://www.packtpub.com/sites/default/files/downloads/9781789803303_ColorImages.pdf.
In this book, you will find several headings that appear frequently (Getting ready, How to do it..., How it works..., There's more..., and See also).
To give clear instructions on how to complete a recipe, use these sections as follows:
This section tells you what to expect in the recipe and describes how to set up any software or any preliminary settings required for the recipe.
This section contains the steps required to follow the recipe.
This section usually consists of a detailed explanation of what happened in the previous section.
This section consists of additional information about the recipe in order to make you more knowledgeable about the recipe.
This section provides helpful links to other useful information for the recipe.
Feedback from our readers is always welcome.
General feedback: If you have questions about any aspect of this book, mention the book title in the subject of your message and email us at [email protected].
Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packt.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.
Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.
If you are interested in becoming an author: If there is a topic that you have expertise in, and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.
Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!
For more information about Packt, please visit packt.com.
PowerShell Core, as the open source alternative to Windows PowerShell from which it evolved, is quickly becoming the automation engine of choice for many administrators and developers alike. As a true cross-platform shell, PowerShell Core is perfectly suited for many different operating system types and workloads.
While the next iteration of PowerShell Core at the time of writing was slated for May 2019 and will drop the Core this book and its recipes will remain accurate and useful. The release of PowerShell is merely a rebranding to unify PowerShell development. In the future we will continue to see rapid new iterations of PowerShell.
While installing PowerShell Core is a breeze, the first steps for people new to PowerShell can be quite challenging. In this chapter, I aim to bridge those little knowledge gaps and show you the ropes.
You'll learn all there is to know about installing and operating PowerShell Core and learn about the cmdlets necessary to find your way on any system running PowerShell Core. In addition to that, you'll discover how to get help without using the internet and do everything from within PowerShell.
This chapter is intended for beginners who are fairly new to PowerShell Core and will help you understand the very basics of PowerShell while the next chapters assume solid scripting knowledge.
In this chapter, we will cover the following recipes:
Installing PowerShell Core on Windows
Installing PowerShell Core on Linux
Running PowerShell Core
Getting help
Getting around
How do cmdlets work?
Performing read-only operations
Introducing change to systems
Discovering the environment
In order to follow the recipes in this chapter, you need a machine capable of running PowerShell Core that is connected to the internet.
In this recipe, you'll learn how to provision PowerShell Core on a Windows system starting with Windows 6.1 (Server 2008 R2/Windows 7).
To follow this recipe, you'll need a Windows machine with at least Windows Server 2008 R2 or Windows 7. If this machine isn't connected to the internet, you'll need a way of transferring the installer to the machine.
Please perform the following steps:
In order to get the most recent release of PowerShell Core for Windows, browse to
https://github.com/powershell/powershell
:
Download the release for your platform. I recommend using the stable 64 bit (x64) edition if possible.
Open the installer file, for example,
PowerShell-6.2.0-win-x64.msi
.
Follow the instructions on screen to install PowerShell Core
On the final page, you can directly enable PowerShell remoting if you want. Leave the option disabled for now; you'll configure it properly later on:
Start PowerShell Core by typing
pwsh
into the search bar!
Using the standard MSI installer methods, PowerShell Core will be installed for your system in the 64 bit Program Files directory by default. It won't replace Windows PowerShell but will simply coexist peacefully and include the most recent updates to PowerShell.
If left on the default settings, an event manifest will be registered on the system, enabling an event log for PowerShell Core. The log file will be placed at %SystemRoot%\System32\Winevt\Logs\PowerShellCore%4Operational.evtx and can be found in the Applications and Services Logs.
By default, no remoting configuration will be made. We'll talk about remoting in Chapter 8, Running Remote Commands and Understanding Just Enough Administration, where you'll enable PowerShell remoting for a system.
PowerShell documentation:
https://docs.microsoft.com/powershell
Chocolatey documentation:
https://chocolatey.org/docs/installation
Since the arrival of PowerShell Core, its key feature has been the ability to run cross-platform and provide the exact same experience on any operating system. Installing PowerShell on Linux is nearly as easy as it is in Windows. If your distribution is among the list of supported distributions such as CentOS, openSUSE, or Ubuntu, the process is pretty straightforward.
In order to follow the recipe, you'll need any Linux distribution (even the Windows Subsystem for Linux) that's preferably connected to the internet and can download packages. In the recipe, I'm using CentOS and Ubuntu to show some very different approaches.
At the time of writing, the recipe was correct. However, check whether it still applies on the official installation page for your operating system, for example, https://docs.microsoft.com/en-us/powershell/scripting/setup/installing-powershell-core-on-linux.
On CentOS 7.x, perform the following steps:
Register the Microsoft RPM repository:
curl https://packages.microsoft.com/config/rhel/7/prod.repo | sudo tee /etc/yum.repos.d/microsoft.repo.
Install the package:
sudo yum install -y powershell
.
Run PowerShell by executing
pwsh
, which is now installed.
On Ubuntu 18.10, perform the following steps:
Open the Ubuntu Software store.
Search for
powershell
:
Install and run PowerShell Core:
The steps to install PowerShell on other distributions are fairly similar as long as you can use your distribution's package management system, such as rpm or dpkg.
Usually, the installation instructions for Linux require the registration of a package repository that's maintained by Microsoft and used to publish PowerShell Core. The repository settings include the URL, a reference to the GPG public key, and the status of the repository.
On Linux, binary packages are usually compiled using a makefile on the running OS and then, for example, linked or copied to one of the binary paths. The best example for this is probably Gentoo, where compiling your kernel and all components, libraries, and software is actually required. With RPM and DEB packages, developers can better resolve dependencies and include all necessary instructions to install a binary package or compile a source package.
With binary packages, the component is compiled for a specific architecture with general compilation flags set. While this won't allow the user to fine-tune every part of the installation, it'll provide the benefit of an easier deployment.
PowerShell comes pre-built in, for example, an RPM package for different OS architectures. By using the package management provider of the distribution, you ensure that all necessary dependencies are installed alongside the package itself.
With Ubuntu 18.10, PowerShell is available as a Snap package in the Ubuntu Software store. This allows a more user-friendly installation of PowerShell that doesn't require the command line at all—apart from using PowerShell, of course.
There're many different flavors of Linux—there is macOS, Windows, and probably other platforms to come. Stay up-to-date by having a look at the official installation instructions at https://github.com/powershell/powershell.
In addition to traditional installation methods, you can also build your entire PowerShell from scratch.
Information on the Snap package format:
https://snapcraft.io
The official package sour
ce for PowerShell packages:
https://github.com/powershell/powershell
Using PowerShell Core is very simple. This recipe will show you the very first steps and help you to run PowerShell Core after the installation.
In order to follow this recipe, you should have completed the installation of PowerShell Core for your operating system.
Let's perform the following steps:
On Windows, run
pwsh.exe
. On Linux or macOS, run
pwsh
.
Type your first cmdlet,
Get-Process
, to retrieve a list of running processes on the system and hit
Enter
to confirm.
Compare the output of the cmdlet with the output of
tasklist
(in Windows) and
ps
(in Linux):
Type
Get-Date
and hit
Enter
to confirm.
Compare the output of this cmdlet with the output of
date /t
(in Windows) and
date
(in Linux):
Execute the line:
Get-Process | Where-Object -Property WorkingSet -gt 100MB
.
Compare the output again with the output of
tasklist /FI "MEMUSAGE gt 102400"
(in Windows) and
ps -aux | awk -F" " '$5 > 102400'
(in Linux):
Lastly, execute this cmdlet:
Stop-Computer -WhatIf
. This time, there's no comparable command on either Windows or Linux.
PowerShell works with commands like any other shell environment. Native PowerShell commands are called cmdlets. Unlike commands from other shells, PowerShell cmdlets should only serve one purpose and fulfill this purpose only. As always, there're exceptions to the rule. In some cases, command-line switches, called switch parameters, can be used to toggle additional functionality.
The first example, Get-Process, returns (Get) a list of running processes (Process). While the formatted output appears similar to that of the Windows command tasklist, PowerShell doesn't merely return text, but .NET objects.
Our second example, Get-Date, returns the current date and time as a .NET object again. In .NET, time is calculated with ticks, which are 100 nanosecond-intervals starting at 0001-01-01 00:00:00. The output is formatted depending on your operating system's culture and can be changed on demand.
The third example has you filter the output with PowerShell, which is extremely easy compared to Windows and Linux alike. Especially the endless possibilities of working with text in Linux make this a striking example. The ps command doesn't allow much filtering, so you need to rely on tools such as awk to process the text that is returned. This simple task without PowerShell requires knowledge of text processing and filtering with different tools.
The last cmdlet, Stop-Computer, demonstrates a very common parameter with many cmdlets called WhatIf. This parameter allows you to simply try a cmdlet before actually doing anything. This is an excellent way to test changes for general correctness, for example, before modifying your 10.000 Active Directory user accounts:
There's plenty more to do and see in PowerShell—part of which will be covered in this book. Try to follow the upcoming recipes as well to find out about cmdlet discovery, the flow between cmdlets in the pipeline, and much more.
All official documentation regarding PowerShell:
https://docs.microsoft.com/powershell
Learn PowerShell Core 6.0
by
David das Neves
and
Jan-Hendrik Peters
, as a supplementary book to really learn the language:
https://www.packtpub.com/networking-and-servers/learn-powershell-core-60
Help is never far away in PowerShell Core and, in this section, you'll learn how to utilize the help to your benefit.
In order to follow this recipe, you should have completed the installation of PowerShell Core for your operating system.
Please perform the following steps:
Open PowerShell Core.
Type the
Get-Help
cmdlet
and hit
Enter.
The cmdlet displays help about the help system.
Use the
-?
parameter
with any cmdlet, for example,
Start-Process -?
. Notice the output after this cmdlet. You can see the syntax of the cmdlet, as well as some additional remarks:
Type the
Get-Help Start-Process -Parameter FilePath
command.
Note the output at that point. Only help for the
FilePath
parameter
is returned. From the output, you can see that the parameter is mandatory, has two aliases, and doesn't like pipeline input:
Type the
Get-Help Start-Process -Full
command.
You can notice in the output that indeed no help files have been downloaded yet.
Type the
Update-Help -Scope CurrentUser
command
to download all current help content.
Examine the folder contents of
$home\Documents\PowerShell\Help
in Windows and
~/.local/share/powershell
in Linux.
Type the
Update-Help -Module CimCmdlets -UICulture ja-jp,sv-se
command.
Notice that not all modules provide localized help content—the content in
en-us
should be available for most modules, however.
Now that the help content has downloaded, try
Get-Help Start-Process -Full
again.
Notice that now the full content is available, allowing you to get additional information about a cmdlet.
The help system of PowerShell Core can be used to update help files from the internet or from a CIFS share. Without updated help content, the help system always displays the name and syntax of a cmdlet as well as detailed parameter help for all parameters of a cmdlet.
In order to update help for modules on the local system, Update-Help will examine all modules in the PSModulePathenvironmental variable in order to find all modules that have the HelpInfoUriproperty set. It'll try to resolve the URI, which should point to a browsable website where it will then look for an XML file called <ModuleName>_<ModuleGuid>_HelpInfo.xml. Inside this XML file, the location of a cabinet file (*.cab