Get Expiring Certifikate from Skype Environment

# ===================
# Global Variables
# ===================

# For Testing Purpose
# New-TimeSpan -Start (get-date) -End 21-06-2019

$Days = "75" # ATTENTION a variable does not funktion at AddDays with Powershell Version 3 in combination with Invoke. Version5 and up Works
$OutFile = "C:\Temp\Certlist.txt"

# ============================
# Create Array
# ============================
$Serverliste = @(
                    "Server01.domain.local"
                    "Server02.domain.local"
                    "Server03.domain.local"
                    "Server04.domain.local"
                  )

"Serverliste `r`n===========" | Out-File $OutFile
# ============================
# Reading out Certificates
# ============================
$Zertliste = (
Invoke-Command -ComputerName  $Serverliste -ScriptBlock  {Get-ChildItem Cert:\LocalMachine\My  | 
  Where {$_.NotAfter -lt  (Get-Date).AddDays(75)}} | Select-Object Issuer, NotAfter, SerialNumber, Subject, Use, DnsNameList | ForEach { #Added Days manuall because of Powershel Version 3

  [pscustomobject]@{
  ".===========" = "======================================="
  FQDN =  $_.Subject
  Issuer = $_.Issuer
  Use = $_.Use
  #Computername =  $_.PSComputername
  ExpiresOn =  $_.NotAfter
  DNSListNames = $_.DnsNameList 
  "============" = "======================================="
  }
  } |Sort-Object -Descending ) |Out-File $OutFile -Append


"Get-CScertificate`r`n=================" | Out-File $OutFile -Append

  Get-CsCertificate | Select-Object Issuer, NotAfter, SerialNumber, Subject, Use, AlternativeNames | 
  Where {$_.NotAfter -lt  (Get-Date).AddDays($Days)}| ForEach {

  [pscustomobject]@{
  ".===========" = "======================================="
  FQDN =  $_.Subject
  Issuer = $_.Issuer
  Use = $_.Use
  Computername =  $_.PSComputername
  ExpiresOn =  $_.NotAfter
  AlternativeNames = $_.AlternativeNames 
  "============" = "======================================="
  }
  } |Sort-Object -Descending | Out-File $OutFile -Append

  "List of Dns Names expiring in Lync Or Skype`r`n===================================================" | Out-File $OutFile -Append
  Get-CsCertificate | Where {$_.NotAfter -lt  (Get-Date).AddDays($Days)} | Select-Object -ExpandProperty AlternativeNames -Unique|
  Sort-Object -Descending | Out-File $OutFile -Append
  "===================================================" | Out-File $OutFile -Append


# ===================================================================
# Sending Mail if file is bigger then Empty kb.
# ===================================================================
$ToAddress = "admin@domain.local"
$FromAddres = "$env:computername.$env:userdnsdomain <$ToAddress>"
#$SmtpServer = "smtp.office365.com"
$SmtpServer = "mrelay.domain.local" # INFO: server that runs script must be allowed in FW, on mailrelay and in Virus Console outgoing connection to port 25 must be allowed!
#$SmtpPort = "587"
$Attachment = "$OutFile"
$Subject = "Certliste with Certificates that will Expire in $days from the Skype Environment"
#Body as HTML
$BodyHead = "This mail has been automaticly generated by the GetCSertificate script. <br> This Task runs following script C:\Temp\GetCSCertifikate.ps1. <br><br>See Attachment for expiring Certificates.<br>Please do not reply<br><br><br>"
$Body = "$BodyHead" + "$Zertliste"
#Body as Text
#$Body = "This is an automated mail used by a script. `r`n Please do not reply. `r`n `r See Attachment with log"

$mailparam = @{
    To = $ToAddress
    From = $FromAddres
    Subject = $Subject
    Body = $Body 
    Smtpserver = $SmtpServer
    #Port = $SmtpPort
    #Credential = $SmtpCred
    Attachment = $Attachment
    }

# =========================
# Check File for testing
# =========================
# Get-ChildItem -File $OutFile | select length

if( (get-item $OutFile).length -eq 434) {"File is blank"}
#If ((Get-Content $Attachment) -eq 1kb) {"File is blank"}
    Elseif  ((Get-Content $Attachment) -gt 434) {Send-MailMessage @mailparam -UseSsl -BodyAsHtml}