icinga-powershell-framework/lib/core/jea/Get-IcingaCommandDependency.psm1

66 lines
2.3 KiB
PowerShell
Raw Normal View History

2021-08-06 12:12:27 -04:00
function Get-IcingaCommandDependency()
{
param (
$DependencyList = (New-Object PSCustomObject),
[hashtable]$CompiledList = @{ },
[string]$CmdName = '',
[string]$CmdType = ''
);
# Function, Cmdlet, Alias, Modules, Application
2021-08-06 12:12:27 -04:00
if ([string]::IsNullOrEmpty($CmdType)) {
return $CompiledList;
}
# Create the list container for our object type if not existing
# => Function, Cmdlet, Alias, Modules, Application
2021-08-06 12:12:27 -04:00
if ($CompiledList.ContainsKey($CmdType) -eq $FALSE) {
$CompiledList.Add($CmdType, @{ });
}
# e.g. Invoke-IcingaCheckCPU
2021-08-06 12:12:27 -04:00
if ($CompiledList[$CmdType].ContainsKey($CmdName)) {
$CompiledList[$CmdType][$CmdName] += 1;
2021-08-06 12:12:27 -04:00
return $CompiledList;
}
# Add the command this function is called with
2021-08-06 12:12:27 -04:00
$CompiledList[$CmdType].Add($CmdName, 0);
# The command is not known in our Framework dependency list -> could be a native Windows command
2021-08-06 12:12:27 -04:00
if ((Test-PSCustomObjectMember -PSObject $DependencyList -Name $CmdName) -eq $FALSE) {
return $CompiledList;
}
# Loop our entire dependency list for every single command
2021-08-06 12:12:27 -04:00
foreach ($CmdList in $DependencyList.$CmdName.PSObject.Properties.Name) {
# $Cmd => The list of child commands
# $CmdList => Function, Cmdlet, Alias, Modules, Application
2021-08-06 12:12:27 -04:00
$Cmd = $DependencyList.$CmdName.$CmdList;
# Create the list container for our object type if not existing
# => Function, Cmdlet, Alias, Modules, Application
2021-08-06 12:12:27 -04:00
if ($CompiledList.ContainsKey($CmdList) -eq $FALSE) {
$CompiledList.Add($CmdList, @{ });
}
# Loop all commands within our child list for this command
2021-08-06 12:12:27 -04:00
foreach ($entry in $Cmd.PSObject.Properties.Name) {
# $entry => The command name e.g. Write-IcingaConsolePlain
if ($CompiledList[$CmdList].ContainsKey($entry) -eq $FALSE) {
2021-08-06 12:12:27 -04:00
$CompiledList = Get-IcingaCommandDependency `
-DependencyList $DependencyList `
-CompiledList $CompiledList `
-CmdName $entry `
-CmdType $CmdList;
} else {
$CompiledList[$CmdList][$entry] += 1;
}
2021-08-06 12:12:27 -04:00
}
}
return $CompiledList;
}