Get-LyncServer v1.2

#Powershell, #Lync

Okay so made some updates to include Lync/OCS version, Lync Type, OS and Memory.  Also change the services from RTC* to *RTC*

Downloadable from here or cut and paste below .. enjoy

<#
  .NOTES
  NAME: Get-LyncServer.ps1
  AUTHOR: Paul Flaherty
  Last Edit: "v1.2 [21 February 2012]
  .LINK
  blogs.flaphead.com
  .SYNOPSIS
  This Script gets connectes 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
#v1.1 20 Feb 2012 : Change Services to *RTC* instead of RTC*
#v1.2 21 Feb 2012 : Added Lync Server version
#
# 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         = "E:PsMon.LyncHTML"

##########################################################################################
#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", "ProductVersion", "DeploymentType", "OS", "RAM","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:","")
  $tmpOS          = Get-WmiObject win32_operatingsystem -ComputerName $tmpLyncServerId

  $key="SOFTWAREMicrosoftReal-Time Communications"
  $type = [Microsoft.Win32.RegistryHive]::LocalMachine
  $regKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($type, $tmpLyncServerId)
  $regKey = $regKey.OpenSubKey($key, $True)
  $tmpLabel = $regKey.GetValueNames()
  $tmpVC = "" | Select $tmpLabel
  ForEach($vc in $regKey.GetValueNames()){$tmpVC.$vc = $regkey.GetValue($vc)}


  $tmpLyncServer = "" | Select $LyncRoleLabels
  $tmpLyncServer.Computer = $tmpLyncServerId
  $tmpLyncServer.Pool     = $tmpServer.Pool
  $tmpLyncServer.Site     = $strReplication
  $tmpLyncServer.UpToDate = $objReplication.UpToDate
  $tmpLyncServer.OS       = $tmpOS.Caption + " " + $tmpOS.CSDVersion
  $tmpLyncServer.RAM      = "!AR!" + $tmpOS.FreePhysicalMemory
  $tmpLyncServer.ProductVersion = $tmpVC.ProductVersion
  $tmpLyncServer.DeploymentType = $tmpVC.DeploymentType

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

$i=0; $xHTML | foreach{IF ($_ -like "*>!AR!*"){ $txtAR += $i;}; $i++}
$txtAR | ForEach{$xHTML[$_] = $xHTML[$_].Replace(">!AR!"," align=right>")}


$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
##########################################################################################

Leave a Reply

%d bloggers like this: