Functions/HealthMonitor/Install-FpsHealthMonitor.ps1
function Install-FpsHealthMonitor { param( # Microsoft Customer ID, found in .flf Business Central license file. #[Parameter(Mandatory=$true)] [string] $CustomerId, # Business Central Server Instance Name. [Parameter(Mandatory=$true)] [string] $ServerInstance, [string] $UserName = ('{0}\{1}' -f $env:USERDOMAIN, $env:USERNAME), [Parameter(Mandatory=$true)] [string] $Password, [string] $DestinationPath = (Join-Path -Path $env:ProgramData -ChildPath '4ps\healthMonitor'), [int32] $DaysInterval = 1 ) $logFilePath = (Join-Path $env:LOCALAPPDATA '4ps\healthMonitor\') # Create folders for Data Collector if((Test-Path $DestinationPath) -eq $false){ New-Item $DestinationPath -ItemType Directory -Force | Out-Null } if((Test-Path $logFilePath) -eq $false){ New-Item $logFilePath -ItemType Directory -Force | Out-Null } <# Create Json file with customer info @{ 'CustomerId' = $CustomerId 'ServerInstance' = $ServerInstance } | ConvertTo-Json | Out-File -FilePath (Join-Path $DestinationPath 'DataCollectorSettings.json') #> # Save Data Collector script to disk Get-FpsHealthMonitorScript | Out-File -FilePath (Join-Path $DestinationPath 'FpsHealthMonitor.ps1') # Create Windows Task $taskName = '4PS Health Monitor' $command = "Start-Transcript -path (Join-Path $logFilePath 'healthMonitor.log'); " if($CustomerId){ $command += '$CustomerId = ''{0}'';' -f $CustomerId } if($ServerInstance){ $command += '$ServerInstance = ''{0}'';' -f $ServerInstance } $command += '$Path = ''{0}''; ' -f (Join-Path $DestinationPath 'FpsHealthMonitor.ps1') $command += 'if ((Test-Path $Path -ErrorAction Continue) -eq $true){. $Path}; ' $command += 'Stop-Transcript' $taskArguments = @{ 'TaskName' = $taskName 'Description' = 'Periodically sends Business Central and Extension version information to 4PS. Version compatibility monitor.' 'User' = $UserName 'Password' = $Password 'RunLevel' = 'Highest' 'Action' = New-ScheduledTaskAction -Execute 'Powershell' -Argument ('-NoProfile -WindowStyle Hidden -command "{0}"' -f $command) 'Trigger' = New-ScheduledTaskTrigger -Daily -DaysInterval $DaysInterval -At ('5:{0}' -f (Get-Random -Maximum 59 -Minimum 00)) 'Settings' = New-ScheduledTaskSettingsSet -RunOnlyIfNetworkAvailable -StartWhenAvailable -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries } Remove-FpsHealthMonitor Register-ScheduledTask @taskArguments -Force | Out-Null } Export-ModuleMember -Function Install-FpsHealthMonitor |