Book Image

PowerShell Core for Linux Administrators Cookbook

By : Prashanth Jayaram, Ram Iyer
Book Image

PowerShell Core for Linux Administrators Cookbook

By: Prashanth Jayaram, Ram Iyer

Overview of this book

PowerShell Core, the open source, cross-platform that is based on the open source, cross-platform .NET Core, is not a shell that came out by accident; it was intentionally created to be versatile and easy to learn at the same time. PowerShell Core enables automation on systems ranging from the Raspberry Pi to the cloud. PowerShell Core for Linux Administrators Cookbook uses simple, real-world examples that teach you how to use PowerShell to effectively administer your environment. As you make your way through the book, you will cover interesting recipes on how PowerShell Core can be used to quickly automate complex, repetitive, and time-consuming tasks. In the concluding chapters, you will learn how to develop scripts to automate tasks that involve systems and enterprise management. By the end of this book, you will have learned about the automation capabilities of PowerShell Core, including remote management using OpenSSH, cross-platform enterprise management, working with Docker containers, and managing SQL databases.
Table of Contents (19 chapters)

Using exit codes

Error codes are ubiquitous. PowerShell is no exception.

Find out whether the following command generated an error, and if it did, show the error code:

PS> ping ghostname

Would the same technique work with the following command?

PS> Get-Item $HOME/random/randompackage.zip -ErrorAction SilentlyContinue

How to do it...

We will place the given cmdlet within a script and try to find whether the line generated an error, and if it did, find its error code.

Create a script with the following and run it. Dot-sourcing the script is optional:

ping ghostname

if (!$?) {
    Write-Host "PowerShell seems to have encountered an error while running the last command."
}

Write-Host "The exit code for...