PSDxExpeditions.psm1

<#
 
 
New-ModuleManifest `
-Path "$env:userprofile\OneDrive\WindowsPowerShell\Modules\PSDxExpeditions\PSDxExpeditions.psd1" `
-RootModule "$env:userprofile\OneDrive\WindowsPowerShell\Modules\PSDxExpeditions\PSDxExpeditions.psm1" `
-Author 'Reginald Baalbergen, The PA1REG' `
-CompanyName 'Radio Amateur' `
-Copyright '(c)2017 Reginald Baalbergen (PA1REG)' `
-Description 'PSDxExpeditions' `
-ModuleVersion 1.1.3 `
-PowerShellVersion 5.0 `
-FunctionsToExport 'Update-DxHrdAlarmFile', 'Update-DxVe7ccAlarmFile' `
-ProjectUri 'https://github.com/PA1REG/PSDxExpeditions' `
-HelpInfoUri 'https://github.com/PA1REG/PSDxExpeditions/blob/master/readme.md' `
-ReleaseNotes 'PSDxExpeditions is a PowerShell module that creates Dx Expedition alarm files for VE7CC cluster or Ham Radio DeLuxe.'
 
#-Tag 'Ham_Radio_DeLuxe","VE7CC","DX_Expeditions' `
#-AliasesToExport 'UdxH', 'UdxV' `
   
https://dfinke.github.io/2016/Quickly-Install-PowerShell-Modules-from-GitHub/
get-command -Module InstallModuleFromGitHub
 
Install-Module -Name InstallModuleFromGitHub -RequiredVersion 0.3
Install-ModuleFromGitHub -GitHubRepo /PA1REG/PoShHrdUtils
 
 
#>


#$DisplayColor = 'Green'
$AllDxCalls = @()


function RemoveLineCarriage($lineIn)
# Remove all CR or LF, etc form string
{
  $ReturnValue = [System.String] $lineIn;
  $ReturnValue = $ReturnValue -replace "`t","";
  $ReturnValue = $ReturnValue -replace "`n","";
  $ReturnValue = $ReturnValue -replace "`r","";
  $ReturnValue = $ReturnValue -replace " ;",";";
  $ReturnValue = $ReturnValue -replace "; ",";";
  $ReturnValue = $ReturnValue -replace [Environment]::NewLine, "";
  return $ReturnValue;
}


function Get-CheckAllowedCharsInLine ($lineIn) 
# An radio amateur callsign must contain only alfabet and a number with optional an /, this is checked
#
{
  $ResultChars = $false
  $ResultNumbers = $false
  $ResultPattern = $true
  $ResultLength = $false
  $checkPattern = "^[/a-zA-Z0-9\s]+$" #'^[a-z0-9]+$')
  $checkPatternChars = "[a-zA-Z]"
  $checkPatternNumbers = "[0-9]"
  
  # Check if the string contains the given pattern
  if ($lineIn -notmatch $checkPattern) { $ResultPattern = $false }
  # there are no callsigns less than 3 positions
  if ($lineIn.Length -ge 3) { $ResultLength = $true }
  # Check if the callsign contains a letter and a number
  foreach ($Char In $lineIn)
  {
     if ($Char -match $checkPatternChars ) { $ResultChars = $true }
     if ($Char -match $checkPatternNumbers ) { $ResultNumbers = $true }
  }  
  # check the results and decide
  if($ResultChars -and $ResultNumbers -and $ResultPattern -and $ResultLength)
  {
    $ReturnValue = $true
  } else
  {
    $ReturnValue = $false
  }
  return $ReturnValue 
}


function Get-ValidCall ($lineIn) 
# Check if it is really a callsign
{
  Try 
  {
    $ReturnValue = $true
    switch -wildcard ($lineIn) 
    { 
        "AF-*"  {$ReturnValue = $false} 
        "AS-*"  {$ReturnValue = $false} 
        "OC-*"  {$ReturnValue = $false} 
        "SA-*"  {$ReturnValue = $false} 
        "NA-*"  {$ReturnValue = $false} 
        "EU-*"  {$ReturnValue = $false} 
        "HF+*"  {$ReturnValue = $false} 
        "EU-*"  {$ReturnValue = $false} 
        "ETC.*" {$ReturnValue = $false} 
        ""      {$ReturnValue = $false} 
    }
    
    switch ($lineIn) 
    { 
        "0"         {$ReturnValue = $false} 
        "TEST"      {$ReturnValue = $false} 
        "ALL"       {$ReturnValue = $false} 
        "ASIAN"     {$ReturnValue = $false} 
        "CW"        {$ReturnValue = $false} 
        "ETC."      {$ReturnValue = $false} 
        "5W"        {$ReturnValue = $false} 
        "10W"       {$ReturnValue = $false} 
        "100W"      {$ReturnValue = $false} 
        "200W"      {$ReturnValue = $false} 
        "500W"      {$ReturnValue = $false} 
        "1000W"     {$ReturnValue = $false} 
        "1KW"       {$ReturnValue = $false} 
        "PSK31"     {$ReturnValue = $false} 
        "PSK63"     {$ReturnValue = $false} 
        "JT65"      {$ReturnValue = $false} 
        "JT9"      {$ReturnValue = $false} 
        "FT8"       {$ReturnValue = $false} 
        "160M"      {$ReturnValue = $false} 
        "80M"       {$ReturnValue = $false} 
        "60M"       {$ReturnValue = $false} 
        "40M"       {$ReturnValue = $false} 
        "30M"       {$ReturnValue = $false} 
        "24M"       {$ReturnValue = $false} 
        "20M"       {$ReturnValue = $false} 
        "17M"       {$ReturnValue = $false} 
        "15M"       {$ReturnValue = $false} 
        "12M"       {$ReturnValue = $false} 
        "10M"       {$ReturnValue = $false} 
        "6M"        {$ReturnValue = $false} 
        "0600Z"     {$ReturnValue = $false} 
        "24X7"      {$ReturnValue = $false} 
        "24HRS/DAY" {$ReturnValue = $false} 
    }
  
    $validCall = Get-CheckAllowedCharsInLine($lineIn)
    if($validCall -and $ReturnValue)
    {
       $ReturnValue = $true
    } else
    {
       $ReturnValue = $false
    }

   } Catch 
  {
    $ReturnValue = $false
  }
  
  return $ReturnValue 
}
 

function Get-MonthNumber ($lineIn) 
# Return the number of a given month
{
   switch -wildcard ($lineIn) 
    { 
        "JAN" {$ReturnValue = "01"} 
        "FEB" {$ReturnValue = "02"} 
        "MAR" {$ReturnValue = "03"} 
        "APR" {$ReturnValue = "04"} 
        "MAY" {$ReturnValue = "05"} 
        "JUN" {$ReturnValue = "06"} 
        "JUL" {$ReturnValue = "07"} 
        "AUG" {$ReturnValue = "08"} 
        "SEP" {$ReturnValue = "09"} 
        "OCT" {$ReturnValue = "10"} 
        "NOV" {$ReturnValue = "11"} 
        "DEC" {$ReturnValue = "12"} 
        "MAA" {$ReturnValue = "03"} 
        "MEI" {$ReturnValue = "05"} 
        "OKT" {$ReturnValue = "10"} 
    }
 
  return $ReturnValue 
#Get-Culture
}


function Get-DateFromString ($lineIn) 
# Return the date from a string
{
# Get-Culture
  Try 
  {
    $Year  = $lineIn.substring(0,4)
    $Month = $lineIn.substring(5,3)
    $Day   = $lineIn.substring(8,2)
          
    $Month = Get-MonthNumber ($Month)
    $date = "$day/$month/$year"
    $EDate = [datetime]::parseexact($date, 'dd/MM/yyyy', $null)
    return $EDate
  } Catch 
  {
    return $null
  }
}


function Get-CallsCleanedUp
# Remove double entries and sort the output.
{
    [CmdletBinding()]
    param
    (
        [Parameter(Position=0, Mandatory=$true)]
        [Object] $DxCallsArray
    )
    begin
    {
      Write-Verbose "Start Module : [$($MyInvocation.MyCommand)] *************************************"
      Write-Verbose "Sorting and remove duplicates"
      $DxCallsArray = $DxCallsArray | Sort-Object -Unique
      Return $DxCallsArray
      Write-Verbose "Eind Function : [$($MyInvocation.MyCommand)] *************************************"
   }
}


function Add-ConcatDxCall
# Add 2 array with calls together
{
    [CmdletBinding()]
    param
    (
        [Parameter(Position=0, Mandatory=$true)]
        [Object] $DxCallsArray1,
        [Parameter(Position=1, Mandatory=$true)]
        [Object] $DxCallsArray2
    )
    begin
    {
      Write-Verbose "Start Module : [$($MyInvocation.MyCommand)] *************************************"
      Write-Verbose "Concatenate 2 array objects"
      $ConcatDxCallsArray = $DxCallsArray1 + $DxCallsArray2
      Return $ConcatDxCallsArray
      Write-Verbose "Eind Function : [$($MyInvocation.MyCommand)] *************************************"
   }
}
 

function Update-WriteVE7CCAlarmFile
{
    [CmdletBinding()]
    param
    (
        [Parameter(Position=0, Mandatory=$true)]
        [String] $AlarmFile,
        [Parameter(Position=1, Mandatory=$true)]
        [Object] $DxCallsArray
    )
    begin
    {
      Write-Verbose "Start Module : [$($MyInvocation.MyCommand)] *************************************"
      $arrCalls =@()

      if (! (Test-Path $AlarmFile)) 
      {
        $ErrorMessage = $_.Exception.Message
        $FailedItem = $_.Exception.ItemName
        Throw "Unable to locate file $AlarmFile from $URL ($ErrorMessage, $FailedItem)" 
      } else
      {
        Write-Verbose "Empty file $AlarmFile)"
        Clear-Content $AlarmFile 
      }

      Try 
      {
        foreach ($Call in $DxCallsArray) 
        {
          $objResults = New-Object PSObject
          $objResults | Add-Member -membertype NoteProperty -name "Call" -Value $Call 
          $objResults | Add-Member -membertype NoteProperty -name "Settings" -Value "Y1101111111000001101111111000001101011111000001"
          $arrCalls += $objResults 
        }
  
        $CsvContent = $arrCalls | ConvertTo-Csv -NoTypeInformation
        Write-Verbose "Write all calls to $AlarmFile)"
        $CsvContent[1..$CsvContent.Length] | Add-Content $AlarmFile
      } Catch 
      {
        Write-Verbose "Unable to write all calls to $AlarmFile)"
      }

      Write-Verbose "End Module : [$($MyInvocation.MyCommand)] *************************************"
    }
}
 

function Update-WriteHRDAlarmFile
{
    [CmdletBinding()]
    param
    (
        [Parameter(Position=0, Mandatory=$false)]
        [String] $DXClusterAlarmsFile,
        [Parameter(Position=1, Mandatory=$false)]
        [Object] $DxCallsArray
    )
    begin
    {
      Write-Verbose "Start Module : [$($MyInvocation.MyCommand)] *************************************"

      if (! (Test-Path $DXClusterAlarmsFile)) 
      {
        $ErrorMessage = $_.Exception.Message
        $FailedItem = $_.Exception.ItemName
        Throw "Unable to locate file $DXClusterAlarmsFile from $URL ($ErrorMessage, $FailedItem)" 
      }

      Try 
      {
        # Building the xml string for calls
        foreach ($Call in $DxCallsArray) 
        {
          $DXClusterCalls += "$($Call)|"
        }
        # Delete the last character which contains a |-sign
        $DXClusterCalls = $DXClusterCalls.Substring(0,$DXClusterCalls.Length-1)
        Write-Verbose "Builded line is now : $$DXClusterCalls"
        # Naming elements
        $Time = (Get-Date -f g)
        $DXTitle = 'Special Event Callsigns'
        $DXComment = "Created by PS_DxExpeditions.ps1, PA1REG, last update : $Time"

        Write-Verbose "Reading file $DXClusterAlarmsFile"
        $XMLContent = [xml](Get-Content $DXClusterAlarmsFile)
        $Alarms = $XMLContent.SelectNodes("/HRD/Alarm")
        $SpecialAlarm = $Alarms | Where-Object {$_.Title -eq $DXTitle}
        if ($SpecialAlarm.Count -ne 0)
        {
          Write-Verbose "Updating XML file"
          $SpecialAlarm.Callsign = $DXClusterCalls
          $SpecialAlarm.SetAttribute('Comment',$DXComment)
        } else
        {
          Write-Verbose "Adding XML file"
          $NewAlarm = $XMLContent.CreateElement("Alarm")
          $NewAlarm.InnerText = ''
          $NewAlarm.SetAttribute('Callsign',$DXClusterCalls)
          $NewAlarm.SetAttribute('Comment',$DXComment)
          $NewAlarm.SetAttribute('Filter','ALL')
          $NewAlarm.SetAttribute('Title',$DXTitle)
          $NewAlarm.SetAttribute('Enable','1')
          $NewAlarm.SetAttribute('Interval','5')
          $NewAlarm.SetAttribute('Options','16')
          $Response = $XMLContent.DocumentElement.AppendChild($NewAlarm)
          Write-Verbose "Adding XML response $Response"
        }
      
        Write-Verbose "Write all calls to $DXClusterAlarmsFile"
        $XMLContent.Save($DXClusterAlarmsFile)
      } Catch 
      {
        Write-Verbose "Unable to write all calls to $DXClusterAlarmsFile)"
      }

      Write-Verbose "End Module : [$($MyInvocation.MyCommand)] *************************************"
    }
}


function Stop-Application
{
    [CmdletBinding()]
    param
    (
        [Parameter(Position=0, Mandatory=$true)]
        [String] $ProcessName,
        [Parameter(Position=1, Mandatory=$true)]
        [String] $ProcessActivity
    )
    begin
    {
      Write-Verbose "Start Module : [$($MyInvocation.MyCommand)] *************************************"

       $Delaytime   = 1
       $TimeElaped  = 0
       $TimeOut     = 120
       $Id          = 1


          Write-Progress -Id $Id -Activity $ProcessActivity  -PercentComplete ($TimeElaped / $TimeOut * 100)
       While ( (Get-Process -Name $($ProcessName) -ErrorAction SilentlyContinue))
       {
          Write-Progress -Id $Id -Activity $ProcessActivity  -PercentComplete ($TimeElaped / $TimeOut * 100)
         Stop-Process -Name $ProcessName
         Start-Sleep -Seconds $Delaytime
         $TimeElaped =+ $TimeElaped + $Delaytime
         if ($TimeElaped -ge $TimeOut)
         {
             Write-Verbose "Timeout : Stopping process takes too long ( > $TimeOut sec.)" -ForegroundColor Red
            Throw "Timeout : Starting process takes too long ( > $TimeOut sec.)" 
         }
       }

       if ($TimeElaped -lt $TimeOut)
       {
          #Write-Output "---------" -ForegroundColor Red
       } else
       {
            Write-Output "Stopping process due timeout" 
       }
  
        Write-Verbose "Eind Module : [$($MyInvocation.MyCommand)] *************************************"
    }
}
  

function Start-Application
{
    [CmdletBinding()]
    param
    (
        [Parameter(Position=0, Mandatory=$true)]
        [String] $ProcessName,
        [Parameter(Position=1, Mandatory=$true)]
        [String] $ProcessToStart,
        [Parameter(Position=2, Mandatory=$true)]
        [String] $ProcessActivity
    )
    begin
    {
      Write-Verbose "Start Module : [$($MyInvocation.MyCommand)] *************************************"

       $Delaytime   = 1
       $TimeElaped  = 0
       $TimeOut     = 120
       $Id          = 1


          Write-Progress -Id $Id -Activity $ProcessActivity  -PercentComplete ($TimeElaped / $TimeOut * 100)
       While ( -not (Get-Process -Name $($ProcessName) -ErrorAction SilentlyContinue))
       {
          Write-Progress -Id $Id -Activity $ProcessActivity  -PercentComplete ($TimeElaped / $TimeOut * 100)
         Start-Process $ProcessToStart 
         Start-Sleep -Seconds $Delaytime
         $TimeElaped =+ $TimeElaped + $Delaytime
         if ($TimeElaped -ge $TimeOut)
         {
             Write-Verbose "Timeout : Starting process takes too long ( > $TimeOut sec.)"
            Throw "Timeout : Starting process takes too long ( > $TimeOut sec.)" 
         }
       }

       if ($TimeElaped -lt $TimeOut)
       {
          #Write-Output "---------" -ForegroundColor Red
       } else
       {
            Write-Output "Start process due timeout"
       }
  
        Write-Verbose "Eind Module : [$($MyInvocation.MyCommand)] *************************************"
    }
}


function Write-DxCallsToCSV
{
    [CmdletBinding()]
    param
    (
        [Parameter(Position=0, Mandatory=$true)]
        [Object] $objDxCalls,
        [Parameter(Position=1, Mandatory=$true)]
        [String] $CSVFile
    )
    begin
    {
      Write-Verbose "Start Function : [$($MyInvocation.MyCommand)] *************************************"

      Try 
      {
        Write-Verbose "Start Writing CSV file $CSVFile" 
        $stuff = @()
        foreach($row in $objDxCalls) 
        {
         $obj = new-object PSObject
         $obj | add-member -membertype NoteProperty -name "DxCall" -value $row
         $stuff += $obj
        }
        $stuff | Export-Csv -Path $CSVFile -notypeinformation

      } Catch 
      {
        $ErrorMessage = $_.Exception.Message
        $FailedItem = $_.Exception.ItemName
        Throw "Unable to write file $CSVFile ($ErrorMessage, $FailedItem)" 
      }

      Write-Verbose "Eind Function : [$($MyInvocation.MyCommand)] *************************************"
    }
}


function Get-ContentNg3K
{
    [CmdletBinding()]
    param
    (
        [Parameter(Position=0, Mandatory=$true)]
        [Uri] $URL
    )
    begin
    {
      Write-Verbose "Start Module : [$($MyInvocation.MyCommand)] *************************************"
      $arrDxCalls =@()

  
      Try 
      {
        Write-Verbose "Start Reading from $URL" 
        $result = Invoke-WebRequest $URL
      } Catch 
      {
        $ErrorMessage = $_.Exception.Message
        $FailedItem = $_.Exception.ItemName
        Write-Verbose "Unable to open website $URL ($ErrorMessage, $FailedItem)"
        Throw "Unable to open website $URL ($ErrorMessage, $FailedItem)" 
      }

      Write-Verbose "Reading content from site $URL)"

      Try 
      {
        $elementStartdate = 0
        $elementEnddate = 1
        #$elementDXCCentity = 2
        $elementCall = 3
        #$elementQSLvia = 4
        #$elementReportedBy= 5
        $elementInfo= 6 

        $StartDate = Get-DateFromString($EventDate)
        $CurrentDate = Get-Date 

        $result.ParsedHtml.getElementsByTagName('tr') | ForEach-Object {
          $tr =  $_;
          $startdate = Get-DateFromString ($tr.childNodes.item($elementStartdate).outerText)
          if ($null -ne $startdate)
          {
            $EndDate = Get-DateFromString ($tr.childNodes.item($elementEnddate).outerText)
            #$DXCCentity = $tr.childNodes.item($elementDXCCentity).outerText
            $Call = $tr.childNodes.item($elementCall).outerText
            #$QSLvia = $tr.childNodes.item($elementQSLvia).outerText
            #$ReportedBy = $tr.childNodes.item($elementReportedBy).outerText
            $Info = $tr.childNodes.item($elementInfo).outerText

            if ( ($EndDate.Month -eq $CurrentDate.Month -or $StartDate.Month -le $CurrentDate.Month) -and ($EndDate.Year -eq $CurrentDate.Year) )
            {
              $call = RemoveLineCarriage($call)
              $startBracket = $Call.IndexOf("[")
              if ($startBracket -gt 0)
              {
                $call = $Call.substring(0,$startBracket).ToUpper()
                If (Get-ValidCall ($call)) 
                {
                  $arrDxCalls += $call
                 #Write-Output "YES $Remark " -ForegroundColor Green
                } 
              }
              $Remarks = RemoveLineCarriage($Remarks)
              $Remarks = $Info.Split(' ')
              foreach($Remark In $Remarks)
              { 
                $Remark = $Remark.Replace(")","")
                $Remark = $Remark.Replace("(","")
                $Remark = $Remark.Replace(",","")
                $Remark = $Remark.Replace(";","").Trim().ToUpper()
                If (Get-ValidCall ($Remark)) 
                {
                  $arrDxCalls += $Remark
                  #Write-Output "YES $Remark " -ForegroundColor Green
                } 
              }

            #Write-Output "YES : 0=$startdate 1=$EndDate 3=$Call "
            # Write-Output "YES : 0=$startdate 1=$EndDate 2=$time2 3=$time3 4=$time4 5=$time5 6=$time6 7=$time7 8=$time8"
          } else 
          {
            #Write-Output "NO : 0=$startdate 1=$EndDate 2=$time2 3=$time3 4=$time4 5=$time5 6=$time6 7=$time7 8=$time8" -ForegroundColor Gray
          }
        }
      }
      Return $arrDxCalls | Sort-Object -Unique
      } Catch 
      {
        $ErrorMessage = $_.Exception.Message
        $FailedItem = $_.Exception.ItemName
        Throw "Error reading website $URL ($ErrorMessage, $FailedItem)" 
      }
      
      Write-Verbose "Eind Function : [$($MyInvocation.MyCommand)] *************************************"

    }
}


function Get-ContentDxWorld
{
    [CmdletBinding()]
    param
    (
        [Parameter(Position=0, Mandatory=$true)]
        [Uri] $URL
    )
    begin
    {
      Write-Verbose "Start Module : [$($MyInvocation.MyCommand)] *************************************"
      $arrDxCalls =@()

  
      Try 
      {
        Write-Verbose "Start Reading from $URL" 
        $result = Invoke-WebRequest $url
      } Catch 
      {
        $ErrorMessage = $_.Exception.Message
        $FailedItem = $_.Exception.ItemName
        Throw "Unable to open website $URL ($ErrorMessage, $FailedItem)" 
      }

      Write-Verbose "Reading content from site $URL)"
      Try 
      {
        $CalendarFileContent = $result.Content -split "`r`n" | Where-Object { $_ -like "SUMMARY*"}

        foreach ($Line in $CalendarFileContent) 
        {
          $Line = $Line.ToUpper().Trim() 
          If ($Line.StartsWith("SUMMARY")) 
          {
            $LineSplit = $Line.Split(':')
            $DxCalls = $LineSplit[1].Trim().Split(' ')
            #Write-Output "$LineSplit $DxCallsLine $DxCalls" -ForegroundColor Yellow

            foreach($DxCall In $DxCalls)
            { 
              $DxCall = $DxCall.Replace(")","")
              $DxCall = $DxCall.Replace("(","").Trim()
              If (Get-ValidCall ($DxCall)) 
              {
                $arrDxCalls += $DxCall
              } 
            }
        }
       }
      Return $arrDxCalls | Sort-Object -Unique
      } Catch 
      {
        $ErrorMessage = $_.Exception.Message
        $FailedItem = $_.Exception.ItemName
        Throw "Unable to read website $URL ($ErrorMessage, $FailedItem)" 
      }

      Write-Verbose "Eind Function : [$($MyInvocation.MyCommand)] *************************************"
   }
}


function Get-CallsFromWebsite
{
    begin
    {
      Write-Verbose "Start Function : [$($MyInvocation.MyCommand)] *************************************"
      
      $CalendarCalls = @()
      $Ng3KCalls = @()
      $objDxCalls= @()
      # https://dx-world.net/
      [uri]$CalendarURL       = 'https://www.google.com/calendar/ical/hamradioweb2007%40gmail.com/public/basic.ics'
      # Announced DX Operations
      [uri]$Ng3kURL           = "http://www.ng3k.com/misc/adxo.html"

      Write-Output "Reading website : $CalendarURL" 
      $CalendarCalls = Get-ContentDxWorld -URL $CalendarURL
      Write-Verbose "Calls Readed : $CalendarCalls"
      Write-Output "Calls Loaded : $($CalendarCalls.Count)" 
      #Write-DxCallsToCSV -objDxCalls $CalendarCalls -CSVFile $env:USERPROFILE\downloads\Calls_Agenda.csv

      Write-Output "Reading website : $Ng3kURL" 
      $Ng3KCalls = Get-ContentNg3K -URL $Ng3kURL
      Write-Verbose "Calls Readed : $Ng3KCalls"
      Write-Output "Calls Loaded : $($Ng3KCalls.Count)"
      #Write-DxCallsToCSV -objDxCalls $Ng3KCalls -CSVFile $env:USERPROFILE\downloads\Calls_Ng3K.csv

      $objDxCalls = Add-ConcatDxCall -DxCallsArray1 $CalendarCalls -DxCallsArray2 $Ng3KCalls
      $NumberInputCalls = $($objDxCalls.Count)
      $objDxCalls = Get-CallsCleanedUp -DxCallsArray $objDxCalls
      Write-Output "Remove duplicate callsigns, total $NumberInputCalls, returned $($objDxCalls.Count)" 
      return $objDxCalls
      
      Write-Verbose "Eind Function : [$($MyInvocation.MyCommand)] *************************************"
    }
}


function Update-DxHrdAlarmFile
{
    <#
        .SYNOPSIS
            Function is to create a Dx Cluster Alarm file for use In Ham Radio DeLuxe
        .DESCRIPTION
            This function downloads callsign from DX Expeditions from websites and place It In the Alarm file from Ham Radio DeLuxe
            If there is no entry In the "DX Cluster Alarm Defenitions" It creates one, existing will be updated.
            WARNING : The file DXClusterAlarms.xml must exists!
        .PARAMETER $HRDLogbookDirectory
            Optional : Specify this parameter if the DXClusterAlarms.xml file is on an other location.
            Notes:
                * Default path = C:\Users\<user>\AppData\Roaming\HRDLLC\HRD Logbook
        .EXAMPLE
            PS> Update-DxHrdAlarmFile $HRDLogbookDirectory ""
 
            This example creates the alarmfile on default location.
        .EXAMPLE
            PS> Update-DxHrdAlarmFile $HRDLogbookDirectory "$env:USERPROFILE\downloads"
 
            This example creates a alarmfile on location "C:\Users\<user>\downloads\"
        .EXAMPLE
            PS> Update-DxHrdAlarmFile -Verbose
 
            This example gives extra information about the internal steps.
        .EXAMPLE
        .INPUTS
        .OUTPUTS
            $null
    #>

    [CmdletBinding()]
    param
    (
        [Parameter(Position=0, Mandatory=$false)]
        [String] $HRDLogbookDirectory
    )
    begin
    {
      Write-Verbose "Start Module : [$($MyInvocation.MyCommand)] *************************************"
 
      if ($HRDLogbookDirectory)
      {
        $AlarmFile = "$HRDLogbookDirectory\DXClusterAlarms.xml"
      } else
      {
        $AlarmFile = "$env:APPDATA\HRDLLC\HRD Logbook\DXClusterAlarms.xml"
      }
      
      if (! (Test-Path $AlarmFile)) 
      {
        $ErrorMessage = $_.Exception.Message
        $FailedItem = $_.Exception.ItemName
        Throw "Unable to locate file $AlarmFile ($ErrorMessage, $FailedItem)" 
      } else
      {
        Write-Verbose "File found : $AlarmFile"
      }
      
      $AllDxCalls = Get-CallsFromWebsite    
      
      Write-Output "Write Ham Radio DeLuxe Alarm File : $AlarmFile" 
      Update-WriteHRDAlarmFile -DXClusterAlarmsFile $AlarmFile -DxCallsArray $AllDxCalls

      Write-Verbose "End Module : [$($MyInvocation.MyCommand)] *************************************"
    }
}


function Update-DxVe7ccAlarmFile
{
    <#
        .SYNOPSIS
            Function is to create a VE7CC Cluster Alarm file for use VE7CC Cluster software
        .DESCRIPTION
            This function downloads callsign from DX Expeditions from websites and place It In the Alarm file from VE7CC cluster
            WARNING : The file alarm.dat must exists!
        .PARAMETER VE7CCDirectory
            Mandatory : Specify this parameter to point to location of the directory where the alarm.dat and ve7cc.exe are installed.
 
        .PARAMETER RestartVE7CC
            Optional : Specify this parameter restart the VE7CC application.
 
        .EXAMPLE
            PS> Update-DxVe7ccAlarmFile -VE7CCDirectory "D:\VE7CC"
 
            This example change the alarmfile alarm.dat in this directory.
        .EXAMPLE
            PS> Update-DxVe7ccAlarmFile -VE7CCDirectory "D:\VE7CC" -RestartVE7CC
 
            This example change the alarmfile alarm.dat in this directory and restart VE7CC application, this will read the new alarm.dat.
        .EXAMPLE
            PS> Update-DxVe7ccAlarmFile -VE7CCDirectory "D:\VE7CC" -RestartVE7CC
 
            This example change the alarmfile alarm.dat in this directory and restart VE7CC application, this will read the new alarm.dat.
            This example gives extra information about the internal steps.
        .EXAMPLE
        .INPUTS
        .OUTPUTS
            $null
    #>

    [CmdletBinding()]
    param
    (
        [Parameter(Position=0, Mandatory=$false)]
        [String] $VE7CCDirectory,
        [Parameter(Position=0, Mandatory=$false)]
        [Switch] $RestartVE7CC

    )
    begin
    {
      Write-Verbose "Start Module : [$($MyInvocation.MyCommand)] *************************************"
 
      if ($VE7CCDirectory)
      {
        $AlarmFile = "$VE7CCDirectory\alarm.dat"
        $ve7ccProcessName = 've7cc'
        $ve7ccExe         = "$VE7CCDirectory\ve7cc.exe"
        $ve7ccActivity    = "VE7CC application, stopping and starting"
      }
      
      if ($DebugPreference -eq "Inquire")
      {
        $AlarmFile = "$env:USERPROFILE\downloads\alarm.dat"
        $ve7ccProcessName = 'notepad'
        $ve7ccExe         = "$env:WINDIR\notepad.exe"
        $ve7ccActivity    = "VE7CC application, stopping and starting"
        Write-Debug "Alarm file changend to $AlarmFile"
        Write-Debug ".exe changend to $ve7ccExe"
        Write-Output ".exe changend to $ve7ccExe"
      }
      
      if (! (Test-Path $AlarmFile)) 
      {
        $ErrorMessage = $_.Exception.Message
        $FailedItem = $_.Exception.ItemName
        Throw "Unable to locate file $AlarmFile ($ErrorMessage, $FailedItem)" 
      } else
      {
        Write-Verbose "File found : $AlarmFile"
      }
      
      $AllDxCalls = Get-CallsFromWebsite    
      
      Write-Output "Write VE7CC Alarm File : $AlarmFile" 
      Update-WriteVE7CCAlarmFile -AlarmFile $alarmFile -DxCallsArray $AllDxCalls

      if($RestartVE7CC)
      {
        Write-Output "$ve7ccActivity" 
        if (! (Test-Path $ve7ccExe)) 
        {
          $ErrorMessage = $_.Exception.Message
          $FailedItem = $_.Exception.ItemName
          Throw "Unable to locate file $($ve7ccExe) ($ErrorMessage, $FailedItem)" 
        } else
        {
          Stop-Application -ProcessName $ve7ccProcessName -ProcessActivity $ve7ccActivity
          Start-Application -ProcessName $ve7ccProcessName -ProcessToStart $ve7ccExe -ProcessActivity $ve7ccActivity
        }
      }
      
      Write-Verbose "End Module : [$($MyInvocation.MyCommand)] *************************************"
    }
}

<#
New-Alias -Name UdxA -Value Update-DxHrdAlarmFile
Export-ModuleMember -function Update-DxHrdAlarmFile -alias UdxA
New-Alias -Name UdxV -Value Update-DxVe7ccAlarmFile
Export-ModuleMember -function Update-DxVe7ccAlarmFile -alias UdxV
#>


Export-ModuleMember -function Update-DxHrdAlarmFile 
Export-ModuleMember -function Update-DxVe7ccAlarmFile