Book Image

Windows Server 2016 Automation with PowerShell Cookbook - Second Edition

By : Thomas Lee, Ed Goad
Book Image

Windows Server 2016 Automation with PowerShell Cookbook - Second Edition

By: Thomas Lee, Ed Goad

Overview of this book

This book showcases several ways that Windows administrators can use to automate and streamline their job. You'll start with the PowerShell and Windows Server fundamentals, where you'll become well versed with PowerShell and Windows Server features. In the next module, Core Windows Server 2016, you'll implement Nano Server, manage Windows updates, and implement troubleshooting and server inventories. You'll then move on to the Networking module, where you'll manage Windows network services and network shares. The last module covers Azure and DSC, where you will use Azure on PowerShell and DSC to easily maintain Windows servers.
Table of Contents (21 chapters)
Title Page
Credits
About the Author
Acknowledgment
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface

Discovering new cmdlets in PowerShell 4 and Windows Server 2012 R2


PowerShell V4 and Server 2012 R2 added many new features to existing cmdlets but did not add many new cmdlets. A notable exception is Desired State Configuration (DSC) feature that debuted in PowerShell V4.

DSC is a set of language extensions that enable you to define computer configurations in a declarative fashion then apply that configuration to managed computers. DSC is a tool to provision or manage servers and to ensure those systems stay configured correctly. DSC provides a solution to the problem of configuration drift—computer configurations that change, often incorrectly, over time.

Get more information on DSC in Chapter 13, Using Desired State Configuration.

New cmdlets

Two other useful cmdlets included in PowerShell V4 are:

  • Get-FileHash: Creates a hash value from a given file or binary value. This is useful for quickly determining whether files have changed or for finding duplicate files (that have different file names)
  • Test-NetConnection: Diagnoses a network connection and provides helpful troubleshooting information. This cmdlet is described in more detail in Chapter 8, Managing Windows Network Services.

How to do it...

  1. You use the Show-Command to investigate the Get-FileHash cmdlet:
  Show-Command -Name Get-FileHash

Note

Show0-Command is not available in the Server Core version, as it lacks the graphical user interface.

  1. In the dialog that pops up, the Path tab corresponds to one of three parameter sets for this command. For the Path tab, enter $Env:windirnotepad.exe or any other valid file path.
  2. Choose an algorithm like SHA512 from the drop-down menu.
  3. Click the Copy button then paste the command into your PowerShell ISE and press Enter to run it. Note the hash value that is returned.
  4. Use Show-Command to investigate Test-NetConnection:
  Show-Command -Name Test-NetConnection
  1. In the dialog box, the CommonTCPPort tab corresponds to the default parameter set, the first of four. Choose HTTP from the CommonTCPPort drop-down, and choose Detailed for InformationLevel. Then click Copy, and paste the script into your editor below the Show-Command line, then close the Show-Command window. Select this line and press F8 to run this line.
  1. Repeat your call to Show-Command -Name Test-NetConnection. Choose the ICMP tab and enter a valid internet hostname like Windows.Com in the ComputerName field, or leave it blank, and choose Detailed for InformationLevel.
  2. Click the Copy button then paste the command into your PowerShell ISE below the previous command, then close the Show-Command window and select the line and press F8 to run it.
  3. Repeat your call to Show-Command Name Test-NetConnection. Choose the NetRouteDiagnostics tab, check the box for DiagnoseRouting, and click Run.
  4. Repeat your call to Show-Command -Name Test-NetConnection. Choose the RemotePort tab, enter 443 for the Port, and choose Detailed for InformationLevel, and click Run.

How it works...

In step 1, you use Show-Command to provide a graphical interface to explore new commands like Get-FileHash or new ways to use commands you know. It is the same interface that displays in the Commands tab in PowerShell ISE, and the interface is programmatically generated from the parameter definitions in the cmdlet or function, so it works with commands you create or install from outside sources.

In steps 2 and 3, choosing the Path tab corresponds to a parameter set defined in the command; each parameter set may have different required and optional parameters, represented by check boxes, drop-down menus, or text fields. This parameter set requires the Path and Algorithm parameters.

In step 4, the Copy button puts a syntax-correct command on our clipboard, either to be run as is or added to a script and modified. This is a very useful feature for new PowerShell scripters or those working with unfamiliar commands. The result of the command displays in the console, but it could be stored into a variable for comparison with other hash values to look for duplicate or changed files:

In steps 5 and 6, you use Show-Command to explore the Test-NetConnection cmdlet. This is a flexible and useful troubleshooting command with four parameter sets to use. First, test the connection to a web host over HTTP port 80. Note the -InformationLevel Detailed parameter provides additional troubleshooting information on the connectivity.

In steps 7 and 8, you use the ICMP parameter set with the -InformationLevel Detailed parameter to ping, using ICMP echo request, a web server. This is different to the earlier steps—here you are just determining whether the target server is responding to echo requests. Some web servers turn off returning of pings, so you may see a server that doesn't respond to a ping but does allow a port 80 HTTP connection.

In step 9, you use the NetRouteDiagnostics parameter set with the -DiagnoseRouting parameter, which was introduced in PowerShell 5.1, to get routing information. Here when you click the Run button, the result displays in the console window.

In step 10, you specify a RemotePort parameter set with a specified Port and ComputerName to test:

There's more...

Both Server 2012 R2 and PowerShell V4 introduced many new features and added enhancements to existing features. This included the Hyper-V, SmbShare, and BranchCache features, all of which were improved. These features came with PowerShell modules that enable you to leverage these features using PowerShell. Get more information on the modules that support the 2012 R2 features at https://technet.microsoft.com/en-us/library/dn249523.aspx.