IT tutorials
 
Applications Server
 

Sharepoint 2010 : Windows PowerShell Functions

11/18/2014 8:16:18 PM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

Functions are used in most programming and scripting languages. A function is a named block of code that can be referred to from within Windows PowerShell. When a function’s name is called, the list of statements contained in the function is executed.

A function may accept input in the form of arguments, the values of which can then be used by the code inside the function. The output from a function can be stored in a variable, passed to another function, passed to a cmdlet, or written to one of the output streams.

A function is declared with the keyword function, and the associated code is placed within a script block. Here is an example of a basic function:

PS > function Hello {
>> "Hello $env:username"
>> }
>>
PS > Hello
Hello nigo

When we call the function Hello, the block of code contained in the function is executed, and the output is returned to the session.

A function also accepts arguments, as this example shows:

PS > function foo { $args }
PS > foo 1 2 3
1
2
3

This function uses the automatic variable $args to return the arguments passed to the function. When we call the function and pass the arguments 1, 2, and 3, they are returned to the session.

Like cmdlets, functions can have parameters. One way to define a parameter is to place a variable within parentheses after the function’s name. Here is an example of a function with two named parameters:

PS > function username ($firstname, $lastname) {
>> "FirstName: $firstname"
>> "LastName: $lastname"
>> }
>>

The two named parameters to the function are $firstname and $lastname. When we call the function, each argument passed to the function will be bound to the corresponding parameter. If we simply type two arguments after we call the function, the arguments will bind to the corresponding parameter based on the argument’s position.

PS > username Niklas Goude
FirstName: Niklas
LastName: Goude

You can also bind the arguments to a named parameter by typing the parameter’s name before the argument:

PS > username -firstname Niklas -lastname Goude
FirstName: Niklas
LastName: Goude

This way, you do not need to enter the arguments in positional order. For example, you can input the last parameter first:

PS > username -lastname Goude -firstname Niklas
FirstName: Niklas
LastName: Goude

By adding a type to a named parameter, you can control the type of argument that the function accepts. Here is an example:

PS > function addition ([int]$val1, [int]$val2) { $val1 + $val2 }
PS > addition 2 3
5

If we try to input a string value to this function, it returns an error.

PS > addition 2 "three"
addition : Cannot process argument transformation on parameter 'val2'.
Cannot convert value "three" to type "System.Int
32". Error: "Input string was not in a correct format."
At line:1 char:9
+ addition <<<< 2 "three"
+ CategoryInfo : InvalidData: (:) [addition], ParameterBindin...
mationException
+ FullyQualifiedErrorId : ParameterArgumentTransformationError,addition



You can also create switch parameters that can either evaluate to True or False. Switch parameters do not require any input; you can simply type the function’s name followed by the name of the switch parameter. Here is an example:

PS > function TV([switch]$on) {
>> if($on) { "The television is on" }

>> else { "The television is off" }
>> }
PS > TV
The television is off
PS > TV -on
The television is on

When we call the function TV without entering the switch parameter’s name, the variable $on is set to False, and The television is off is returned. If we do type the switch parameter’s name, the variable $on is set to True, and The television is on is returned.

You can add other types of named parameters to a function as well. In the next example, we create a named parameter of the type System.uri to check if a URL is valid.

PS > function Check-Url([uri]$url) {
>> if($url.AbsoluteUri -ne $Null -and $url.Scheme -match 'http|https') {
>> $true
>> } else {
>> $false
>> }
>> }

The Check-Url function has one parameter: $url. When we call the function, the argument passed to the function needs to be bound to the corresponding parameter, which can happen either by the parameter’s name or position. If we simply type the argument value after the function’s name, the argument will be bound to the parameter based on the argument’s position. Since we have only one parameter in this example, the argument will be bound to the $url parameter.

It is also possible to specify the type of parameter the function will accept (if there are multiple parameters, type information will also be used in the binding process, after the name and position). Notice how we use the type System.Uri—an object representation of a uniform resource identifier (URI), which, according to Microsoft Developer Network (MSDN), is “a compact representation of a resource available to your application on the intranet or Internet.” We then check if the value of its AbsoluteURI property is not null and that the Scheme property value (which represents the protocol) contains either http or https. If the condition evaluates to true, True is returned; if not, False is returned. This is a quick way of testing if a URL supplied is in the correct format. We can call the function by typing its name followed by the URL that we want to check.

PS > Check-Url -url http://SPServer01
True
 
Others
 
- Sharepoint 2013 : Security and Policy - SharePoint Users
- Sharepoint 2013 : Security and Policy - Permissions and Permission Levels (part 2) - Creating Custom Permission Levels
- Sharepoint 2013 : Security and Policy - Permissions and Permission Levels (part 1)
- Sharepoint 2013 : Security and Policy - Security Administration
- Installing Exchange Server 2010 : Command-Line Setup (part 2) - Command-Line Server Recovery Options , Command-Line Delegated Server Installation , Installing Language Packs
- Installing Exchange Server 2010 : Command-Line Setup (part 1) - Command-Line Installation Options
- Installing Exchange Server 2010 : Graphical User Interface Setup
- Installing Exchange Server 2010 : Preparing for Exchange 2010 Ahead of Time (part 2) - Preparing the Active Directory Forest,Preparing Additional Domains
- Installing Exchange Server 2010 : Preparing for Exchange 2010 Ahead of Time (part 1) - Existing Exchange Organizations , Preparing the Schema
- Securing an Exchange Server 2007 Environment : Securing Outlook Web Access
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us