Exchange-PowerShell Create PowerShell functions

Source: Internet
Author: User

The function is to merge some column commands into a reusable block of code, and then we just need to call this function. Functions can be configured to change or return one or more objects that can be displayed in the console or output to an external file. You can pass the output of a function to a variable, or to another command. In this section, we'll learn how to create a PowerShell function

to create a function, we need to use the keyword Function , followed by the name of the function, and then the code of the function is appended to the curly brace {} . Let's take a look at an example, which is a basic function that will display three properties of a mailbox in a list

function Get-mailboxlist {

Param ($name)

Get-mailbox $name | FL name,alias,servername

}

650) this.width=650; "title=" 01.png "style=" Float:none; "src=" http://s3.51cto.com/wyfs02/M00/59/40/ Wkiom1tmpkmjg1kjaacnpr3ycfa127.jpg "alt=" Wkiom1tmpkmjg1kjaacnpr3ycfa127.jpg "/>

how PowerShell functions work

PowerShell function allows us to run a sequence of commands with a simple function name. We can add the input parameters to the function or as a pipe input. This allows us to write our own reusable function, which is similar to a command.

There are several ways you can add function functions to your Shell the session.

First, You can save the function function as a . PS1 suffix file, and then navigate to the current file under Run function command

Second, You can save the function function as a . PS1 file and then configure the PowerShell configuration file

PowerShell A function can be written to accept and execute a process pipeline input, which can be used in three stages, respectively: Begin , Process and the End block.

Begin: at the beginning of the function, Begin blocks run only once, and any customizations and initializations can occur here

Process : This process block runs once per object pipe, and each object, through the pipeline, can access the $_automatic variables

End:end block at all Process processing after Operation

below we use Process Block Create a simple pipe function. the Begin block and End block are optional. For example, the following example function will return the name of each mailbox through a pipeline

function Get-mailboxname {

Process {

"Mailbox Name: $ ($_. Name) "

}

}

we can use Get-mailbox command to pass information to the function, the name of each mailbox will be returned:

650) this.width=650; "title=" 02.png "style=" Float:none; "src=" http://s3.51cto.com/wyfs02/M01/59/3D/wKioL1TMpy6ybq_ Zaafavi6bdrm801.jpg "alt=" Wkiol1tmpy6ybq_zaafavi6bdrm801.jpg "/>

PowerShell function Advanced Experience

Here we combine a practical example, combining Get-mailboxstatistics and the Set-mailbox into a function that automates some tasks through this function for demonstration PowerShell Functions of the function

The following function sets a mailbox's Prohibitsendreceivequota , configure the mailbox name and the required quota size. This function only modifies the current user's quota value beyond the limit set for the mailbox user.

function Set-sendreceivequota {

Param

[Parameter (mandatory= $true, valuefrompipelinebypropertyname =

$true)]

$name,

[Parameter (mandatory= $true)]

$quota

)

Begin {

$count = 0

Write-output "Started: $ (get-date-format T)"

}

Process {

$count + = 1

$mailboxstatistics = Get-mailboxstatistics $name

$total = $mailboxstatistics. TotalItemSize.Value.ToMB ()

if ($total-lt $quota) {

Set-mailbox $name-prohibitsendreceivequota $quota '

-usedatabasequotadefaults $false

}

}

End {

Write-output "Ended: $ (get-date-format T)"

Write-output "Mailboxes processed: $count"

}

}

650) this.width=650; "title=" 03.png "src=" http://s3.51cto.com/wyfs02/M01/59/40/wKiom1TMpkqh1M_JAAH1sb_rPjw969.jpg "alt=" Wkiom1tmpkqh1m_jaah1sb_rpjw969.jpg "/>

To run a function command:

650) this.width=650; "title=" 04.png "style=" Float:none; "src=" http://s3.51cto.com/wyfs02/M02/59/3D/ Wkiol1tmpy-y7qs8aabtwrg3dai145.jpg "alt=" Wkiol1tmpy-y7qs8aabtwrg3dai145.jpg "/>

this Bebin Block run only once at the beginning, and when the function command is called, the start time is quickly displayed. In process Block , we used $count variables to track how many objects were processed. the end Block finally runs, reporting the total number of processing items. In the previous example, we can see that the output is displayed very quickly, and below we demonstrate the processing of a collection of mailboxes to look at:

650) this.width=650; "title=" 05.png "style=" height:407px;width:720px; "border=" 0 "hspace=" 0 "src=" http:// S3.51cto.com/wyfs02/m02/59/40/wkiom1tmpkqjk5hwaask6_nu5qo720.jpg "width=" 720 "height=" 407 "alt=" Wkiom1tmpkqjk5hwaask6_nu5qo720.jpg "/>

Since some of my mailboxes have never landed, you may see some warning messages. But we can see the start time and the end time, as well as the number of mailboxes processed.

This article from "Robin's Home" blog, declined reprint!

Exchange-PowerShell Create PowerShell functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.