Book Image

Windows Server 2019 Automation with PowerShell Cookbook - Third Edition

By : Thomas Lee
Book Image

Windows Server 2019 Automation with PowerShell Cookbook - Third Edition

By: Thomas Lee

Overview of this book

Windows Server 2019 is the latest version of Microsoft’s flagship server operating system. It also comes with PowerShell Version 5.1 and offers a number of additional features that IT professionals will find useful. This book is designed to help you learn how to use PowerShell and manage the core roles, features, and services of Windows Server 2019. You will begin by creating a PowerShell Administrative Environment that features updated versions of PowerShell, the Windows Management Framework, .NET Framework, and third-party modules. Next, you will learn to use PowerShell to set up and configure Windows Server 2019 networking and understand how to manage objects in the Active Directory (AD) environment. The book will also guide you in setting up a host to utilize containers and deploying containers. Further along, you will be able to implement different mechanisms to achieve Desired State Configuration. The book will then get you up to speed with Azure infrastructure, in addition to helping you get to grips with setting up virtual machines (VMs), websites, and file share on Azure. In the concluding chapters, you will be able to deploy some powerful tools to diagnose and resolve issues with Windows Server 2019. By the end of this book, you will be equipped with a number of useful tips and tricks to automate your Windows environment with PowerShell.
Table of Contents (19 chapters)
Windows Server 2019 Automation with PowerShell Cookbook Third Edition
Foreword
Contributors
Preface
Index

Reporting on printer usage


Knowing who is using your printing devices and how much can be important in terms of capacity planning.

By default, Windows does not log printer usage information. But it is simple to turn on this logging and use the results.

Getting ready

You use this recipe on the PSRV host.

How to do it...

  1. Run wevtutil.exe to turn on printer monitoring on the PSRV host:

    $LogName = 'Microsoft-Windows-PrintService/Operational'
    wevtutil.exe sl $LogName /enabled:true
  2. Define a function that returns objects for each printer job completed on the server:

    Function Get-PrinterUsage {
    # 2.1 Get events from the print server event log
    $LogName = 'Microsoft-Windows-PrintService/Operational'
    $Dps = Get-WinEvent -LogName $LogName |
             Where-Object ID -eq 307 
    Foreach ($Dp in $Dps) {
    # 2.2 Create an ordered hash table
       $Document          = [ordered] @{}
    # 2.3 Populate the hash table with properties from the 
    # Event Log entry
       $Document.Id       = $Dp.Properties[0].value
       $Document.Type...