Wednesday, May 8, 2019

Windows PowerShell cmdlets


In this topic, we will cover the following:
  • Exploring Windows PowerShell commands
  • Exploring Windows PowerShell modules
  • Getting help
  • Understanding aliases, expressions, objects, pipelines, filtering, and formatting
  • Scripting with Windows PowerShell
Windows PowerShell is designed to execute four kinds of named commands:
  • Cmdlets: These are .NET programs designed to interact with PowerShell
  • Scripts: These are files with the FileName.PS1 extension
  • Functions: These are a block of code that are very helpful for script organization
  • Native window commands: These are internal commands such as MKDIR, CHDIR, and so on
The internal commands of Windows PowerShell are called cmdletsCmdlets are always named in the verb-and-noun combination. This appears as Verb-Noun, for example Get-Service and Stop-Service, where Get and Stop are verbs, and Service is a noun.
(Verb)—Action
(Noun)—Something to be acted on
For example, Get-Service
Let's execute the following command:
Get-Command
The output of this command is illustrated in the following image:
The previous command will list all the installed commands from the computer and retrieve only the cmdlets, functions, and aliases.
To retrieve only cmdlets, we can use the CommandType parameter, as shown in the following command:
Get-Command -CommandType Cmdlet

Note

Windows PowerShell supports tab completion. For example, let's run the following command:
Get-Comm + Tab + -Co + Tab + C + Tab
When we type out this command, it will result as follows:
Get-Command -CommandType Cmdlet
Now, let's explore the commands. In the following example, we will take a look at the cmdlet Get-Service. Since we know the cmdlet up front, let's collect more information such as the cmdlet version and source, as follows:
Get-Command Get-Service
The output of the preceding command is illustrated in the following image:
Windows PowerShell cmdlets
The points marked in the figure are explained in the following list:
  • 1: This is the type of command
  • 2: This is the name of the command
  • 3: This indicates the version (From Windows PowerShell 5.0 onward)
  • 4: This indicates the module's name
To retrieve the commands for a specific module, we need to use the Module parameter, which accepts the module name (Source) as we saw in bullet 4. The following is an example of the command:
Get-Command -Module Microsoft.PowerShell.Management
This outputs all the commands available in the Microsoft.PowerShell.Management module.
The Get-Service command will retrieve all the services running on your local computer.

Note

Refer to the following link to get more information on the ServiceController class:
Let's see the alternate of the Get-Service cmdlet using the .NET class. To do this, let's find the TypeName of the Get-Service cmdlet by executing Get-Service | Get-Member and this gives the TypeName as System.ServiceProcess.ServiceController. Windows PowerShell allows us to use this directly as follows:
[System.ServiceProcess.ServiceController]::GetServices()
Now, let's take a look at how the command that we just discussed works.
Here, we will consume the .NET ServiceController class in PowerShell and invoke the GetServices()method.
The GetServices method has an overload with a machineName parameter. To find this, we will execute the following command:
[System.ServiceProcess.ServiceController]::GetServices
The difference is that here, we did not use parentheses. The output of OverloadDefinitions is as follows:
OverloadDefinitions
-------------------
static System.ServiceProcess.ServiceController[] GetServices()

static System.ServiceProcess.ServiceController[] GetServices(string machineName)
We can query the service information of a remote computer by passing the host name of the remote computer as the machineName parameter, as follows:
 [System.ServiceProcess.ServiceController]::GetServices('RemoteServer')

No comments:

Post a Comment

Remote Hybrid and Office work