Get-LyncServer v1

#Powershell, #Lync

So I just love Exchange 2010 and PowerShell, and a cmdlet that rocks is Get-ExchangeServer which is a cmdlet that get the attributes of some or all the servers in the Exchange organization.

Lync however doesn’t have a similar cmdlet and that annoyed the hell out of me.  All I want is a list of Lync Servers and what they do .. simple you would think! Think again Disappointed smile

So I knocked this up.  It has some help text too that will help with the usage.  You can run get-help ./get-lyncserver –full

The basic output is a HTML summary matrix and a csv file that I use as input to other scripts.  Essentially I run this script once a day and then the other scripts when needed.

Oh another thing, the connectionuri needs credentials .. help explains how to set them or you get a prompt.

Let me know what you think!  this is v1 to enjoy

You can download the script from here or copy and paste from here:

<#
  .NOTES
  NAME: Get-LyncServer.ps1
  AUTHOR: Paul Flaherty
  Last Edit: "v1.0 [13 February 2012]
  .LINK
  blogs.flaphead.com
  .SYNOPSIS
  This Script gets connects to a Lync Server and enumerates a list
  of all OCS/Lync Servers and gathers additional information.
 .DESCRIPTION
  This script will connect to a Lync Server using the /OcsPowershell URI.
  Once connected it will use Get-CsComputer to get a list of OCS/Lync related servers.
  Then it uses Get-CsService to get a list of Lync Services and the Lync related
  windows services.

  The script then creates a matrix of the data an output a CSV file and HTML web Page
 .OUTPUTS
  Get-LyncServer.html
  Get-LyncServer.csv
 .EXAMPLE
  Get-LyncServer.ps1
  This will prompt you for Credentials and search the AD for a lync server to use
 .EXAMPLE
  Get-LyncServer.ps1 -LyncServer myLyncServer01
  This will prompt you for Credentials and use the specified lync server.
  it will also add the default ad domain to the server name is missing
 .EXAMPLE
  Get-LyncServer.ps1 -CredentialsFile .creds.txt
  This will search the AD for a lync server to use and use the current user account and
  credentials store in the CredentialsFile as the password
 .EXAMPLE
  Get-LyncServer.ps1 -CredentialsFile .creds.txt -LyncServer myLyncServer01
  This will use the specified lync server and use the current user account and
  credentials store in the CredentialsFile as the password
  .PARAMETER LyncServer
  Netbios or FQDN of a lync server to use.
  If the Netbios name is used, the AD Domain DNS name will be appended
  .PARAMETER CredentialsFile
  Credentials Password file for the current logged on user.
  To Create the credentials file run:
  $creds = Get-Credential
  $creds.Password  | ConvertFrom-SecureString | Set-Content creds.txt

#>

PARAM([String]$LyncServer="", [String]$CredentialsFile="")
##########################################################################################
$AppName = "Get-LyncServer.ps1"
$AppVer  = "v1.0 [13 February 2012]"
#v1.0 08 Feb 2012 : A script it born
#
# This script gets a list of Lync Servers
#
#Written By Paul Flaherty
#blogs.flaphead.com
##########################################################################################
$errorPref = $errorActionPreference
$errorActionPreference = "SilentlyContinue"

$ServerName             = hostname
$Today                  = Get-Date
$Global:CurrentUser     = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$Global:CurrentUserName	= $Global:CurrentUser.Name
$xUser                  = $Global:CurrentUserName
$currentdom             = [System.DirectoryServices.ActiveDirectory.Domain]::getcurrentdomain()
$Forest                 = $currentdom.Forest.ToString()
$ForestDomain           = $Forest

$xHTMLoutFolder         = $pwd

##########################################################################################
#Display script name and version
#########################################################################################
Write-host " " $AppName -NoNewLine -foregroundcolor Green
Write-Host ": " $AppVer -foregroundcolor Green
Write-host "`n Run on $ServerName at $Today by $xUser" -foregroundcolor Yellow
Write-Host "|-------------------------------------------------------------------|`n"
##########################################################################################
#Load the Lync bits & bobs
#########################################################################################
If($CredentialsFile -eq "") {
  $creds = Get-Credential -Credential $xUser
}ELSE{
  Write-Host "Using Credentials for " -NoNewLine
  Write-Host $xUser -NoNewLine -ForeGroundColor Yellow
  Write-Host " and password from " -NoNewLine
  Write-Host $CredentialsFile -ForeGroundColor Yellow

  $password = Get-Content $CredentialsFile | ConvertTo-SecureString
  $creds = New-Object System.Management.Automation.PsCredential $xUser,$password
} #$CredentialsFile

If($LyncServer -eq ""){
  Write-Host "Looking in RTCHSUniversalServices for a Lync Server"
  $Forest              = $Forest.Replace(".", ",DC=")
  $Forest              = "DC=" + $Forest
  $Dom                 = "LDAP://"
  $Dom                += $Forest
  $Root                = New-Object DirectoryServices.DirectoryEntry $Dom
  $Filter              = "(&(ObjectCategory=group)(name=*RTCHSUniversalServices*))"
  $selector            = New-Object DirectoryServices.DirectorySearcher $Filter
  $selector.PageSize   = 1000
  $selector.SearchRoot = $root
  $objs                = $selector.findall()
  $tmpGroup            = $objs | Select Path -First 1
  $Group               = [ADSI]$tmpGroup.Path
  $GrpMembers          = $Group.Member
  $grpCnt              = $GrpMembers.Count
  Write-Host "Found Group.  It has " -NoNewLine
  Write-Host  $GrpCnt -NoNewLine -ForeGroundColor Green
  Write-Host " Members"

  $LyncServerList = @()
  ForEach($tmpMember in $GrpMembers){
    $tmpADSI = "LDAP://" + $tmpMember
    $tmpA    = [ADSI]$tmpADSI
    If($tmpA.objectClass -contains "computer"){$LyncServerList += $tmpA.Name}
  }

  $LyncServerList = $LyncServerList | Sort
  $tmpNumber = Get-Random -Minimum 0 -Maximum $LyncServerList.Count
  $LyncServer = $LyncServerList[$tmpNumber]
  $LyncServer = $LyncServer + "." + $ForestDomain
  $LyncConnectionURI = "https://" + $LyncServer + "/OcsPowershell"



}ELSE{
  IF($LyncServer.Contains(".") -eq $False){$LyncServer = $LyncServer + "." + $ForestDomain}
  $LyncConnectionURI = "https://" + $LyncServer + "/OcsPowershell"
}

Write-Host "`nConnecting to:"$LyncConnectionURI
$s=New-PSSession -ConnectionURI $LyncConnectionURI -Credential $creds
If ($s -eq $Null){
  Write-Host "Error connecting to Lync" -Foregroundcolor Red
  Write-Host "Exiting" -Foregroundcolor Yellow
  Exit
}
Import-PSSession $s

$arrObjects = @()
$LyncRoleLabels = @()
$LyncRoleLabels += "Computer", "Pool", "Site", "UpToDate"

Write-Host "`nGetting Lync Servers`t" -NoNewLine
$LyncServers  = Get-CsComputer | Sort Identity
Write-host $LyncServers.Count"`n" -ForeGroundColor Green

Write-Host "Get-CsService`t" -NoNewLine
$LyncService  = Get-CsService
Write-Host $LyncService.count "Services found" -ForeGroundColor Green

Write-Host "`nGetting Windows Services"
$LyncWindowsServices = @()
ForEach($item in $LyncServers){
  $n = $item.Identity
  Write-Host $n"`t" -NoNewLine
  $tmpLWS = @()
  $tmpLWS = Get-Service RTC* -ComputerName $n #| select @{Expression={($_.DisplayName).Replace(" ","")};Label="DisplayName"}, Status
  Write-Host $tmpLWS.count -ForeGroundColor Green
  $LyncWindowsServices += $tmpLWS
}
Write-host "Total Services Found"$LyncWindowsServices.count

$LyncWindowsServicesByServer = $LyncWindowsServices | Group MachineName

#Get Available Roles
Write-host "`nGetting Lync Server Roles"

$LyncRoles = $LyncService | group Role | sort name
ForEach($tmpRole in $LyncRoles){$LyncRoleLabels += $tmpRole.Name}
$LyncServiceByPool = $LyncService | Group PoolFqdn | Sort Name

#Get Available Services
$grpLyncWindowsServices = $LyncWindowsServices | Group DisplayName
$grpLyncWindowsServices | ForEach{$n = $_.name; $LyncRoleLabels += $n}

Write-Host "`nMatrix Label Count" $LyncRoleLabels.count
Write-Debug $LyncRoleLabels

Write-Host "`nBuilding Matrix"
ForEach($tmpServer in $LyncServers){
  $tmpLyncServerId = $tmpServer.Identity
  Write-Host $tmpLyncServerId
  $objSite = Get-CsPool -Identity $tmpServer.pool | Select-Object Site
  $objReplication = $Null
  $objReplication = Get-CsManagementStoreReplicationStatus -ReplicaFqdn $tmpLyncServerId | Select-Object UpToDate
  $strReplication = $objSite.site -replace("Site:","")

  $tmpLyncServer = "" | Select $LyncRoleLabels
  $tmpLyncServer.Computer = $tmpLyncServerId
  $tmpLyncServer.Pool     = $tmpServer.Pool
  $tmpLyncServer.Site     = $strReplication
  $tmpLyncServer.UpToDate = $objReplication.UpToDate

  $tmpLSbP = $LyncServiceByPool | Where {$_.Name -eq $tmpLyncServerId}
  ForEach($item in $tmpLSbP){
    $tmpG = $item.Group
    ForEach($itemG in $tmpG){
      $xRole = $itemG.Role
      $tmpLyncServer.$xRole = $True
    }
  }

  $tmpWinSvc = $LyncWindowsServicesByServer | Where {$_.Name -eq $tmpLyncServerId}
  ForEach($item in $tmpWinSvc){
    $tmpG = $item.Group
    Write-Host $tmpG.count
    ForEach($itemG in $tmpG){
      $xSvc = $itemG.DisplayName
      Write-Host $xSvc
      $tmpLyncServer.$xSvc = $True
    }
  }

  $arrObjects += $tmpLyncServer
}


$HtmlHeader = "
<Style>
  TABLE{border-width: 1px;padding: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
  TD{border-width: 1px;padding: 1px;border-style: solid;border-color: black;}
  TH{font-family:'Arial';font-size:12px;border-width: 1px;padding: 1px;border-style: solid;border-color: black;background-color:peachpuff;layout-flow:vertical-ideographic;Text-align:left}
  TR{font-family:'Arial';font-size:10px}
  P{font-family:'Arial';}
</Style>
<TITLE>LYNC SERVERS</TITLE>"

$z="<B><FONT size='2' face='VERDANA'>Lync Servers</B></FONT><BR><FONT size='1' face='VERDANA'>Last updated: $today</FONT></font><HR size=6 color=Green>"
$xHTML = $arrObjects | ConvertTo-Html -head $HtmlHeader -Title "Lync Server Info" -body $z

$txtYELLOW = @()
$i=0; $xHTML | foreach{IF ($_ -like "*<td>True*"){ $txtYellow += $i}; $i++}
$txtYELLOW | ForEach{$xHTML[$_] = $xHTML[$_].Replace("<td>True","<td bgcolor=ORANGE>True")}


$xEnd = get-Date
$xHTML += "
<HR size=6 color=Green>
<FONT size='1' face='VERDANA'>Script Completed: $xEnd</FONT>
</FONT><BR></BODY>
</HTML>
"

$xHTML      | out-file $xHTMLoutFolderGet-LyncServer.html
$arrObjects | Export-csv Get-LyncServer.csv -NoTypeInformation

Get-PSSession | ForEach{Remove-PSSession -id $_.id}
$errorActionPreference = $errorPref
#End
##########################################################################################