How to set up your Exchange Server Scripting Environment using PowerShell

Home > Blogs > Exchange Server > How to set up your Exchange Server Scripting Environment using PowerShell

How to set up your Exchange Server Scripting Environment using PowerShell

1 0 Mike Pfeiffer
Added by August 19, 2013

How to set up your Exchange Server Scripting Environment using PowerShell
Instructor: Mike Pfeiffer, Microsoft Exchange MVP

You’re going to spend a lot of time in the Exchange Management Shell (EMS) doing things interactively. For example one‑liners maybe like get-mailbox and sending that down to Set-Mailbox, setting the -Office to Sales etc…

001-example-Exchange-Server-Scripting-using-PowerShell

Obviously, this isn’t a great example of a one liner but this is probably something that you would normally do. Now when it comes to building more robust enterprise type of scripts that are running a series of commands maybe four or five different commands all in sequence. Typically people save those in a PS1 Script that can be run over-and-over again.

When it comes to developing those scripts a lot of times people use a scripting editor instead of just you know plain text editor like Notepad. So on Windows 7 you get a scripting editor called the Windows PowerShell Integrated Scripting Environment or what we normally call the ISE.

002-ise-scripting-editor-Exchange-Server-Scripting-using-PowerShell

This comes installed by default. There are also some commercial editors out there as well. There are some good free ones out on the Internet.

But this one typically is easy to use. It’s installed on Windows 7. You can install it as an additional feature on 2008 R2 so it’s just easy to get to but typically what will happen is you’ll come in here post your scripts in this top pane.

003-ise-scripting-editor-Exchange-Server-Scripting-using-PowerShell

Notice that you can just save the PS1 file from there. If you run any commands out of these environments you’ll actually get the output in here.

004-ise-get-service-Exchange-Server-Scripting-using-PowerShell

You can interactively kind of test things out. So for example I can do something like get service now where objects status running.

So this is kind of the beginning of my script. I can run that. I can get the output here in the middle. I can also come down here and do things like service bits.

Kind of check that one then I could know oh yeah the property status.

005-ise-status-Exchange-Server-Scripting-using-PowerShell

So I need to kind of do that so that’s kind of the idea. Now the thing here is that I come in here and do a get-mailbox and run it, I actually get an error and that’s because the Exchange Management Shell environment is not loaded in here by default.

006-ise-get-mailbox-error-Exchange-Server-Scripting-using-PowerShell

Same thing goes for the “blue shell” and run get-mailbox in this environment without first kind of initializing Exchange. It has no idea that the actual tools that are installed locally.

0065-blue-shell-get-mailbox-error-Exchange-Server-Scripting-using-PowerShell

You might not need them tools installed locally, you might want to do that on a machine that does not have them. So let’s take a look at how you would kind of approach this.

What I typically do is set up a profile. Let me show you the profiles that are installed by default or actually set up for you by default. $profile.psextended | fL

007-set-profile-Exchange-Server-Scripting-using-PowerShell

We have four specific for PowerShell.exe.

AllUsersAllHosts simply means that if I create this PS1 script here:

008-set-profile-Exchange-Server-Scripting-using-PowerShell

It’s like a log in script for PowerShell so I can put my initialization commands there. Any user that logs into the machine regardless of which host they use will get the settings in this profile.ps1 file. So I have to manually create this file in order for that to happen.

Now since I’m the only user in this machine I’m just going to do CurrentUserAllHosts.

009-currentuserallhosts-Exchange-Server-Scripting-using-PowerShell

This means that whether I run PowerShell here or in the PowerShell cmd line, or PowerShell in the ISE, all of the settings defined here are going to get loaded for me.

So that’s what I’m going to do. I’m actually going to create the CurrentUserAllHosts script. So I’m going use New-Item –ItemType file –path C:\Users\administrator\Documents\WindowsPowerShell\profile.ps1 -Force

010-currentuserallhosts-Exchange-Server-Scripting-using-PowerShell

Now I’m going to create that file and open it up in the ISE.

011-open-profile-ise-Exchange-Server-Scripting-using-PowerShell

So in here in the profile I’ll go ahead and set up my remote Exchange connection:

$s = NewPSSession –ConfigurationName Microsoft.exchange –ConnectionUri http://exchange.uss.local/powershell

Import-PSSession $s

012-ISE-new-pssession-Exchange-Server-Scripting-using-PowerShell

So what will happen here when I save this regardless of which instance of the PowerShellI start, I’ll actually go out of the Exchange Server, import the cmdlets and then I’ll have those commands available to me.

I’m going to restart the ISC here so we can see this in action.

013-restart-ISE-Exchange-Server-Scripting-using-PowerShell

Notice that when we first start this up it’s kind of hanging a little bit it’s actually running a script. So it’s actually executing my profile right now and that’s why the stop button is lit up. It’s waiting right now for that initialization to complete.

Here’s the import of my PowerShell command for Exchange.

014-restart-ISE-Exchange-Server-Scripting-using-PowerShell

Once it is complete I can start composing my Exchange Management Shells (EMS) scripts in here.

015-ISE-profile-complete-Exchange-Server-Scripting-using-PowerShell

But just to test this out interactively I can run and get-mailbox. In the output pane you can see that I get the results there.

015-ISE-test-get-mailbox-Exchange-Server-Scripting-using-PowerShell

Up in the top, I can do get mailbox and of tab through all the different options available that help write scripts effectively.

017-ISE-test-get-mailbox-Exchange-Server-Scripting-using-PowerShell

I also get the code highlighting. I get the debugging capabilities of the ISE as well.

Now there is one drawback to using this Manual Implicit Remoting. When I manually import this command into the main scripting environment, I lose some features.

Let me show you this real quick. Let me take the Exchange Management Shell and we’ll look at the administrator mailbox, get mailbox statistics administrator.

019-EMS-limitations-Exchange-Server-Scripting-using-PowerShell

Notice that what I get back with this cmdlet is the number of items in the mailbox. If I select total item size, how big is the mailbox type of thing.

020-EMS-limitations-Exchange-Server-Scripting-using-PowerShell

Now if I save this in a variable like $var to make this a little easier to work with.

021-EMS-limitations-Exchange-Server-Scripting-using-PowerShell

I can actually go as far as doing something like $var.TotalItemSize

022-var-Exchange-Server-Scripting-using-PowerShell

Notice that I get back two properties. So total item size is really kind of like a rich property. It’s an object. This point we are saying we have a property that is unlimited and then the value.

But the thing I really want to outline here is if we do a .value with Get-Member. You can see that it’s got some really interesting methods.

023-var-get-member-Exchange-Server-Scripting-using-PowerShell

So I might not want the byte information. I don’t want these in kilobytes maybe I want this value represented in megabytes. So I could do that. I could do value.ToMB.

In this case that’s not going to be anything because it’s only a couple hundred k. Just to give you an idea here, run ToMB(), and convert that to ToKb().

024-var-get-member-Exchange-Server-Scripting-using-PowerShell

That strips out the byte information so I just get the actual kilobytes or megabytes or gigabytes or whatever.

Now let’s do this over in the “blue” shell.

PS C:\> $var = Get-MailboxStatistics administrator

PS C:\> $var.TotalItemSize

025-var-get-member-Exchange-Server-Scripting-using-PowerShell

Then we’ll do:

PS C:\> $var.TotalItemSize.value.tokb()

026-var-error-Exchange-Server-Scripting-using-PowerShell

It produces this error. So let’s double check this in the EMS.

027-EMS-Exchange-Server-Scripting-using-PowerShell

Just to make sure that I’m not typing it wrong let’s copy that and paste it in the main screen.

028-var-error-Exchange-Server-Scripting-using-PowerShell

Still getting an error. So here’s the thing, let’s take a look at this with get-member. If you look at this property with get member notice that in this case total item size is represented as a string.

030-get-member-string-Exchange-Server-Scripting-using-PowerShell

If we do the same thing over here in this version of the shell, let me clear that. Total item size in this environment the data type is this crazy microsoft.exchange.data.unlimited and on and on.

031-get-member-string-Exchange-Server-Scripting-using-PowerShell

Thing is this is the exact property we’re looking at here. The difference is that in this environment we got some additional DLLs loaded even though we are still connected remotely to this Exchange Server and running the commands remotely.

The Shell environment has loaded some data in here so we get this rich objects where we can do things like run the 2kb method or 2md method. When we’re in the “blue” script, it’s represented in string.

032-string-Exchange-Server-Scripting-using-PowerShell

You will know flat string data because we might be running this on a machine that doesn’t have the tools installed. When I’m actually working in this kind of ISE environment in my profile.ps1

033-string-Exchange-Server-Scripting-using-PowerShell

I really don’t want to use this because I kind of lose some of these features, some of those rich objects.

So if I go to the tools installed on my machine what I will do is I will take the actual properties of this shortcut.

034-EMS-Exchange-Server-Scripting-using-PowerShell

And use that as my initialization command for my profile.

035-EMS-Exchange-Server-Scripting-using-PowerShell

So I don’t need to run powershell.exe but I do want to import the PS1 script and I do want it to automatically connect to Exchange.

036-EMS-Exchange-Server-Scripting-using-PowerShell

So in that case when I load the Exchange in my environment into my scripting editor, I’ll get all of the commands and I’ll also get those rich kind of objects that I wouldn’t normally get with that Manual Implicit Remoting.

So I’ll save this and restart the ISE. Upon restarting you can see again it’s running my PowerShell profile and importing those commands. Not a lot has changed at this point. Eventually what I’ll get is my Exchange Management Shell environment and everything will work great. I should be able to run my Exchange commands at this point.

037-EMS-Exchange-Server-Scripting-using-PowerShell

That indeed looks like it’s actually working.

So if you don’t have the actual tools installed the other method to manually import the commands is fine but if you’re on a machine that does have the tools installed I recommend you set up your profile in this fashion. Then again take this information straight out of this Exchange Management Shell shortcut and that will just work just fine. Now the catch to this is at this point this profile is set to current user all hosts.

So if I were to run the Exchange shell at this point I would be easily trying to run these commands twice. So what I would want to do if I’m going to use this configuration is just modify my shortcut and basically tell it to not use the profile when it starts powershell.exe. I would come here and modify this and say -noprofile.

038-EMS-no-profile-Exchange-Server-Scripting-using-PowerShell

That would mean I could go ahead and start this without any errors and the Exchange Management Shell would be loaded into any PowerShell instance that I would start on this machine. So let me just make sure that this loads up OK.

So that comes out just fine. No red errors and everything looks good.

039-EMS-no-profile-Exchange-Server-Scripting-using-PowerShell

So that’s process of getting your environment set up for Scripting for Exchange. From here I can go out and compose my scripts save them off to a directory with a PS1 extension. I can execute them either interactively or automatically through some kind of scheduled task.

Using the profile and loading the cmdlets for Exchange should work regardless of whether using the ISE or some of the kind of commercial scripting editors.

Videos You May Like

A Simple Introduction to Cisco CML2

0 3896 0

Mark Jacob, Cisco Instructor, presents an introduction to Cisco Modeling Labs 2.0 or CML2.0, an upgrade to Cisco’s VIRL Personal Edition. Mark demonstrates Terminal Emulator access to console, as well as console access from within the CML2.0 product. Hello, I’m Mark Jacob, a Cisco Instructor and Network Instructor at Interface Technical Training. I’ve been using … Continue reading A Simple Introduction to Cisco CML2

Creating Dynamic DNS in Network Environments

0 642 1

This content is from our CompTIA Network + Video Certification Training Course. Start training today! In this video, CompTIA Network + instructor Rick Trader teaches how to create Dynamic DNS zones in Network Environments. Video Transcription: Now that we’ve installed DNS, we’ve created our DNS zones, the next step is now, how do we produce those … Continue reading Creating Dynamic DNS in Network Environments

Cable Testers and How to Use them in Network Environments

0 727 1

This content is from our CompTIA Network + Video Certification Training Course. Start training today! In this video, CompTIA Network + instructor Rick Trader demonstrates how to use cable testers in network environments. Let’s look at some tools that we can use to test our different cables in our environment. Cable Testers Properly Wired Connectivity … Continue reading Cable Testers and How to Use them in Network Environments

Write a Comment

Share your thoughts...

Please fill out the comment form below to post a reply.