mirror of
https://github.com/Icinga/icinga-powershell-framework.git
synced 2025-12-20 23:00:35 -05:00
Improves testing if Add-Type is already loaded
This commit is contained in:
parent
2832383d22
commit
32c4541397
6 changed files with 137 additions and 13 deletions
|
|
@ -17,6 +17,10 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
|
|||
* [#403](https://github.com/Icinga/icinga-powershell-framework/pull/403) Fixes memory leak on newly EventLog reader for CLI event stream
|
||||
* [#407](https://github.com/Icinga/icinga-powershell-framework/pull/407) Removes unnecessary module import inside `Invoke-IcingaNamespaceCmdlets`
|
||||
|
||||
### Enhancements
|
||||
|
||||
* [#388](https://github.com/Icinga/icinga-powershell-framework/issues/388) Improves performance for testing if `Add-Type` functions have been added, by adding an internal test for newly introduced environment variables within a PowerShell session
|
||||
|
||||
## 1.7.1 (2021-11-11)
|
||||
|
||||
[Issue and PRs](https://github.com/Icinga/icinga-powershell-framework/milestone/22?closed=1)
|
||||
|
|
|
|||
33
lib/core/framework/Get-IcingaPrivateEnvironmentVariable.psm1
Normal file
33
lib/core/framework/Get-IcingaPrivateEnvironmentVariable.psm1
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
<#
|
||||
.SYNOPSIS
|
||||
Reads a private environment variable from Icinga for Windows
|
||||
of the current PowerShell session
|
||||
.DESCRIPTION
|
||||
Reads a private environment variable from Icinga for Windows
|
||||
of the current PowerShell session
|
||||
.PARAMETER Name
|
||||
The name of the variable to load the content from
|
||||
.EXAMPLE
|
||||
Get-IcingaPrivateEnvironmentVariable -Name 'AddTypeFunctions';
|
||||
.NOTES
|
||||
General notes
|
||||
#>
|
||||
function Get-IcingaPrivateEnvironmentVariable()
|
||||
{
|
||||
param (
|
||||
[string]$Name
|
||||
);
|
||||
|
||||
if ([string]::IsNullOrEmpty($Name)) {
|
||||
return $null;
|
||||
}
|
||||
|
||||
# Setup the environments in case not present already
|
||||
New-IcingaEnvironmentVariable;
|
||||
|
||||
if ($global:Icinga.Private.ContainsKey($Name) -eq $FALSE) {
|
||||
return $null;
|
||||
}
|
||||
|
||||
return $global:Icinga.Private[$Name];
|
||||
}
|
||||
|
|
@ -44,17 +44,5 @@ function New-IcingaCheckSchedulerEnvironment()
|
|||
$IcingaDaemonData.IcingaThreadContent.Add('Scheduler', @{ });
|
||||
}
|
||||
|
||||
if ($null -eq $global:Icinga) {
|
||||
$global:Icinga = @{ };
|
||||
}
|
||||
|
||||
if ($global:Icinga.ContainsKey('CheckResults') -eq $FALSE) {
|
||||
$global:Icinga.Add('CheckResults', @());
|
||||
}
|
||||
if ($global:Icinga.ContainsKey('PerfData') -eq $FALSE) {
|
||||
$global:Icinga.Add('PerfData', @());
|
||||
}
|
||||
if ($global:Icinga.ContainsKey('CheckData') -eq $FALSE) {
|
||||
$global:Icinga.Add('CheckData', @{ });
|
||||
}
|
||||
New-IcingaEnvironmentVariable;
|
||||
}
|
||||
|
|
|
|||
37
lib/core/framework/New-IcingaEnvironmentVariable.psm1
Normal file
37
lib/core/framework/New-IcingaEnvironmentVariable.psm1
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
<#
|
||||
.SYNOPSIS
|
||||
Creates all environment variables for Icinga for Windows for the
|
||||
PowerShell session
|
||||
.DESCRIPTION
|
||||
Creates all environment variables for Icinga for Windows for the
|
||||
PowerShell session
|
||||
.EXAMPLE
|
||||
New-IcingaEnvironmentVariable;
|
||||
#>
|
||||
|
||||
function New-IcingaEnvironmentVariable()
|
||||
{
|
||||
if ($null -eq $global:Icinga) {
|
||||
$global:Icinga = @{ };
|
||||
}
|
||||
|
||||
# Session specific configuration for this shell
|
||||
if ($global:Icinga.ContainsKey('Private') -eq $FALSE) {
|
||||
$global:Icinga.Add('Private', @{ });
|
||||
}
|
||||
|
||||
# Shared configuration for all threads
|
||||
if ($global:Icinga.ContainsKey('Public') -eq $FALSE) {
|
||||
$global:Icinga.Add('Public', [hashtable]::Synchronized(@{ }));
|
||||
}
|
||||
|
||||
if ($global:Icinga.ContainsKey('CheckResults') -eq $FALSE) {
|
||||
$global:Icinga.Add('CheckResults', @());
|
||||
}
|
||||
if ($global:Icinga.ContainsKey('PerfData') -eq $FALSE) {
|
||||
$global:Icinga.Add('PerfData', @());
|
||||
}
|
||||
if ($global:Icinga.ContainsKey('CheckData') -eq $FALSE) {
|
||||
$global:Icinga.Add('CheckData', @{ });
|
||||
}
|
||||
}
|
||||
36
lib/core/framework/Set-IcingaPrivateEnvironmentVariable.psm1
Normal file
36
lib/core/framework/Set-IcingaPrivateEnvironmentVariable.psm1
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
<#
|
||||
.SYNOPSIS
|
||||
Sets a private variable for the Icinga for Windows environment
|
||||
to use within the current PowerShell Session
|
||||
.DESCRIPTION
|
||||
Sets a private variable for the Icinga for Windows environment
|
||||
to use within the current PowerShell Session
|
||||
.PARAMETER Name
|
||||
The name of the variable
|
||||
.PARAMETER Value
|
||||
The value the variable will be assigned with
|
||||
.EXAMPLE
|
||||
Set-IcingaPrivateEnvironmentVariable -Name 'AddTypeFunctions' -Value @{ 'IcingaDiskAttributes', $TRUE };
|
||||
#>
|
||||
|
||||
function Set-IcingaPrivateEnvironmentVariable()
|
||||
{
|
||||
param (
|
||||
[string]$Name,
|
||||
$Value
|
||||
);
|
||||
|
||||
if ([string]::IsNullOrEmpty($Name)) {
|
||||
return;
|
||||
}
|
||||
|
||||
# Setup the environments in case not present already
|
||||
New-IcingaEnvironmentVariable;
|
||||
|
||||
if ($global:Icinga.Private.ContainsKey($Name) -eq $FALSE) {
|
||||
$global:Icinga.Private.Add($Name, $Value);
|
||||
return;
|
||||
}
|
||||
|
||||
$global:Icinga.Private[$Name] = $Value;
|
||||
}
|
||||
|
|
@ -1,3 +1,15 @@
|
|||
<#
|
||||
.SYNOPSIS
|
||||
Tests if a Add-Type function is already installed inside the current
|
||||
PowerShell session
|
||||
.DESCRIPTION
|
||||
Tests if a Add-Type function is already installed inside the current
|
||||
PowerShell session
|
||||
.PARAMETER Type
|
||||
The name of the function being added
|
||||
.EXAMPLE
|
||||
Test-IcingaAddTypeExis -Type 'IcingaDiskAttributes';
|
||||
#>
|
||||
function Test-IcingaAddTypeExist()
|
||||
{
|
||||
param (
|
||||
|
|
@ -8,8 +20,22 @@ function Test-IcingaAddTypeExist()
|
|||
return $FALSE;
|
||||
}
|
||||
|
||||
[hashtable]$LoadedTypes = Get-IcingaPrivateEnvironmentVariable -Name 'AddTypeFunctions';
|
||||
|
||||
if ($null -eq $LoadedTypes) {
|
||||
$LoadedTypes = @{ };
|
||||
}
|
||||
|
||||
if ($LoadedTypes.ContainsKey($Type)) {
|
||||
return $TRUE;
|
||||
}
|
||||
|
||||
foreach ($entry in [System.AppDomain]::CurrentDomain.GetAssemblies()) {
|
||||
if ($entry.GetTypes() -Match $Type) {
|
||||
$LoadedTypes.Add($Type, $TRUE);
|
||||
|
||||
Set-IcingaPrivateEnvironmentVariable -Name 'AddTypeFunctions' -Value $LoadedTypes;
|
||||
|
||||
return $TRUE;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue