public/Invoke-UpdateWebTemplate.ps1
function Invoke-UpdateWebTemplate { [CmdletBinding()] param( [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [string]$Competition, [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [string]$Date, [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [string]$Path ) process{ $ErrorActionPreference = 'Stop' try { $MarketTypes = Get-ChildItem -Path "$Path\sportsmonk-predictions\$Competition\$Date" -File -Filter '*.csv' foreach ($MarketType in $MarketTypes) { if ($($MarketType.Name) -like '*240*') { continue } # if $csvData = Import-Csv -Path $($MarketType.FullName) # Convert to JavaScript array format $jsData = $csvData | ConvertTo-Json -Compress switch ($($MarketType.Name)) { {$_ -like '*231*' -or $_ -like '*234*' -or $_ -like '*235*' -or $_ -like '*236*' -or $_ -like '*32*' -or $_ -like '*33*'} { $TemplateToUse = 'yes_no_template.html' } {$_ -like '*232*'} { $TemplateToUse = '232_htft_template.html' } {$_ -like '*239*'} { $TemplateToUse = 'drawhome_drawaway_homeaway_template.html' } {$_ -like '*233*' -or $_ -like '*237*' -or $_ -like '*238*'} { $TemplateToUse = 'home_draw_away_template.html' } {$_ -like '*16*'} { $TemplateToUse = 'yes_no_equal_template.html' } } # switch # Read the HTML template $htmlTemplate = Get-Content -Path "$Path\git-hub\sportsmonk-api\templates\html\$TemplateToUse" -Raw # Replace the placeholder with actual data $htmlWithData = $htmlTemplate -replace '/\* CSV_DATA_PLACEHOLDER \*/', $jsData # Set current file name $CurrentFileName = $($MarketType.Name) # Rename to have html file extension $NewFileName = $CurrentFileName.Replace('.csv','.html') # Save the populated HTML file $htmlWithData | Out-File "$Path\sportsmonk-predictions\$Competition\$Date\$NewFileName" -Encoding UTF8 } # foreach } catch { "$($MyInvocation.MyCommand.Name): $_.Exception.Message" } # trycatch } # process } # function |