Logo
Windows XP
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
World Cup news
 
 
Windows Server

Using Windows PowerShell in an Exchange Server 2010 Environment : Creating Your Own Scripts

3/24/2011 6:06:20 PM
The Exchange Management Console contains many built-in cmdlets. Administrators can create their own scripts using the PowerShell ISE or a common text editor by using one or more cmdlets, which typically execute in order. The script is stored in a text file with a .PS1 extension.

The PowerShell common language runtime is an interpretive environment, meaning that cmdlets, functions, and scripts are loaded into random access memory (RAM), where they are validated and executed. In EMS, this is performed on the Exchange server.

The Exchange Management Shell and PowerShell define several types of commands that administrators can use in development. These commands include functions, filters, scripts, aliases, and executables (applications). The main command type discussed in this article is a simple, small command called a cmdlet. Both EMS and PowerShell supply sets of cmdlets and fully support cmdlet customization to suit the organization’s environment. The PowerShell/EMS runtime processes all cmdlets.

An EMS cmdlet is a simple set of Exchange Server-specific commands bundled together to interact with a managed application (Exchange Server) and the operating system. It is similar to a built-in command in any other shell, such as Cmd.exe, Bash, or ksh. A conventional shell processes most commands as separate executable programs. Each program must parse the input and parameters, bind values to the correct parameters, format the output, and display the output.

EMS, in contrast, processes commands as instances of .NET classes and objects. The administrator must provide the necessary parameters and values, and then supply details of object types and formatting. EMS does the rest of the work: parsing the parameters and binding them to their values, formatting the output, and then displaying the output.

Demonstrating Cmdlet Examples

The administrator can run a cmdlet singly or as one of several cmdlets piped together on the command line. For example, the single cmdlet

Get-AddressList

returns all attributes of an address list or set of address lists. The pipelined command

Get-AddressList | export-csv "C:\AddressList.csv"

produces a collection of address lists and pipelines it to the export-csv cmdlet, which requires the file path and name parameter to create a CSV file.

The following example is a custom script line that displays all public folders, their message counts, and total message sizes in a table format:

get-PublicFolder -recurse | get-PublicFolderStatistics | select-object name,itemCount,totalItemSize


Although this is only a single-line command, it can be tedious to type every time it is needed. It can be typed into a text editor and saved as a .ps1 file, PFSize.ps1 for example, in the system path so that it can easily be run again and again.

A working knowledge of .NET is required to write more complex functions that access objects and classes that are not exposed using the built-in cmdlets. The following cmdlet example uses the system.net.mail.smtpClient class in .NET to send a Simple Mail Transfer Protocol (SMTP) email to a nonauthenticating SMTP server using the EMS or PowerShell command line:

$SmtpServer = "server1.companyabc.com"
$From = "EMStest@companyabc.com"
if ($args.Length -lt 1) {
$To = "administrator@companyabc.com"
}
else {
$To = $args[0]
}
$Subject = "Greetings from EMS!"
$Body = "Hello, this is a test from the Exchange Management Shell."
$SmtpClient = new-object system.net.mail.smtpClient
$SmtpClient.host = $SmtpServer
$SmtpClient.Send($From, $To, $Subject, $Body)

This cmdlet takes an argument, or parameter. If the cmdlet is saved as TestMail.ps1, the administrator can issue the following command to send a test SMTP email:

TestMail testuser@companyabc.com

The book Script the World Using PowerShell is a good resource for more detail on writing scripts using cmdlets.

Combining Functions to Create a Cmdlet Library

As the administrators become more familiar with EMS and using and writing cmdlets, they will begin to build a library of commonly used cmdlets and scripts. It is common to “recycle” similar cmdlets to use for different tasks. Over time, administrators will find useful scripts and concepts from many resources: colleagues, search engines, scripting blogs, newsgroups, and so on.

It is sometimes useful to put all the cmdlets in a common area where other administrators, users, and developers can peruse them and add to the knowledge base. Often, a fellow administrator needs to perform the same task that another administrator has already written. There is no reason to “reinvent the wheel.”

A common practice is to create a network or DFS share where administrators and cmdlet developers have modify permissions and other users have read and execute access permissions. Arrange the folder structure based on business needs and technical requirements.

Modifying and Applying Server Cmdlets to Other Systems

After a cmdlet has been written and tested, it is often useful to run the same cmdlet against many or all servers in the organization. For example, consider the following cmdlet that configures the external URL for OWA on SERVER1:

Set-OwaVirtualDirectory -Identity 'SERVER1\owa (Default Web Site)' -ExternalUrl 'https://mail.companyabc.com/owa'


It is easy to convert this cmdlet so it will run against all client access servers in the Exchange Server organization using pipelining:

Get-OwaVirtualDirectory | Set-OwaVirtualDirectory -ExternalUrl 'https://mail.companyabc.com/owa'


In this example, Get-OwaVirtualDirectory returns a collection of all the OWA Virtual Directories in the Exchange Server organization and pipes them to the Set-OwaVirtualDirectory cmdlet, where it assigns the value.

Top Search -----------------
- Windows Server 2008 R2 : Work with RAID Volumes - Understand RAID Levels & Implement RAID
- Windows Server 2008 R2 Administration : Managing Printers with the Print Management Console
- Configuring Email Settings in Windows Small Business Server 2011
- Windows Server 2008 R2 : Configuring Folder Security, Access, and Replication - Implement Permissions
- Monitoring Exchange Server 2010 : Monitoring Mail Flow
- Windows Server 2008 R2 :Task Scheduler
- Windows Server 2008 R2 : File Server Resource Manager
- Windows Server 2008 R2 : Installing DFS
- Exchange Server 2010 : Managing Anti-Spam and Antivirus Countermeasures
- Windows Server 2008 R2 : Configuring Folder Security, Access, and Replication - Share Folders
Other -----------------
- Windows Server 2008 R2 : Installing Windows Deployment Services (part 3)
- Windows Server 2008 R2 : Installing Windows Deployment Services (part 2)
- Windows Server 2008 R2 : Installing Windows Deployment Services (part 1) - Configuring the WDS Server
- Windows Server 2008 R2 Windows Deployment Services
- Windows Server 2008 R2 Administration Tools for Desktops : Operating System Deployment Options
- Windows Server 2008 R2 Administration Tools for Desktops : Managing Desktops and Servers
- Backing Up Windows Server 2008 (part 2) - Individual Component Backup
- Backing Up Windows Server 2008 (part 1) - Full Server Backup
- Exchange Server 2010 : Mailbox Configuration (part 5) - Import, Export and Archive Mailboxes
- Exchange Server 2010 : Mailbox Configuration (part 4) - Moving Mailboxes & Disabling, Removing, and Reconnecting Mailboxes
 
 
Most view of day
- Windows Server 2008 R2 : Hyper-V feature focus - Introduction to Virtualization and Hyper-V, Hyper-V Changes
- BizTalk Server 2009 Operations : Maintaining the BizTalk Group (part 3) - Restore Procedures
- Microsoft Visio 2010 : Adding Graphics to Your Documents - Adding Clip Art to a Document - Finding and Inserting a Clip Art Graphic
- Microsoft Exchange Server 2010 : Managing Connectivity with Hub Transport Servers - Transport Improvements in Exchange Server 2010
- Windows Server 2012 Requirements and Installation : Switching Between Install Modes
- Microsoft Dynamic GP 2010 : Sales Order Processing (part 1) - Sales Order Processing Setup, Sales Document Setup
- Designing and Configuring Unified Messaging in Exchange Server 2007 : Monitoring and Troubleshooting Unified Messaging (part 1) - Active Calls , Connectivity
Top 10
- Windows Server 2012 : Configuring IPv6/IPv4 interoperability (part 7) - ISATAP
- Windows Server 2012 : Configuring IPv6/IPv4 interoperability (part 6) - Configuring a DHCPv6 server, IPv6 transition technologies
- Windows Server 2012 : Configuring IPv6/IPv4 interoperability (part 5) - Stateless address autoconfiguration,Stateful address autoconfiguration
- Windows Server 2012 : Configuring IPv6/IPv4 interoperability (part 4) - IPv6 address assignment - Manual address assignment
- Windows Server 2012 : Configuring IPv6/IPv4 interoperability (part 3) - IPv6 address representation
- Windows Server 2012 : Configuring IPv6/IPv4 interoperability (part 2) - Default IPv6 functionality
- Windows Server 2012 : Configuring IPv6/IPv4 interoperability (part 1) - IPv6 concepts and terminology
- Multi-Tenancy in SharePoint 2013 (part 2) - Multi-Tenant Use Cases, Partitioning in the Enterprise
- Multi-Tenancy in SharePoint 2013 (part 1) - Managing Service Application Groups, Creating a Site Subscription
- Sharepoint 2013 : Service Application Administration (part 4) - Setting Up the Farm Trust, Publishing a Service Application
Windows XP
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
Cars Review