title: How to set powershell to send email alerts when returned JSON api is empty
body: <p>i need to write a powershell script to connect to a REST API and to output into a text file stored locally. How do i write it such that if the REST API has issues and timeout without returning a response, the powershell script will trigger an email to send out when the saved text file is empty? 
  Also, how do i append the date to the saved text file for eg. c:\scripts\response_DDMMYYYY.txt</p>
  
  <p>Below are my current powershell script. Newbie at powershell scripting. Hope someone can enlighten me. 
  Thanks alot!</p>
  
  <pre><code>Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
  
  [Net.ServicePointManager]::SecurityProtocol=[Net.SecurityProtocolType]::Tls12
  
  add-type @"
      using System.Net;
      using System.Security.Cryptography.X509Certificates;
      public class TrustAllCertsPolicy : ICertificatePolicy {
          public bool CheckValidationResult(
              ServicePoint srvPoint, X509Certificate certificate,
              WebRequest request, int certificateProblem) {
              return true;
          }
      }
  "@
  [System.Net.ServicePointManager]::CertificatePolicy = New-Object 
  TrustAllCertsPolicy
  
  $url = "https://someapi.com/get_info"
  $token = '12345'
  
  $header = @{Authorization = "Bearer $token" }
  $body = @{code = "ABC"}
  
  $result = Invoke-RestMethod -Method POST -Uri $url -ContentType 
  'application/json' -Body (ConvertTo-Json $body) -Header $header 
  
  $result | ConvertTo-Json -depth 100 | Set-Content "c:\scripts\response.txt"
  </code></pre>
  
  <p>Edited based on kumar's advice:</p>
  
  <pre><code>Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
  
  [Net.ServicePointManager]::SecurityProtocol=[Net.SecurityProtocolType]::Tls12
  
  add-type @"
      using System.Net;
      using System.Security.Cryptography.X509Certificates;
      public class TrustAllCertsPolicy : ICertificatePolicy {
      public bool CheckValidationResult(
              ServicePoint srvPoint, X509Certificate certificate,
              WebRequest request, int certificateProblem) {
              return true;
           }
      }
   "@
   [System.Net.ServicePointManager]::CertificatePolicy = New-Object 
   TrustAllCertsPolicy
  
   $url = "https://someapi.com/get_info"
  
   $token = '12345'
  
   $header = @{Authorization = "Bearer $token" }
   $body = @{code = "ABC"}
  
   try {
      $result = Invoke-RestMethod -Method POST -Uri $url -ContentType 
      'application/json' -Body (ConvertTo-Json $body) -Header $header 
  
      $time = (Get-Date).ToString("DD-MM-YYYY")
      $result | ConvertTo-Json -depth 100 | Set-Content "c:\scripts\response- 
      "+$time+".txt"
  
   } catch {
       Write-Host "StatusCode: " $_.Exception.Response.StatusCode.value__
       Write-Host "StatusDescription:" 
   $_.Exception.Response.Status.StatusDescription
  
       Send-MailMessage -To "ABC&lt;abc@gmail.com&gt;" -From "server &lt;admin@abc.com&gt;" 
   -Subject "Error with sync to server"
  
  }
  </code></pre>