PowerShell: Check Lync Services and start if not running

Hi Readers,

Just sharing another script that We have recently written for our Lync enviornment and added to startup of Lync edge servers.

but this can be used with any Lync server.

We have used it on our edge servers as we have seen that after restart some times serivces were not coming up automatically.

As a wrokaround we have used this solution, this can also be used to monitor the Lync services & start it , if found not running.

below is the output that it can send you in email..

Just change the below variables:

$smtphost = “smtp server”

$from = “Lyncserver1Services@lab.com”

$to = “Vikas.sukhija@lab.com”

and run the batch file..(download the zip file from below link & extract)

https://github.com/VikasSukhija/Downloads/blob/master/CHECkLync%20services.zip

########################################################################################## 
#             Author: Diwakar Sharma 
#             Reviewer: Vikas SUkhija 
#             Date: 03/11/2014 
#             Description: Script to Check & Start Lync services after maintenance 
#                           
########################################################################################## 
###########################Define Variables############################################## 
 
$reportpath = ".\LyncReport.htm"  
 
if((test-path $reportpath-like $false) 
{ 
new-item $reportpath -type file 
} 
 
$smtphost = "smtp server"  
$from = "Lyncserver1Services@lab.com"  
$to = "Vikas.sukhija@lab.com" 
 
 
 
If (( Get-Module | where {$_.Name -match "Lync"}) -eq $null) 
{ 
    Import-Module Lync 
} 
 
########################HTML Table######################################################### 
$report = $reportpath 
 
Clear-Content $report  
Add-Content $report "<html>"  
Add-Content $report "<head>"  
Add-Content $report "<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>"  
Add-Content $report '<title>Exchange Status Report</title>'  
add-content $report '<STYLE TYPE="text/css">'  
add-content $report  "<!--"  
add-content $report  "td {"  
add-content $report  "font-family: Tahoma;"  
add-content $report  "font-size: 11px;"  
add-content $report  "border-top: 1px solid #999999;"  
add-content $report  "border-right: 1px solid #999999;"  
add-content $report  "border-bottom: 1px solid #999999;"  
add-content $report  "border-left: 1px solid #999999;"  
add-content $report  "padding-top: 0px;"  
add-content $report  "padding-right: 0px;"  
add-content $report  "padding-bottom: 0px;"  
add-content $report  "padding-left: 0px;"  
add-content $report  "}"  
add-content $report  "body {"  
add-content $report  "margin-left: 5px;"  
add-content $report  "margin-top: 5px;"  
add-content $report  "margin-right: 0px;"  
add-content $report  "margin-bottom: 10px;"  
add-content $report  ""  
add-content $report  "table {"  
add-content $report  "border: thin solid #000000;"  
add-content $report  "}"  
add-content $report  "-->"  
add-content $report  "</style>"  
Add-Content $report "</head>"  
Add-Content $report "<body>"  
add-content $report  "<table width='100%'>"  
add-content $report  "<tr bgcolor='Lavender'>"  
add-content $report  "<td colspan='7' height='25' align='center'>"  
add-content $report  "<font face='tahoma' color='#003399' size='4'><strong>Lync Edge Services Status</strong></font>"  
add-content $report  "</td>"  
add-content $report  "</tr>"  
add-content $report  "</table>"  
  
add-content $report  "<table width='100%'>"  
Add-Content $report  "<tr bgcolor='IndianRed'>"  
Add-Content $report  "<td width='10%' align='center'><B>Services</B></td>"  
Add-Content $report  "<td width='5%' align='center'><B>ActivityLevel</B></td>"  
Add-Content $report  "<td width='20%' align='center'><B>PreviousStatus</B></td>"  
Add-Content $report  "<td width='20%' align='center'><B>CurrentStatus</B></td>" 
  
 
Add-Content $report "</tr>"  
 
 
 
 
#####################Start Lync service if its not running############################## 
 
$services = Get-CsWindowsService 
 
foreach($service in $services) 
 
{ 
 
$Name = $service.Name 
$Status$service.status 
$ActivityLevel =  $service.ActivityLevel 
 
        Add-Content $report "<tr>"  
        Add-Content $report "<td bgcolor= 'GainsBoro' align=center>  <B> $Name</B></td>"  
             Add-Content $report "<td bgcolor= 'GainsBoro' align=center>  <B>$ActivityLevel</B></td>"  
                if($status -eq "Running") 
                 { 
        Add-Content $report "<td bgcolor= 'Aquamarine' align=center>  <B>$Status</B></td>" 
                Add-Content $report "<td bgcolor= 'Aquamarine' align=center>  <B>$Status</B></td>" 
                 } 
                else 
                 { 
                Add-Content $report "<td bgcolor= 'Red' align=center>  <B>$status</B></td>"  
                Start-CsWindowsService -Name $Name 
                $getstatus = get-CsWindowsService -Name $Name  
                $statusc = $getstatus.status 
                Add-Content $report "<td bgcolor= 'Aquamarine' align=center><B>$statusc<B></B></td>" 
                  } 
                    
         
                Add-Content $report "</tr>"       
 
} 
 
 
Write-Host "Sending Email..........." -foregroundcolor Green 
     
$subject = "Lync2010ServiceStatus"  
$body = Get-Content $report 
$smtpNew-Object System.Net.Mail.SmtpClient $smtphost  
$msg = New-Object System.Net.Mail.MailMessage $from$to$subject$body  
$msg.isBodyhtml = $true  
$smtp.send($msg) 
 
##########################Task Completed#####################################

Tech Wizard

https://techwizard.cloud

https://syscloudpro.com/

Leave a comment