Skip to main content

How to Read Help Files (Part 3 of 7)

Syntax is very important when writing code. A computer needs exact instructions.  That is why every computer language has a syntax.  The syntax is a set of rules that you must follow in order for a command to execute successfully.  Today we are going to examine the syntax section of a PowerShell Help File.  Execute this command:

Get-Help Stop-Service

Below is the Syntax section.
SYNTAX
    Stop-Service [-InputObject] [-Exclude []]
    [-Force] [-Include []] [-InformationAction {SilentlyContinue |
    Stop | Continue | Inquire | Ignore | Suspend}] [-InformationVariable
    []] [-NoWait] [-PassThru] [-Confirm] [-WhatIf]
    []
   
    Stop-Service [-Exclude []] [-Force] [-Include []]
    [-InformationAction {SilentlyContinue | Stop | Continue | Inquire | Ignore
    | Suspend}] [-InformationVariable []] [-NoWait] [-PassThru]
    -DisplayName [-Confirm] [-WhatIf] []
   
    Stop-Service [-Name] [-Exclude []] [-Force] [-Include
    []] [-InformationAction {SilentlyContinue | Stop | Continue |
    Inquire | Ignore | Suspend}] [-InformationVariable []]
    [-NoWait] [-PassThru] [-Confirm] [-WhatIf] []

The Syntax block of information shows you all of the valid parameters for Stop-Service and a little bit about how to use them. Personally, I am not a fan of this cluster of information.  There is a lot going on here.  You can see that Stop-Service is listed 3 times.  That is because the author of the cmdlet created parameters that cannot be used at the same time.  Download my code for Get-RequiredParameters (http://mctexpert.blogspot.com/2015/12/finding-required-parameters-in.html). Once you load Get-RequiredParameters into memory, execute this code:

Get-RequiredParameters -Cmdlet Stop-Service | Where Required -eq $True

PS C:\> Get-RequiredParameters -Cmdlet Stop-Service | Where Required -eq $True

Parameter   Required Set       
---------   -------- ---       
InputObject true     InputObject
DisplayName true     Default   
Name        true     DisplayName

You are now looking at the parameter sets and the individual parameters that cannot be used at the same time.  Below is the syntax block of the help file once again with these 3 parameters highlighted.
SYNTAX
    Stop-Service [-InputObject] [-Exclude []]
    [-Force] [-Include []] [-InformationAction {SilentlyContinue |
    Stop | Continue | Inquire | Ignore | Suspend}] [-InformationVariable
    []] [-NoWait] [-PassThru] [-Confirm] [-WhatIf]
    []
   
    Stop-Service [-Exclude []] [-Force] [-Include []]
    [-InformationAction {SilentlyContinue | Stop | Continue | Inquire | Ignore
    | Suspend}] [-InformationVariable []] [-NoWait] [-PassThru]
    -DisplayName []> [-Confirm] [-WhatIf] []
   
    Stop-Service [-Name] [-Exclude []] [-Force] [-Include
    []] [-InformationAction {SilentlyContinue | Stop | Continue |
    Inquire | Ignore | Suspend}] [-InformationVariable []]
    [-NoWait] [-PassThru] [-Confirm] [-WhatIf] []

Information is also displayed with a series of <> and [].  All of these items have the name of a parameter (starts with a dash character “ – “) and then is followed by its data type.  There are four common combinations of how to display this information utilizing the <> and [].  They all mean different things.

[-ParameterName]

Square braces surrounding the parameter name and then followed by the data type are required parameters.  If you omit these parameters, PowerShell will suspend execution and ask you for the value to give this parameter.

[-ParameterName ]

Square braces surrounding both the parameter name and data type is an optional parameter.  This means that you use it only if you need to.

[-Parameter Name]

A parameter name in square braces with no data type is a switch parameter.  A switch parameter does not accept an argument.  You use this to turn on extra functionality of the cmdlet.

[-ParameterName {Value1 | Value2 | Value 3}]

A parameter name inside of square braces with a set of potential values inside curly braces is an optional parameter.  The valid values for that parameter is listed. Inside of the curly braces.


Tomorrow we will look at detailed help files. 

Comments

Popular posts from this blog

How to list all the AD LDS instances on a server

AD LDS allows you to provide directory services to applications that are free of the confines of Active Directory.  To list all the AD LDS instances on a server, follow this procedure: Log into the server in question Open a command prompt. Type dsdbutil and press Enter Type List Instances and press Enter . You will receive a list of the instance name, both the LDAP and SSL port numbers, the location of the database, and its status.

How to run GPResult on a remote client with PowerShell

In the past, to run the GPResult command, you would need to either physically visit this client, have the user do it, or use and RDP connection.  In all cases, this will disrupt the user.  First, you need PowerShell remoting enabled on the target machine.  You can do this via Group Policy . Open PowerShell and type this command. Invoke-Command –ScriptBlock {GPResult /r} –ComputerName <ComputerName> Replace <ComputerName> with the name of the target.  Remember, the target needs to be online and accessible to you.

Error icon when creating a GPO Preference drive map

You may not have an error at all.  Take a look at the drive mapping below. The red triangle is what threw us off.  It is not an error.  It is simply a color representation of the Replace option of the Action field in the properties of the drive mappings. Create action This give you a green triangle. The Create action creates a new mapped drive for users. Replace Action The Replace action gives you a red triangle.  This action will delete and recreate mapped drives for users. The net result of the Replace action is to overwrite all existing settings associated with the mapped drive. If the drive mapping does not exist, then the Replace action creates a new drive mapping. Update Action The Update action will have a yellow triangle. Update will modify settings of an existing mapped drive for users. This action differs from Replace in that it only updates settings defined within the preference item. All other settings remain as configured on the ma...