icinga-powershell-framework/lib/core/jea/Get-IcingaFrameworkDependency.psm1
2021-09-02 09:23:10 +02:00

44 lines
1.5 KiB
PowerShell

function Get-IcingaFrameworkDependency()
{
param (
[string]$Command = $null,
$DependencyList = (New-Object PSCustomObject)
);
if (Test-PSCustomObjectMember -PSObject $DependencyList -Name $Command) {
return $DependencyList;
}
$DependencyList | Add-Member -MemberType NoteProperty -Name ($Command) -Value (New-Object PSCustomObject);
$CommandConfig = (Get-Command $Command);
$ModuleContent = $CommandConfig.ScriptBlock.ToString();
$DeserializedFile = Read-IcingaPowerShellModuleFile -FileContent $ModuleContent;
[array]$CheckCmd = $DeserializedFile.CommandList + $DeserializedFile.FunctionList;
foreach ($cmd in $CheckCmd) {
if ($cmd -eq $Command) {
continue;
}
$CommandConfig = Get-Command $cmd -ErrorAction SilentlyContinue;
if ($null -eq $CommandConfig) {
continue;
}
[string]$CommandType = ([string]$CommandConfig.CommandType).Replace(' ', '');
if ((Test-PSCustomObjectMember -PSObject ($DependencyList.$Command) -Name $CommandType) -eq $FALSE) {
$DependencyList.$Command | Add-Member -MemberType NoteProperty -Name ($CommandType) -Value (New-Object PSCustomObject);
}
if ((Test-PSCustomObjectMember -PSObject ($DependencyList.$Command.$CommandType) -Name $cmd) -eq $FALSE) {
$DependencyList.$Command.$CommandType | Add-Member -MemberType NoteProperty -Name ($cmd) -Value 0;
}
$DependencyList.$Command.$CommandType.($cmd) += 1;
}
return $DependencyList;
}