Let's break the following command down into its individual components
Get-EventLog -LogName Security -ComputerName REHl8SERV1, CENTOSSERV2 -Verbose
Components:
- Command:
Get-EventLog
- Parameter Name 1:
-LogName
- Parameter Value 1:
Security
- Parameter Value 1:
- Parameter Name 2:
-ComputerName
- Parameter Value 2 (multiple):
REHl8SERV1, CENTOSSERV2
- Parameter Value 2 (multiple):
- Parameter Name 3 (switch):
-Verbose
- Parameter Value 3: No value
You might first notice the structure of the command Get-EventLog
, a verb Get
followed by a single dash -
and ending in a singular noun EventLog
.
This structure is applicable to all native PowerShell cmdlets
After working with PowerShell for sometime you'll find you're able to intuitively construct a command you've never used before, due to all cmdlets following the same conventions. The list below contains many of the most commonly seen/used verbs:
- Get
- Add
- Remove
- New
- Set
- Write
So, we've discussed the base structure of a cmdlet but for those unfamiliar with scripting and development from other languages, what are parameters?
The primary function of a parameter is to provide additional input for the command to which it's attached.
Within our example: Get-EventLog -LogName Security -ComputerName REHl8SERV1, CENTOSSERV -Verbose
We have three parameters being used:
- LogName
- ComputerName
- Verbose
With each being prepended by a dash -
. Immediately following -LogName
and -ComputerName
are values being passed to provide additional context/values to these named parameters
ie.
- Single Value: Security
- Multiple Values: REHl8SERV1, CENTOSSERV
Neither is required to be contained in quotes for this example as there are no space or punctuation marks in the values used.
The reason they are called named parameters is self-explanatory; we've explicitly named the parameters we're using. Alternatively we could use positional parameters if know what order the cmdlets expects to recieve it's arguments. Example below:
Get-EventLog Security
Only parameter -LogName is required and can be passed positionally with all other parameters being options and requiring named parameters specifying their use.
The example above will of course not provide verbose output since the -Verbose
is missing and default to executing the command against localhost (The computer executing the command) since no list of external hosts is provided using
-ComputerName
This concludes a brief intoduction into PowerShell. This required knowledge to learn to effectively script with PowerShell going forward.
Note you can always use Get-Help
to learn more about a cmdlet's functionality and it's optional/required parameters.
Example: Get-Help -Name Get-EventLog
Click here to return to our scripting PowerShellComplete
Tags: PowerShell Windows