|
| 1 | +function Invoke-IcingaWindowsScheduledTask() |
| 2 | +{ |
| 3 | + param ( |
| 4 | + [ValidateSet('UninstallAgent', 'UpgradeAgent', 'ReadMSIPackage', 'InstallJEA', 'StartWindowsService', 'StopWindowsService', 'RestartWindowsService', 'GetWindowsService')] |
| 5 | + [string]$JobType = '', |
| 6 | + [string]$FilePath = '', |
| 7 | + [string]$TargetPath = '', |
| 8 | + [string]$ObjectName = '' |
| 9 | + ); |
| 10 | + |
| 11 | + if ((Test-AdministrativeShell) -eq $FALSE) { |
| 12 | + Write-IcingaConsoleError 'You require to run this shell in administrative mode for the action "{0}" and object "{1}"' -Objects $JobType, $ObjectName; |
| 13 | + return $null; |
| 14 | + } |
| 15 | + |
| 16 | + [string]$TaskName = 'Management Task'; |
| 17 | + [string]$TaskPath = '\Icinga\Icinga for Windows\'; |
| 18 | + $TaskData = $null; |
| 19 | + $TmpFile = New-IcingaTemporaryFile; |
| 20 | + |
| 21 | + if (Get-ScheduledTask -TaskName $TaskName -TaskPath $TaskPath -ErrorAction SilentlyContinue) { |
| 22 | + Unregister-ScheduledTask -TaskName $TaskName -TaskPath $TaskPath -Confirm:$FALSE -ErrorAction SilentlyContinue | Out-Null; |
| 23 | + } |
| 24 | + |
| 25 | + switch ($JobType) { |
| 26 | + 'StartWindowsService' { |
| 27 | + $TaskData = Invoke-IcingaWindowsServiceHandlerTask -ScriptPath 'jobs\StartWindowsService.ps1' -ServiceName $ObjectName -TmpFile $TmpFile.FullName -TaskName $TaskName -TaskPath $TaskPath; |
| 28 | + }; |
| 29 | + 'StopWindowsService' { |
| 30 | + $TaskData = Invoke-IcingaWindowsServiceHandlerTask -ScriptPath 'jobs\StopWindowsService.ps1' -ServiceName $ObjectName -TmpFile $TmpFile.FullName -TaskName $TaskName -TaskPath $TaskPath; |
| 31 | + }; |
| 32 | + 'RestartWindowsService' { |
| 33 | + $TaskData = Invoke-IcingaWindowsServiceHandlerTask -ScriptPath 'jobs\RestartWindowsService.ps1' -ServiceName $ObjectName -TmpFile $TmpFile.FullName -TaskName $TaskName -TaskPath $TaskPath; |
| 34 | + }; |
| 35 | + 'GetWindowsService' { |
| 36 | + $TaskData = Invoke-IcingaWindowsServiceHandlerTask -ScriptPath 'jobs\GetWindowsService.ps1' -ServiceName $ObjectName -TmpFile $TmpFile.FullName -TaskName $TaskName -TaskPath $TaskPath; |
| 37 | + }; |
| 38 | + 'UninstallAgent' { |
| 39 | + $WinAction = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument ([string]::Format('-WindowStyle Hidden -Command &{{ Use-Icinga -Minimal; Write-IcingaFileSecure -File {0}{1}{0} -Value (Start-IcingaProcess -Executable {0}MsiExec.exe{0} -Arguments {0}"{2}" /q{0} -FlushNewLines | ConvertTo-Json -Depth 100); }}', "'", $TmpFile.FullName, $FilePath, $TargetPath)) |
| 40 | + Register-ScheduledTask -TaskName $TaskName -Action $WinAction -RunLevel Highest -TaskPath $TaskPath | Out-Null; |
| 41 | + |
| 42 | + Start-ScheduledTask -TaskName $TaskName -TaskPath $TaskPath; |
| 43 | + |
| 44 | + Wait-IcingaWindowsScheduledTask; |
| 45 | + # Wait some time before continuing to ensure the service is properly removed |
| 46 | + Start-Sleep -Seconds 2; |
| 47 | + |
| 48 | + [string]$TaskOutput = Read-IcingaFileSecure -File $TmpFile.FullName; |
| 49 | + $TaskData = ConvertFrom-Json $TaskOutput; |
| 50 | + }; |
| 51 | + 'UpgradeAgent' { |
| 52 | + |
| 53 | + }; |
| 54 | + 'ReadMSIPackage' { |
| 55 | + if (Test-Path $FilePath) { |
| 56 | + |
| 57 | + $WinAction = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument ([string]::Format('-WindowStyle Hidden -Command &{{ Use-Icinga -Minimal; Write-IcingaFileSecure -File {0}{1}{0} -Value (Read-IcingaMSIMetadata -File {0}{2}{0} | ConvertTo-Json -Depth 100); }}', "'", $TmpFile.FullName, $FilePath)) |
| 58 | + Register-ScheduledTask -TaskName $TaskName -Action $WinAction -RunLevel Highest -TaskPath $TaskPath | Out-Null; |
| 59 | + |
| 60 | + Start-ScheduledTask -TaskName $TaskName -TaskPath $TaskPath; |
| 61 | + |
| 62 | + Wait-IcingaWindowsScheduledTask; |
| 63 | + |
| 64 | + [string]$TaskOutput = Read-IcingaFileSecure -File $TmpFile.FullName; |
| 65 | + $TaskData = ConvertFrom-Json $TaskOutput; |
| 66 | + } else { |
| 67 | + Write-IcingaConsoleError 'Unable to execute Job Type {0} because the specified file "{1}" does not exist' -Objects $JobType, $FilePath; |
| 68 | + } |
| 69 | + }; |
| 70 | + 'InstallJEA' { |
| 71 | + $WinAction = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument ([string]::Format('-Command &{{ Use-Icinga -Minimal; Install-IcingaJEAProfile; Restart-IcingaWindowsService; }}', "'", $TmpFile.FullName, $FilePath)) |
| 72 | + Register-ScheduledTask -TaskName $TaskName -Action $WinAction -RunLevel Highest -TaskPath $TaskPath | Out-Null; |
| 73 | + Start-ScheduledTask -TaskName $TaskName -TaskPath $TaskPath; |
| 74 | + |
| 75 | + Wait-IcingaWindowsScheduledTask; |
| 76 | + |
| 77 | + # No output data required for this task |
| 78 | + }; |
| 79 | + Default { |
| 80 | + Write-IcingaConsoleError 'Unable to execute Job Type {0}. Undefined operation' -Objects $JobType; |
| 81 | + }; |
| 82 | + }; |
| 83 | + |
| 84 | + if (Get-ScheduledTask -TaskName $TaskName -TaskPath $TaskPath -ErrorAction SilentlyContinue) { |
| 85 | + Unregister-ScheduledTask -TaskName $TaskName -TaskPath $TaskPath -Confirm:$FALSE -ErrorAction SilentlyContinue | Out-Null; |
| 86 | + } |
| 87 | + |
| 88 | + if (Test-Path $TmpFile) { |
| 89 | + Remove-Item -Path $TmpFile -Force; |
| 90 | + } |
| 91 | + |
| 92 | + return $TaskData; |
| 93 | +} |
0 commit comments