Finding the right command – How can I do this?!?
So far we have learned that PowerShell is a object-oriented scripting language, whose aim it is to offer an easy API to complex tasks via predefined Cmdlets and on the other hand to be the ONE tool, which can perform every task in a Windows Environment. In the first place this only means that one does not have to reinvent the wheel for every task that you would like to solve with the help of PowerShell. So it is essential to find and pick the right command of the existing ones for your task.
Mhmm ok, but how is this even possible, as there are three major ways to gather and manipulate relevant data and write software within in the Windows Universe plus the layer above those three with the PowerShell-Cmdlets and therefore a almost endless flood of information.
PowerShell Modules and Cmdlets
Because the built-in PowerShell-Cmdlets are the most abstract and often easiest way to reach the aspired goal, you should start and make yourself comfortable with the different PowerShell-Modules, which are on your Windows-System.
Enter “Get-Module -ListAvailable” into your PowerShell-Console to get every Module, which is available in the current Session.
To list all Information about all Modules pipe the result into a Select Statement:
“Get-Module -ListAvailable | Select *”
To gather information about a specific Module, you are interested in. Commit the following commands:
Get-Module -Name “NameOfTheModule” | Select *”
With these commands you can get to know the Modules and functions, exported by the available Modules.
To find a Function for a specific topic you can try to search for it with a command like this: “Get-Command *network* “(‘*’ can be used as a wildcard).
But having found the right function, does not automatically mean that you can use the function as well. For this there is a command to checkout how to use a function:
“Get-Help -Name Enable-NetworkSwitchVlan -Full”
For further information information feel free to search the web. Especially on GitHub there are several interesting Projects which can be imported into your module-directory. Even Microsoft launched a official Github-Channel for PowerShell there.
.Net and Co.
Until now you have learned how to find and use the built-in PowerShell-Cmdlets, which are the easiest point of entry for any PowerShell-Newbie, because those Cmdlets kind of encapsulate the more complex topics .Net, WMI-/CIM-Classes and COM-Objects. But as these Cmdlets do not cover every single task that you might want to solve, there are ways to use the more basic technologies like .Net within the PowerShell.
.Net is a software framework with countless classes created by Microsoft and it is possible to instantiate every single .Net-Class (which is of course not abstract or an interface) in a PowerShell script. Lets take a closer look with an example:
PowerShell-Arrays are easy to use, but not suitable for every situation, because they are static. For a more dynamic data structure better use the .Net-Class ArrayList out of the Namespace System.Collections. There is a documentation for every class in the web (Documentation – ArrayList). With the help of the documentation it is possible to find out, if the constructor (The method that is called, when a object of the class should be created) needs any arguments. In the case of the ArrayList, this is not necessary, however it is up to you to pass for example the initial capacity in brackets (“New-Object – TypeName System.Collections.ArrayList (5)”)
This syntax (New-Object…) can be used to create any .Net-Class.
Moreover there are countless WMI-/CIM-Classes which are based on the Common Information Model. These Classes provide a general object model for all managed elements of an IT Environment, regardless of the manufacturer.
Because Windows Management Instrumentation (WMI), which is a CIM server that implements the CIM standard on Windows, uses the non-standard DCOM protocol, it is recommended to use the newly in PowerShell 3.0 introcued Cmdlets for CIM-Classes (further reading – Introduction to CIM Cmdlets).
To find the right WMI-/CIM-Class and to get an overview, which class is available, there are some advisable tools:
- WMI-Explorer (download via codeplex: WMI Explorer)
- CIM-Explorer (download via Technet-Gallery: CIM Explorer)
Last thing you need to know is how WMI-/CIM Classes are created. For that, here is an easy example, which figures out which operating System is running on your computer:
Last but not least there are COM-Objects, wich are based on the idea of the Component Object Model, meaning a dynamic object, wich can be uses via an interface in different programming languages. As the Microsoft office products are based on this technology, it is possible to create a Word-Document with the help of PowerShell.
Lets have a short example: This example should open your Microsoft Word with a new Document and the Text “Learn the Power of Shell” in it:
#Create a Word-Instance $Word = New-Object -ComObject Word.Application #Create a new Word Document $Document = $Word.Documents.Add() #Add some Text: $Selection = $Word.Selection $Selection.TypeText("Learn the Power of Shell!") #Show the Magic $Word.Visible = $true
It is not important that you as a PowerShell-Scripter know every single PowerShell-Cmdlet or .Net-Class! But it is important that you are comfortable with the built-in PowerShell modules, that you can find Cmdlets, you know how to use them and that you know how to use the more basic technologies like .Net, WMI-/CIM-Classes and COM-Objects – Not more and not less.
For all other things there are more or less endless documentations (https://msdn.microsoft.com, search the web…). And over time you will memorize the most important .Net-Classes or WMI-Classes for your PowerShell-Projects – System.Collections.ArrayList is a good starting point.
So lets learn to use the Power of Shell!
Look forward to the upcoming posts.