2021-11-03 10:06:54 -04:00
|
|
|
<#
|
|
|
|
|
.SYNOPSIS
|
|
|
|
|
Updates a Icinga for Windows manifest file by updating NestedModules for
|
|
|
|
|
easier usage
|
|
|
|
|
.DESCRIPTION
|
|
|
|
|
Updates a Icinga for Windows manifest file by updating NestedModules for
|
|
|
|
|
easier usage
|
|
|
|
|
.PARAMETER Name
|
|
|
|
|
The name of the Icinga for Windows component to edit
|
|
|
|
|
.PARAMETER ModuleConfig
|
|
|
|
|
Configuration parsed as hashtable to update our manifest template with proper data
|
|
|
|
|
.PARAMETER ModuleList
|
|
|
|
|
An array of PowerShell module files within module to update the NestedModule entry with
|
|
|
|
|
#>
|
|
|
|
|
function Write-IcingaForWindowsComponentManifest()
|
|
|
|
|
{
|
|
|
|
|
param (
|
|
|
|
|
[string]$Name,
|
|
|
|
|
[hashtable]$ModuleConfig = @{ },
|
2022-02-18 10:29:24 -05:00
|
|
|
[array]$ModuleList = @(),
|
|
|
|
|
[array]$FunctionList = @(),
|
|
|
|
|
[array]$CmdletList = @(),
|
|
|
|
|
[array]$VariableList = @(),
|
|
|
|
|
[array]$AliasList = @()
|
2021-11-03 10:06:54 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ([string]::IsNullOrEmpty($Name)) {
|
|
|
|
|
Write-IcingaConsoleError 'Please specify a name for writing the component manifest';
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$PSDefaultParameterValues = @{ '*:Encoding' = 'utf8' };
|
|
|
|
|
|
|
|
|
|
[string]$ModuleName = [string]::Format('icinga-powershell-{0}', $Name.ToLower());
|
|
|
|
|
[string]$ModuleRoot = Get-IcingaForWindowsRootPath;
|
|
|
|
|
[string]$ModuleDir = Join-Path -Path $ModuleRoot -ChildPath $ModuleName;
|
|
|
|
|
[string]$ManifestFileData = Read-IcingaFileSecure -File (Join-Path -Path $ModuleDir -ChildPath ([string]::Format('{0}.psd1', $ModuleName)));
|
|
|
|
|
$ContentString = New-Object -TypeName 'System.Text.StringBuilder';
|
|
|
|
|
|
|
|
|
|
if ([string]::IsNullOrEmpty($ManifestFileData)) {
|
|
|
|
|
Write-IcingaConsoleWarning 'The manifest file of module "{0}" could not be loaded' -Objects $ModuleName;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$ManifestFileData = $ManifestFileData.Substring($ManifestFileData.IndexOf('@'), $ManifestFileData.Length - $ManifestFileData.IndexOf('@'));
|
|
|
|
|
|
|
|
|
|
if ($null -ne $ModuleConfig -And $ModuleConfig.Count -ne 0) {
|
|
|
|
|
foreach ($entry in $ModuleConfig.Keys) {
|
|
|
|
|
$Value = $ModuleConfig[$entry];
|
|
|
|
|
|
|
|
|
|
if ($entry -Like '$TAGS$') {
|
|
|
|
|
$Value = (ConvertFrom-IcingaArrayToString -Array $Value -AddQuotes -UseSingleQuotes)
|
|
|
|
|
} elseif ($entry -Like '$REQUIREDMODULES$') {
|
|
|
|
|
[int]$CurrentIndex = 0;
|
|
|
|
|
foreach ($module in $Value) {
|
|
|
|
|
$CurrentIndex += 1;
|
2021-11-10 04:24:42 -05:00
|
|
|
$ContentString.Append('@{ ') | Out-Null;
|
2021-11-03 10:06:54 -04:00
|
|
|
|
|
|
|
|
foreach ($dependency in $module.Keys) {
|
|
|
|
|
$DependencyValue = $module[$dependency];
|
|
|
|
|
|
2021-11-10 04:24:42 -05:00
|
|
|
$ContentString.Append([string]::Format("{0} = '{1}'; ", $dependency, $DependencyValue)) | Out-Null;
|
2021-11-03 10:06:54 -04:00
|
|
|
}
|
2021-11-10 04:24:42 -05:00
|
|
|
$ContentString.Append('}') | Out-Null;
|
2021-11-03 10:06:54 -04:00
|
|
|
|
|
|
|
|
if ($CurrentIndex -ne $Value.Count) {
|
2021-11-10 04:24:42 -05:00
|
|
|
$ContentString.Append(",`r`n ") | Out-Null;
|
2021-11-03 10:06:54 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$Value = $ContentString.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$ManifestFileData = $ManifestFileData.Replace($entry, $Value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Write-IcingaFileSecure -File (Join-Path -Path $ModuleDir -ChildPath ([string]::Format('{0}.psd1', $ModuleName))) -Value $ManifestFileData;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-10 04:24:42 -05:00
|
|
|
$ContentString.Clear() | Out-Null;
|
2021-11-03 10:06:54 -04:00
|
|
|
|
2022-02-18 10:29:24 -05:00
|
|
|
[string]$ManifestFile = (Join-Path -Path $ModuleDir -ChildPath ([string]::Format('{0}.psd1', $ModuleName)));
|
2021-11-03 10:06:54 -04:00
|
|
|
|
2022-02-18 10:29:24 -05:00
|
|
|
Update-IcingaForWindowsManifestArray -ArrayVariableName 'NestedModules' -ArrayVariableValues $ModuleList -ManifestFile $ManifestFile;
|
|
|
|
|
Update-IcingaForWindowsManifestArray -ArrayVariableName 'FunctionsToExport' -ArrayVariableValues $FunctionList -ManifestFile $ManifestFile;
|
|
|
|
|
Update-IcingaForWindowsManifestArray -ArrayVariableName 'CmdletsToExport' -ArrayVariableValues $CmdletList -ManifestFile $ManifestFile;
|
|
|
|
|
Update-IcingaForWindowsManifestArray -ArrayVariableName 'VariablesToExport' -ArrayVariableValues $VariableList -ManifestFile $ManifestFile;
|
|
|
|
|
Update-IcingaForWindowsManifestArray -ArrayVariableName 'AliasesToExport' -ArrayVariableValues $AliasList -ManifestFile $ManifestFile;
|
2021-11-03 10:06:54 -04:00
|
|
|
|
2022-02-18 10:29:24 -05:00
|
|
|
Import-Module -Name $ManifestFile -Force;
|
|
|
|
|
Import-Module -Name $ManifestFile -Force -Global;
|
2021-11-03 10:06:54 -04:00
|
|
|
}
|