Archive

Archive for December, 2011

Function: New-LocalExchangeConnection – Ensure Your PowerShell Script is Connected to Exchange 2010

December 28, 2011 11 comments

When writing scripts that execute commands on an Exchange server, for Exchange 2010, it’s important to ensure that you’re running within a session connected to an Exchange server, and that all Exchange cmdlets are available. In Exchange 2007, we could load the Exchange snapins. But 2010 doesn’t use snapins. Some people would say that you can simply load the modules, but loading the modules bypasses RBAC, and is thus, not recommended. Mike Pfeiffer wrote a great article Managing Exchange 2010 with Remote PowerShell that sheds some light. It’s worth a read.

A solution around this is to run a PowerShell script that comes built into Exchange 2010. This makes available the Connect-ExchangeServer cmdlet, which will connect via remote PowerShell to Exchange. We can specify a server, or let the -auto option connect to the best server via autodiscover. New-LocalExchangeConnection is a function I wrote to connect:

function New-LocalExchangeConnection	{ 
	[cmdletBinding(SupportsShouldProcess = $true)]
	param(
	)
	Write-Verbose "Checking for Exchange Management Shell"
	$Sessions = Get-PSSession | Where-Object {$_.ConfigurationName -eq "Microsoft.Exchange"}
	if (!($Sessions)){
		if (Test-Path "$env:ExchangeInstallPath\bin\RemoteExchange.ps1"){
			Write-Verbose "Exchange Management Shell not found - Loading..."
			. "$env:ExchangeInstallPath\bin\RemoteExchange.ps1"
			Write-Verbose "Exchange Management Shell loaded"
			Write-Verbose "Connecting to Exchange server"
			Connect-ExchangeServer -auto
			if (Get-PSSession | Where-Object {$_.ConfigurationName -eq "Microsoft.Exchange"}){
				Write-Verbose "Connected to Exchange Server"
			}else{
				Write-Host "An error has occurred" -ForegroundColor red
			}
		}else{
			Write-Warning "Exchange Management Shell is not available on this computer"
		}
	}else{
		Write-Verbose "Exchange Management Shell already loaded"
	}
} # end function New-LocalExchangeConnection

Calling this within your script will make ensuring that your script is running with access to Exchange cmdlets much simpler.

Function: Set-ModuleStatus – PowerShell Function for Loading and Verifying Modules

December 27, 2011 12 comments

PowerShell logo 128x128Often in my PowerShell scripts, I need to either load a specific module, or verify it’s loaded. Manually, of course, we can use Get-Module, Import-Module, etc. But in automation scripts, we need to programtically make sure the module is loaded or load it if it’s not. I wrote this function and it’s worked well for me. introducing Get-ModuleStatus:

function Set-ModuleStatus { 
	[CmdletBinding(SupportsShouldProcess = $True)]
	param	(
		[parameter(ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, Mandatory = $true, HelpMessage = "No module name specified!")] 
		[ValidateNotNullOrEmpty()]
		[string] $name
	)
	PROCESS{
		# Executes once for each pipeline object
		# the $_ variable represents the current input object		
		if (!(Get-Module -name "$name")) { 
			if (Get-Module -ListAvailable | Where-Object Name -eq "$name") { 
				Import-Module -Name "$name" 
				# module was imported
				# return $true
			} else {
				# module was not available
				# return $false
			}
		} else {
			# Write-Output "$_ module already imported"
			# return $true
		} 
	} # end PROCESS
} # end function Set-ModuleStatus

Call it supplying the module name, such as

Set-ModuleStatus Lync

You can use logic such as

if (Set-ModuleStatus Lync){Write-Host "Lync module is loaded}else{Write-Host "Lync module is NOT loaded" -ForegroundColor red}

Simple and effective. You can also pipe module names to it, such as:

“lync”,”activedirectory” | Set-ModuleStatus

Download

v1.2 – 02-07-2014 – Set-ModuleStatus.v1.2.zip

v1.1 – 09-17-2012 – Set-ModuleStatus.v1.1.zip

v1.0 Get-ModuleStatus.ps1

Categories: PowerShell Tags: ,

Function: New-Password – Creating Passwords with PowerShell

December 26, 2011 2 comments

PowerShell logo 256x256When creating new accounts, an admin needs to assign a password. We often then check the box to force a user to change their password when they logon for the first time. Some organizations will use a ‘default’ password for all new accounts. That’s fraught with security implications, and I’ve never recommended it. The downside is that you, as an admin, need to think up a password for each new account. I know how it is – you look around at things on your desk, items on the wall, looking for ideas. Then you have to make sure your super password meets your organizations password requirements, including length and complexity. Well, no more!

Enter New-Password. This function takes one simple input – length. It then spits out a password of said length, using upper and lower case letters, numbers, and punctuation, as well as a phonetic version. If you choose not to use some of the punctuation characters, feel free to just put a ‘#’ in front of that particular line.

function New-Password	{
	[CmdletBinding(SupportsShouldProcess=$true)]
	param(
		[int]$length
	)
	$password = -join ([Char[]]'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789%$!#^{}<>' | Get-Random -count $length)
	Write-Host "`n$password`n" -ForegroundColor green
	# Write-Host
	ForEach ($character in [char[]]"$password"){
		[string]$ThisLetter = $character
		switch ($ThisLetter)	{
			a	{$ThisWord = "alpha"}
			b	{$ThisWord = "bravo"}
			c	{$ThisWord = "charlie"}
			d	{$ThisWord = "delta"}
			e	{$ThisWord = "echo"}
			f	{$ThisWord = "foxtrot"}
			g	{$ThisWord = "golf"}
			h	{$ThisWord = "hotel"}
			i	{$ThisWord = "india"}
			j	{$ThisWord = "juliett"}
			k	{$ThisWord = "kilo"}
			l	{$ThisWord = "lima"}
			m	{$ThisWord = "mike"}
			n	{$ThisWord = "november"}
			o	{$ThisWord = "oscar"}
			p	{$ThisWord = "papa"}
			q	{$ThisWord = "quebec"}
			r	{$ThisWord = "romeo"}
			s	{$ThisWord = "sierra"}
			t	{$ThisWord = "tango"}
			u	{$ThisWord = "uniform"}
			v	{$ThisWord = "victor"}
			w	{$ThisWord = "whiskey"}
			x	{$ThisWord = "xray"}
			y	{$ThisWord = "yankee"}
			z	{$ThisWord = "zulu"}
			1	{$ThisWord = "one"}
			2	{$ThisWord = "two"}
			3	{$ThisWord = "three"}
			4	{$ThisWord = "four"}
			5	{$ThisWord = "five"}
			6	{$ThisWord = "six"}
			7	{$ThisWord = "seven"}
			8	{$ThisWord = "eight"}
			9	{$ThisWord = "nine"}
			0	{$ThisWord = "zero"}
			!	{$ThisWord = "!"}
			$	{$ThisWord = "$"}
			%	{$ThisWord = "%"}
			^	{$ThisWord = "^"}
			*	{$ThisWord = "*"}
			-	{$ThisWord = "-"}
			_	{$ThisWord = "_"}
			:	{$ThisWord = ":"}
			`;	{$ThisWord = ";"}
			`{	{$ThisWord = "{"}
			`}	{$ThisWord = "}"}
			`/	{$ThisWord = "/"}
			`<	{$ThisWord = "<"}
			`>	{$ThisWord = ">"}
			`#	{$ThisWord = "#"}
			`{	{$ThisWord = "{"}
			`}	{$ThisWord = "}"}
		}
		if ($ThisLetter -cmatch $ThisLetter.ToUpper()){
			$ThisWord = $ThisWord.ToUpper()
		}
		Write-Host "$ThisWord " -NoNewLine -ForegroundColor yellow
	}
	Write-Host "`n"
} # end function New-Password

Now, stick that function in your PowerShell profile. Each time you need a new password, use

New-Password -length [number]

such as

New-Password -length 12

And you now have a password to use.

New-Password

New-Password

 

Functions: Get-LocalAdminGroupMembership and Set-LocalAdminGroupMembership – Local Admin Group Membership on Remote Machines

December 22, 2011 1 comment

PowerShell logo 128x128While writing some PowerShell scripts to automate the installation of Exchange on over 100 servers, I needed to set and then verify that a group (in this case, “Exchange Trusted Subsystem”) was a member of the local admins group on some remote servers.

We start with Get-LocalAdminGroupMembership. This function merely checks the local admins group on a remote server to see if the group to be added is already a member. If it is, it returns $true, if not, $false. We need to pass it two variables: $ComputerName, and $Member. We don’t need to run this function. It’s called from the second function.

function Get-LocalAdminGroupMembership	{
	[CmdletBinding()]
	Param(
		[Parameter(Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
		$ComputerName = ".",
		[Parameter(Position=1, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
		$Member
	)
	if($ComputerName -eq '.'){$ComputerName = (get-WmiObject win32_computersystem).Name}
	$computer = [ADSI]("WinNT://" + $ComputerName + ",computer")
	$Group = $computer.psbase.children.find("Administrators")
	$members= $Group.psbase.invoke("Members") | % {$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)}
	if ($members -match $member){return $true}else{return $false}
} # end function Get-LocalAdminGroupMembership

 

The second function does all the heavy lifting.

function Set-LocalAdminGroupMembership {
	[CmdletBinding()]
	Param(
		[Parameter(Position=0, Mandatory=$false, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
		[string]$ComputerName = '.',
		[Parameter(Position=1, Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
		[string]$Member,
		[Parameter(Position=2, Mandatory=$false, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
		[string]$Domain = $env:USERDNSDOMAIN
	)

	Process{
		if (!(Get-LocalAdminGroupMembership -ComputerName "$ComputerName" -Member "$Member")){
			if($ComputerName -eq '.'){$ComputerName = $env:ComputerName.ToUpper()}    

			if($Domain){
  			$adsi = [ADSI]"WinNT://$ComputerName/administrators,group"
    		$adsi.Add("WinNT://$Domain/$Member,group")
			}else{
	  		Write-Host "Not connected to a domain." -ForegroundColor "red"
			}
		} else {
			Write-Host "`"$Account`" is already a local admin on $ComputerName" -ForegroundColor yellow
		}
		Get-LocalAdminGroupMembership -ComputerComputer "$ComputerName" -Member "$Member"
	}# Process
} # end function Set-LocalAdminGroupMembership

We call Set-LocalAdminGroupMembership and pass it the same parameters, $ComputerName and $Member

Set-LocalAdminGroupMembership -ComputerName mycomputer -Member "Exchange Trusted Subsystem"

The function will add the group to the local admins group, and then do a Get-LocalAdminGroupMembership for that same group and dump the results to the screen.

Function: New-Pause – Pausing PowerShell Scripts

December 21, 2011 Leave a comment

Yesterday, I wrote about a sleep function to cause a predetermined delay in a script. Today, I give you a short function, New-Pause. New-Pause stops a script and waits for the user to press a key before continuing.

function New-Pause {
 Write-Host "Press any key to continue" -ForegroundColor green
 $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
} # end function New-Pause

Call the function using

New-Pause
New-Pause

New-Pause

Once any key is pressed, your script can continue.

Categories: PowerShell Tags: ,

Function: New-Sleep – When You Need a Delay

December 20, 2011 6 comments

On a recent project, I needed some PowerShell scripts to wait for a few seconds just to ensure that some other processes were finished and I wasn’t issuing too many commands to some Exchange servers too quickly. I came up with this little function:

function New-Sleep {
	[cmdletbinding()]
	param(
		[parameter(ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, Mandatory=$true, HelpMessage="No time specified")]
		[int]$s
	)
	for ($i=1; $i -lt $s; $i++) {
	[int]$TimeLeft=$s-$i
	Write-Progress -Activity "Waiting $s seconds..." -PercentComplete (100/$s*$i) -CurrentOperation "$TimeLeft seconds left ($i elapsed)" -Status "Please wait"
	Start-Sleep -s 1
	}
	Write-Progress -Completed $true -Status "Please wait"
} # end function New-Sleep

Call the function like this:

New-Sleep -s 60

Where the value of $s is the number of seconds you want to sleep. The display tells you how long your sleeping for, how much time is left, and how much time has elapsed.

New-Sleep

New-Sleep

Download the function below.

Download

v1.0 New-Sleep.ps1

Categories: PowerShell Tags: ,

December 2011 Technical Rollup: Unified Communications

December 12, 2011 Leave a comment

News

Premier

OpsVault — Operate and Optimize IT
http://www.opsvault.com

Microsoft Premier Support UK – Site Home – TechNet Blogs
http://blogs.technet.com/b/mspremuk/

Antigen & Forefront

Forefront Team Blog – Site Home – TechNet Blogs
http://blogs.technet.com/b/forefront

Forefront Server Security Support Blog – Site Home – TechNet Blogs
http://blogs.technet.com/b/fssnerds

Exchange

  1. Exchange Server 2010 SP2
    http://blogs.technet.com/b/exchange/archive/2011/12/05/released-exchange-server-2010-sp2.aspx
  2. What’s New in Exchange 2010 SP2
    http://technet.microsoft.com/en-us/library/hh529924.aspx
  3. Exchange Server 2010 SP2 Unified Messaging Language Packs
    http://www.microsoft.com/download/en/details.aspx?id=28191
  4. Exchange Server 2010 SP2 Help
    http://www.microsoft.com/download/en/details.aspx?id=28207

Exchange Team Blog – Site Home – TechNet Blogs
http://blogs.technet.com/b/exchange/

MCS UK Unified Communications Blog – Site Home – TechNet Blogs
http://blogs.technet.com/b/msukucc

Hosted Messaging Collaboration

Lync, Office Communication Server & LiveMeeting

  1. Lync Server 2010 Hotfix KB 2493736 (Cumulative Update 4)
    http://www.microsoft.com/download/en/details.aspx?id=11551
  2. Lync Server 2010 Documentation Help File
    http://www.microsoft.com/download/en/details.aspx?id=23888
  3. Lync Server 2010 Mobility Guide
    http://www.microsoft.com/download/en/details.aspx?id=28355
  4. Lync Server 2010 Mobility Service and Lync Server 2010 Autodiscover Service
    http://www.microsoft.com/download/en/details.aspx?id=28356

NextHop – Site Home – TechNet Blogs
http://blogs.technet.com/b/nexthop/

Lync Team Blog – Site Home – TechNet Blogs
http://blogs.technet.com/b/lync/

Outlook

http://blogs.msdn.com/b/outlook

Other

NextHop – Site Home – TechNet Blogs
http://blogs.technet.com/b/nexthop/

The Master Blog – Site Home – TechNet Blogs
http://blogs.technet.com/b/themasterblog

New KBs

Antigen & Forefront

Microsoft Forefront Online Protection for Exchange:

  1. “A synchronization error occurred between your Active Directory environment and the Hosted Archive service” error message in Exchange Hosted Archive
    http://support.microsoft.com/kb/2635303
  2. “An unexpected error has occurred” error when you delete junk email messages from the spam quarantine mailbox in Forefront Online Protection for Exchange
    http://support.microsoft.com/kb/2635327
  3. The sign-in page is not displayed, or a “Page not found” error occurs, after a new version of the Forefront Online Protection for Exchange Administration Center is released
    http://support.microsoft.com/kb/2636060
  4. A blank webpage appears when you click the answer-back URL in a message that is encrypted by Exchange Hosted Encryption in Windows Live Hotmail
    http://support.microsoft.com/kb/2636095
  5. How to add a disclaimer or footer to outgoing mail messages through Forefront Online Protection for Exchange
    http://support.microsoft.com/kb/2639679
  6. The footer for outgoing email messages does not work in Forefront Online Protection for Exchange
    http://support.microsoft.com/kb/2642173
  7. How to create a policy for a group of users in a stand-alone Forefront Online Protection for Exchange environment
    http://support.microsoft.com/kb/2645012

Exchange

Microsoft Exchange Server 2003 Enterprise Edition

  1. You cannot connect to Outlook Mobile Access on a server that is running Exchange Server 2003
    http://support.microsoft.com/kb/2448283
  2. How to redirect an HTTP connection to HTTPS for Outlook Web Access clients and how to redirect the Default Web Site to point to the Exchange virtual directory
    http://support.microsoft.com/kb/839357
  3. Error message when you try to synchronize a Windows Mobile-based device by using Exchange ActiveSync for Exchange 2003 or for Exchange 2007 or for Exchange 2010: “Synchronization failed”
    http://support.microsoft.com/kb/927465
  4. Error message when you use ActiveSync to synchronize a Windows Mobile-based device to Exchange 2003: “0x85030027 — The Exchange Server requires certificates to log on”
    http://support.microsoft.com/kb/927467

Microsoft Exchange Server 2003 Service Pack 2

  1. Incremental changes of free/busy information may not be successful replicated from Exchange Server 2010 to Exchange Server 2003
    http://support.microsoft.com/kb/2601033

Microsoft Exchange Server 2003 Standard Edition

  1. The W3wp.exe process uses almost 100 percent of CPU resources when you synchronize large email messages in Exchange Server 2003 Service Pack 2
    http://support.microsoft.com/kb/941439

Microsoft Exchange Server 2007 Enterprise Edition

  1. The Fax feature stops working in Exchange Server 2007 SP3
    http://support.microsoft.com/kb/2526140
  2. Store.exe intermittently stops responding in an Exchange Server 2007 environment
    http://support.microsoft.com/kb/2638878

Microsoft Exchange Server 2007 Service Pack 1

  1. Email messages cannot be delivered to the Hub Transport server in an Exchange Server 2007 environment
    http://support.microsoft.com/kb/2638876

Microsoft Exchange Server 2007 Standard Edition

  1. “HTTP 400 Bad Request” error when you connect to an Exchange Server 2007 mailbox by using Outlook Web App
    http://support.microsoft.com/kb/2645573
  2. The synchronization session of the mobile device fails, and you receive error code “0X85010015″ when you try to synchronize a mobile device by using Exchange ActiveSync in Exchange Server 2007
    http://support.microsoft.com/kb/934402

Microsoft Exchange Server 2010 Enterprise

  1. Office Communications Server 2007 IM integration with Exchange 2010 OWA does not work for all users
    http://support.microsoft.com/kb/2279487
  2. “Cannot open your default e-mail folder” error when users try to open their mailboxes in Outlook after migration to Exchange 2010
    http://support.microsoft.com/kb/2521770
  3. An Exchange Server 2010 database store grows unexpectedly large
    http://support.microsoft.com/kb/2621266
  4. Email message content is missing in OWA
    http://support.microsoft.com/kb/2640306
  5. Error when you try to change the default global address list recipient filter in Exchange Server 2010
    http://support.microsoft.com/kb/2645013

Microsoft Exchange Server 2010 Standard

  1. Windows Network Load Balancing does not work in an Exchange Server cluster
    http://support.microsoft.com/kb/2644137
  2. You receive an error message when you try to create an Exchange Server 2010 DAG
    http://support.microsoft.com/kb/2644540

Lync, Office Communication Server & LiveMeeting

Microsoft Office Communicator 2007

  1. Additional authentication prompt is displayed when an external network user signs in to an Office Communicator 2007 client
    http://support.microsoft.com/kb/2633194

Microsoft Office Live Meeting 2007

  1. Live Meeting 2007 loads a blank white screen when joining a Live Meeting on Macintosh computer
    http://support.microsoft.com/kb/2628045
  2. How to use Microsoft Office Live Meeting 2007 on a Macintosh computer
    http://support.microsoft.com/kb/884961

Outlook

Microsoft Office Outlook 2003

  1. Description of the Outlook 2003 Junk Email Filter update: November 8, 2011
    http://support.microsoft.com/kb/2596972
  2. “80004005-501-4B9-560″ synchronization error logs in the Outlook Sync Issues folder
    http://support.microsoft.com/kb/2637470

Microsoft Office Outlook 2007

  1. Outlook receives a message that has an attachment that is named “not supported calendar message.ics”
    http://support.microsoft.com/kb/2643084

Microsoft Outlook 2000 Standard Edition

  1. Outlook Meeting Request to DL Shows Members As Optional
    http://support.microsoft.com/kb/214633
  2. OL2000: (CW) Sending Pasted Bitmap from Word Behaves Differently
    http://support.microsoft.com/kb/218362

Microsoft Outlook 2010

  1. Description of the Outlook 2010 update: November 8, 2011
    http://support.microsoft.com/kb/2553323
  2. You receive an error message when you publish Internet free/busy information in Outlook 2010
    http://support.microsoft.com/kb/2589415
  3. The Mailbox Cleanup Wizard does not start in Outlook 2010 when the mailbox is full
    http://support.microsoft.com/kb/2632283
  4. Outlook 2010 does not display Journal entries for a contact
    http://support.microsoft.com/kb/2639664
  5. Outlook Issues that occur when you use the ExtractOrganizedMeetings registry value
    http://support.microsoft.com/kb/2646698

Exchange Server 2010 SP2 Is Now Available

December 5, 2011 1 comment

Microsoft has released Service Pack 2 (SP2) for Exchange Server 2010. Release Notes for Exchange 2010 SP2 includes a list of known issues.

The 535MB download is just like RTM – a full install package. Existing installations can be upgraded, as new installs can be completed with the Service Pack integrated.

 

What’s New in Exchange 2010 SP2

The What’s New in Exchange 2010 SP2 has comprehensive list of the changes and enhancements, including:

Address Book Policies

This is a long sought after feature which allows the segmentation of the Global Address List. This essentially allows an organization to have different address books visible to different users and/or groups. This is great, especially in large organization that may want users to just see users in their division, or if an org wants to do a multi-tenant scenario.

Outlook Mobile Access (OWA mini)

This is essentially the old Outlook Mobile Access brought back to life, which allows devices with small screens, such as mobile devices, to see a simple to use web page for accessing their mailbox. This is great for devices that don’t support Exchange ActiveSync.

Hybrid Configuration Wizard

This feature is based around on-premise and cloud based scenarios such as Office365.

OWA Cross-Site redirection

Redirection of client connections across AD sites is now possible.

 

Installation notes

Schema Updates

The service pack does do schema updates in order to support some of the new features.

Required Role Features

Also, on Client Access Servers (CAS), the IIS 6 WMI Compatibility feature is now required.

Installing from the command line

Upgrading is quite simple. Open a command prompt and navigate to the folder containing the extracted files and run:

Setup /m:upgrade /InstallWindowsComponents

The setup routine will automatically install the Web-WMI feature if needed, and upgrade the server.

Installation of SP2 via command prompt

Installation of SP2 via command prompt

Installation from the graphical user interface (GUI)

As mentioned above, the IIS 6 WMI Compatibility (Web-WMI) feature is required. If you plan to install the Service Pack using the GUI, you must manually add this feature. To install the feature, open PowerShell, and type:

Import-Module ServerManager Add-WindowsFeature web-wmi

As shown below.

Add-WindowsFeature Web-WMI
Add-WindowsFeature Web-WMI

If you don’t manually install this feature, the service pack installation will fail:

SP2 fails - IIS 6 WMI Compatibility required

SP2 fails - IIS 6 WMI Compatibility required

Once the SP2 file is downloaded, double click on it to extract the files to a folder.

Navigate to the folder containing the extracted files and double click on setup.exe and follow the prompts.

Installation on Database Availability Groups (DAG)

If you are going to apply SP2 to servers that are members of a DAG, run the StartDagServerMaintenance.ps1 script on the server first. This accomplishes several things, including suspending mailboxdatabase copying for each database; prevents the activation of databases on the server and moves active copies on the server to copies on other servers; prevents the server from taking on the PAM role. When finished, run the StopDagServerMaintenance.ps1 script, which allows databases to be activated, begins the copying, and allows the server to become the PAM. Mike Pfeiffer has a great blog post on this process.

Download

Download the Service Pack from the Microsoft Download Center here.

A stand-alone version of the SP2 Help file is available here.

Unified Messaging language packs for SP2 are available here.