Book Image

Instant Windows PowerShell Guide

By : Harshul Patel
Book Image

Instant Windows PowerShell Guide

By: Harshul Patel

Overview of this book

Windows PowerShell has become a booming scripting language over the last couple of years. It has extensive support with an ample number of vendor products, providing a standardized platform for automation and administration. It has massive support for all Microsoft products which creates a layer that can easily automate everything. In the latest version, the PowerShell team has introduced much more functionality with thousands of CMDLETs, part of various modules.This book is a quick reference guide to enable you to get the most out of the latest Windows PowerShell techniques. In this book, you will find new enhancements in the latest version of PowerShell with some helpful examples. This book enables you to quickly move from older versions of PowerShell to Version 3.0 and Version 4.0.This practical, example-oriented book helps you to overcome the difficulty of using and discovering CMDLETs by providing precise information about everything that has been newly introduced in the latest version of Windows PowerShell. It also focuses on the new configuration management system with the help of DSC as a new feature of Windows PowerShell v4.0.You will learn how to use the newly introduced CMDLETs and parameters to perform daily routine tasks. You will also learn how to administer the servers remotely and maintain persistent sessions to provide continuity. You will gain an insight into writing efficient scripts by using various parameters, snippets, and workflows to gain more productivity. You will also be introduced to various modules like CimCmdlets, PSScheduledJob, PSDesiredStateConfiguration, and so on in order to enhance your scripts with the latest instrumentation. Finally this book will make you aware of the capabilities of PowerShell v4.0 and how to fully leverage the functionality introduced in the new version.
Table of Contents (7 chapters)

WMI versus CIM (Advanced)


In earlier versions, Windows PowerShell's inventory mechanism was strongly dependent on Windows Management Instrumentation (WMI). With the release of PowerShell Version 3.0, we have Common Information Model known as CIM. PowerShell Version 3.0 has ample number of CMDLETs under the default module named CimCmdlets.

Getting ready

Moreover, CIM CMDLETs work with WSMan to manage Windows as well as other operating systems. To ensure all the CMDLETs are available with CimCmdlets, use the following code:

PS C :\> Get-Comman d -Module CimCmdlets
CommandType     Name                          ModuleName
-----------     ----                          ----------
Cmdlet          Get-CimAssociatedInstance     CimCmdlets
Cmdlet          Get-CimClass                  CimCmdlets
Cmdlet          Get-CimInstance               CimCmdlets
Cmdlet          Get-CimSession                CimCmdlets
Cmdlet          Invoke-CimMethod              CimCmdlets
Cmdlet          New-CimInstance               CimCmdlets
Cmdlet          New-CimSession                CimCmdlets
Cmdlet          New-CimSessionOption          CimCmdlets
Cmdlet          Register-CimIndicationEvent   CimCmdlets
Cmdlet          Remove-CimInstance            CimCmdlets
Cmdlet          Remove-CimSession             CimCmdlets
Cmdlet          Set-CimInstance               CimCmdlets

How to do it...

Execute the following lines of code, by performing the following steps:

  1. The following command statement retrieves CIM instances from the Win32_ComputerSystem CIM class.

    PS C:\> Get-CimInstance -ClassName Win32_ComputerSystem
    
  2. The following command statement executes a query for services starting with the win keyword.

    PS C:\> Get-CimInstance -Query "SELECT * from Win32_Service WHERE name LIKE 'win%'"
    
  3. The following two command statements subsequently create a remote CimSession on the PSTest computer and retrieves CIM instances for all the processes from the PSTest CIM server.

    PS C:\> $S = New-CimSession -ComputerName PSTest
    PS C:\> Get-CimInstance -ClassName Win32_Process -CimSession $S
    

How it works...

The Get-CimInstance CMDLET retrieves CIM instances from the specified computer and the CIM class name. This CMDLET omits one or more output instance object, providing information from the specified class. The following are the parameters available with this CMDLET:

  • -CimSession <CimSession[]>: This parameter supplies CimSession that can be retrieved in a variable by using the New-CimSession or Get-CimSession CMDLET.

  • -ClassName <String>: This parameter provides the CIM class name for which we need to retrieve CIM instances.

    Tip

    You can use tab completion to browse for the ClassName parameter's values.

  • -ComputerName <String[]>: This parameter supplies a list of computer names to retrieve the respective CIM instances for them. This CMDLET creates a temporary session on specified computers using the WSMan protocol to retrieve the information.

  • -Filter <String>: This parameter specifies a condition to filter the output data.

  • -InputObject <CimInstance>: You can also specify the CimInstance object as InputObject for this CMDLET using this parameter.

  • -KeyOnly [<SwitchParameter>]: This parameter helps to get the output object with only the key properties, which in fact reduces the load over the network.

  • -Namespace <String>: This parameter specifies the namespace on which the CIM class resides. The default namespace value is defined as root/cimv2.

    Tip

    You can use tab completion to browse for namespaces.

  • -OperationTimeoutSec <UInt32>: This parameter explicitly specifies the time during which CMDLET can wait for the output from the computer.

  • -Property <String[]>: This parameter gives us a choice to select the required properties from the property set. It reduces the output data size.

  • -Query <String>: This parameter allows us to supply the SELECT query to the CMDLET. It accepts all the operators supported by WMI Query Language (WQL).

  • -QueryDialect <String>: This parameter provides information on the query language that is used with the Query parameter. By default, it takes WQL as the query language.

  • -ResourceUri <Uri>: This parameter allows us to provide an external Uri to get CimInstance objects. By default, the CMDLET searches for the specified class name at http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/.

  • -Shallow [<SwitchParameter>]: This parameter, if supplied, avoids retrieving information of a child class. By default, CMDLET retrieves information from both the class and child classes.

There's more…

There is also the Invoke-CimMethod CMDLET available with the CimCmdlets module. Instead of using Invoke-WmiMethod, consider using Invoke-CimMethod.