Book Image

Windows Server 2016 Administration Cookbook

By : Jordan Krause
Book Image

Windows Server 2016 Administration Cookbook

By: Jordan Krause

Overview of this book

<p>Windows Server 2016 is an operating system designed to run on servers. It supports enterprise-level data storage, communications, management, and applications. This book contains specially selected, detailed help on core, essential administrative tasks of Windows Server 2016.</p> <p>This book starts by helping you to navigate the interface of Windows Server 2016, and quickly shifts gears to implementing roles that are necessarily in any Microsoft-centric datacenter.</p> <p>This book will also help you leverage the web services platform built into Windows Server 2016, available to anyone who runs this latest and greatest Server operating system. Further, you will also learn to compose optimal Group Policies and monitor system performance and IP address management.</p> <p>This book will be a handy quick-reference guide for any Windows Server administrator, providing easy to read, step-by-step instructions for many common administrative tasks that will be part of any Server Administrator’s job description as they administer their Windows Server 2016 powered servers.</p> <p>The material in the book has been selected from the content of Packt's Windows Server 2016 Cookbook by Jordan Krause to provide a specific focus on key Windows Server administration tasks.</p>
Table of Contents (12 chapters)
Title Page
Packt Upsell
Contributors
Preface
Index

Building and executing your first PowerShell script


Command Prompt and PowerShell are both great command-line interfaces that can acquire and configure information about our servers. Most of us are familiar with creating some simple batch files that are driven by Command Prompt, essentially programming out small tasks within these batch files to automate a series of commands. This saves time later as we do not have to type out the commands line by line, especially for common tasks or for items that we need to run during login.

PowerShell has similar functionality, the ability to write out multiple lines of PowerShell cmdlets inside a script file. We can then launch this script file as we would a batch file, automating tasks while taking advantage of the additional features that PowerShell brings to the table over Command Prompt. These PowerShell scripts are put together inside .ps1 files; let's build a simple one together to get a feel for running these scripts.

Getting ready

Our work with PowerShell today will be accomplished from a Windows Server 2016 machine. PowerShell is installed by default with Windows, and there is nothing further that we need to install.

How to do it...

Follow these steps to build and execute our first PowerShell script:

  1. Open the Start menu and type Windows PowerShell ISE. Right-click to launch this tool as an administrator. Windows PowerShell ISE is an editor for PowerShell scripts that is much more useful than opening a simple text editor such as Notepad in order to build our script:
  1. Navigate to File | New from the menus in order to open a blank .ps1 script file.
  2. In your first line, type the following: Write-Host "Hello! Here is the current date and time:".
  3. From the toolbar menu, click the green arrow that says Run Script. Alternatively, you can simply press the F5 button. When you run the script, the command and output are displayed in the lower portion of the ISE window:

Cool! Okay, so far it's actually pretty lame. It's just reflecting the text that we told it to echo, but it worked. That is the nice thing about using the ISE editing tool rather than a generic text editor, you have the ability to quickly test run scripts as you make modifications.

  1. Now let's add some additional lines into our script to give us the information we are looking for. You can see a list of available commands on the right side of the screen if you would like to browse through what is available, but for our example simply change your script to include the following:
      Write-Host "Hello! Here is the current date and time:" 
      Get-Date 
      Write-Host "The name of your computer is:" 
      hostname
  1. Press the Run Script button again to see the new output:
  1. Now navigate to File | Save and save your new .ps1 PowerShell script out to the Desktop.
  2. Let's test this script by launching it from within a real PowerShell command window. Right-click on your PowerShell icon in the Taskbar and choose Run as administrator.
  3. Browse to the location of the script file, I placed mine on the Desktop. Then launch the script by inputting .filename. In my case, it looks like this: .time.ps1.

Note

Remember that the Tab key can be our friend in this. When browsing to your Desktop, all you need to do is input the first letter of your script filename, and then press Tab. Since I named my script Time.ps1, all I had to do was press the T and then press Tab, then Enter.

How it works...

In this recipe, we created a very simple PowerShell script and saved it on our server for execution. While in practice getting time and date information from your server may come faster by using the standalone Get-Date cmdlet, we use this recipe to give a small taste of the ISE and to get your scripting juices flowing. Expanding upon the ideas presented here will start to save you valuable time and keystrokes as you identify more and more ways to automate the tasks and information gathering that are part of your daily routines. The possibilities of PowerShell are practically limitless, so make sure that you open it up and start becoming familiar with the interfaces and tools associated with it right away!