PowerShell: The Ultimate Beginners Guide to Learn PowerShell Step-by-Step by Reed Mark
Author:Reed, Mark [Reed, Mark]
Language: eng
Format: epub
Publisher: Publishing Factory
Published: 2020-08-19T16:00:00+00:00
Chapter Five:
Functions
Functions
A function constitutes a list of statements with a user-assigned name. The function can be executed by typing its name. The statements included within the list will execute as if they had been typed on the console.
A function may be simple, such as :
>function Get-PowerShellProcess { Get-Process PowerShell }
A function may also be more complex. It may have the complexity of an application or a cmdlet. Functions, similar to cmdlets, may contain parameters as well. These parameters may be positional, named, dynamic, or switch.
Either a pipeline or console can read a function. Functions can be constructed to return values, which can be shown. These values can also be made part of variable assignments passed to cmdlets or other functions. A function can have a particular return value specified through the keyword return. This keyword will not suppress or affect other output designated to be returned from the function. However, using return will trigger an exit from the function on that line.
A function can have separate statement lists categorised by the keywords Process, Begin, and End. These three types of statement lists have distinct methods for handling pipeline input. A filter is a particular class of functions designated with the keyword Filter. Functions can also be constructed to behave or operate similar to cmdlets without requiring C# programming.
Function Syntax
The syntax for specifying a function is as follows:
function [<scope:>]<name> [([type]$parameter1[,[type]$parameter2])]
{
param([type]$parameter1 [,[type]$parameter2])
dynamicparam {<statement list>}
begin {<statement list>}
process {<statement list>}
end {<statement list>}
}
A function has the following components:
● List of PowerShell statements demarcated with brace symbols {}
● Function keyword
● Scope which is optional
● User selected name
● Named parameters which are optional
Simple Functions
A simple function is simply a standard function deployed without the optional components (named parameters and a scope modifier).
A simple function syntax is as follows:
function <function-name> {statements}
To illustrate, this function starts PowerShell in Administrator mode:
function Start-PSAdmin {Start-Process PowerShell -Verb RunAs}
The function can be used by invoking it with its name:
>Start-PSAdmin
Statements can be added to a function by typing different statements on separate lines, or by demarcation with the semicolon symbol (;). At this stage, it would be informative to illustrate the concepts with a simple example.
Example: A function for finding .jpg files that were changed after a specific date
This function identifies all files with extension .jpg located in the path $env:UserProfile changed after the date specified by the $Start variable:
function Get-NewPix
{
$start = Get-Date -Month 1 -Day 1 -Year 2010
$allpix = Get-ChildItem -Path $env:UserProfile\*.jpg -Recurse
$allpix | Where-Object {$_.LastWriteTime -gt $Start}
}
Convenient functions can be organized into a toolbox and added into your profile.
Function Names
Functions may have any name assigned to them. The best practice recommendation, however, is for the names to conform to established standard PowerShell naming conventions for all commands. These conventions include a recommendation that the names comprise a pairing of a verb and a noun. In doing so, the noun characterizes the item the function acts on, and the verb describes the function’s action. The verb should be one of the approved PowerShell standard verbs for commands. Standard verbs help keep the names of all commands coherent, straightforward, and obvious to users.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7307)
Linux Device Driver Development Cookbook by Rodolfo Giometti(3391)
MCSA Windows Server 2016 Study Guide: Exam 70-740 by William Panek(2309)
The Infinite Retina by Robert Scoble Irena Cronin(2133)
Kali Linux - An Ethical Hacker's Cookbook: End-to-end penetration testing solutions by Sharma Himanshu(2096)
Docker on Windows by Stoneman Elton(2077)
Hands-On AWS Penetration Testing with Kali Linux by Karl Gilbert(1819)
Computers For Seniors For Dummies by Nancy C. Muir(1769)
Hands-On Linux for Architects by Denis Salamanca(1726)
The Old New Thing by Raymond Chen(1718)
Programming in C (4th Edition) (Developer's Library) by Stephen G. Kochan(1691)
MCSA Windows Server 2016 Study Guide: Exam 70-741 by William Panek(1476)
Filmora Efficient Editing by Alexander Zacharias(1456)
Installing and Configuring Windows 10: 70-698 Exam Guide by Bekim Dauti(1400)
Mastering PowerShell Scripting - Fourth Edition by Chris Dent(1385)
Embedded Systems Architecture by Daniele Lacamera(1376)
Learn Computer Forensics by William Oettinger(1352)
Database Reliability Engineering: Designing and Operating Resilient Database Systems by Campbell Laine & Majors Charity(1347)
Mastering Kubernetes by Gigi Sayfan(1340)