Start or display stopped Lync services in Lync topology

Introduction

This script can help system administrators to start all the Lync Server services for each computer in current topology automatically.

Scenarios

System administrators may encounter that Lync servers are not working properly due to required services startup failure. We can create PowerShell script to help them start these services automatically.

Script

This script contains the following functions:

You can use this script in following ways:

Method 1:

  1. Download the script and copy it to your computer.
  2. Open the script file with Notepad or any other script editors.
  3. Scroll down to the end of the script file, and then add function name and parameter to call the function.
  4. Save the file and then run the script on the computer.

Method 2:

  1. Rename scriptname.ps1 to scriptname.psm1 (PowerShell Module file)
  2. Run Import-Module cmdlet to import this module file in current session.
    Import-Module filepath\scriptname.psm1

Here are some code snippets for your references. To get the complete script sample, please click the download button at the beginning of this page.

PowerShell
Edit|Remove
Function Start-OSCLyncServicesOnEachServer 
{ 
    <# 
        .SYNOPSIS 
            Start-OSCLyncServicesOnEachServer can be used to start all the Lync Server services in topology except the Edge server. 
        .DESCRIPTION 
            Start-OSCLyncServicesOnEachServer is a function which can be used to start all the Lync Server services in topology except the Edge server. 
        .EXAMPLE 
            PS: Start-OSCLyncServicesOnEachServer 
        .LINK 
            Windows PowerShell Advanced Function 
            http://technet.microsoft.com/en-us/library/dd315326.aspx 
    #>if((Test-OSCCommonLyncInstalled)) 
    { 
        if((Test-OSCCommonIsEdgeServer)) 
        { 
            Write-Host $Messages.NoEdgeRun -ForegroundColor Yellow 
            return 
        } 
     
        $me = whoami 
        $currentUser = Get-CsAdUser -Identity $me$userName = $currentUser.SamAccountName 
        $roles = Get-CsAdminRoleAssignment -Identity $userName$adminRole = $roles| Where-Object {$_-eq "CsAdministrator"} 
        if($adminRole-ne $null) 
        { 
            $servers = @() 
            #Returns information about the Edge Servers used in your organization.$edges = @(Get-CsService -EdgeServer) 
            Get-CsManagementStoreReplicationStatus |ForEach-Object { 
                $lyncServerName = $_.ReplicaFqdn 
                foreach($edgein$edges) 
                { 
                    if(-not($edge.Identity -like "*$lyncServerName")) 
                    { 
                        $servers+$lyncServerName 
                    } 
                } 
            } 
            foreach($serverin$servers) 
            { 
                Write-Host (-join $Messages.StartingServices,$server,$Messages.StartingServices2) -ForegroundColor Yellow 
                $conn = Test-Connection -ComputerName $server-Count 1 -ErrorAction:SilentlyContinue 
                if($conn-eq $null) 
                { 
                    Write-Host (-join ($Messages.CannotConnect," ",$server)) -ForegroundColor Red 
                } 
                else 
                { 
                    try 
                    { 
                        $services = Get-Service-ComputerName $server-DisplayName "*lync*"-ErrorAction SilentlyContinue 
                        $services|ForEach-Object { 
                            if($_.Status -eq "Stopped") 
                            { 
                                $serviceName = $_.DisplayName 
                                try 
                                { 
                                    $_.Start() 
                                    $_.WaitForStatus([System.ServiceProcess.ServiceControllerStatus]::Running,[TimeSpan]::FromSeconds(15)) 
                                    Write-Host (-join ($serviceName," ",$Messages.IsStarted)) 
                                } 
                                catch [InvalidOperationException] 
                                { 
                                    Write-Host (-join ($Messages.CannotStartService," ",$serviceName)) -ForegroundColor Red 
                                } 
                                Catch [System.ServiceProcess.TimeoutException] 
                                { 
                                    Write-Host (-join ($Messages.CannotStartService," ",$serviceName)) -ForegroundColor Red 
                                } 
                            } 
                        } 
                    } 
                    catch [Exception] 
                    { 
                        Write-Host $_.Exception.Message -ForegroundColor Red 
                    } 
                } 
            } 
            Write-Host $Messages.Done -ForegroundColor Yellow 
        } 
        else 
        { 
            Write-Host $Messages.UserNotInAdminRole -ForegroundColor Yellow 
        } 
    } 
    else 
    { 
        Write-Host $Messages.RequireLyncInstalled -ForegroundColor Yellow 
    } 
}

Examples

To obtain the detailed information about how to use the functions, run the following command to retrieve the help information:
Get-Help functionName –detailed
For example: Get-Help Show-OSCStoppedLyncServices -detailed

Example 1: Start all the Lync Server services for all computers in current topology.
Command: Start-OSCLyncServicesOnEachServer
Screenshot:

Example 2: Start all the Lync Server services for specific computer in current topology.
Command: Start-OSCLyncServicesByServer –ServerFQDN “ls-fe2.contoso.com”
Screenshot:

Example 3: Start all the Lync services for Edge server in current topology.
Command: Start-OSCLyncServicesOnEdge
Screenshot:

Example 4: Get all the stopped Lync Server services for all computers in current topology.
Command: Show-OSCStoppedLyncServices
Screenshot:

Prerequisites

Windows PowerShell 2.0

Microsoft Lync Server 2010

Additional Resources

Technical Resource:
Windows PowerShell Advanced Function 
Administer Lync Server 2010 services from a remote Windows computer