From 64ceb81955ed175de84896da9711deef74e70841 Mon Sep 17 00:00:00 2001 From: Lord Hepipud Date: Wed, 9 Feb 2022 12:22:26 +0100 Subject: [PATCH] Fixes Add-Type exceptions for runtime compiling --- doc/31-Changelog.md | 8 +++++ icinga-powershell-plugins.psd1 | 2 +- provider/disks/Get-IcingaDiskAttributes.psm1 | 18 +++++------ provider/disks/Get-IcingaUNCPathSize.psm1 | 34 +++++++++----------- 4 files changed, 33 insertions(+), 29 deletions(-) diff --git a/doc/31-Changelog.md b/doc/31-Changelog.md index 43c5fc68..c073a877 100644 --- a/doc/31-Changelog.md +++ b/doc/31-Changelog.md @@ -7,6 +7,14 @@ documentation before upgrading to a new release. Released closed milestones can be found on [GitHub](https://github.com/Icinga/icinga-powershell-plugins/milestones?state=closed). +## 1.9.0 (2022-05-03) + +[Issue and PRs](https://github.com/Icinga/icinga-powershell-plugins/milestone/12?closed=1) + +### Bugfixes + +* [#283](https://github.com/Icinga/icinga-powershell-plugins/pull/283) Fixes random `Add-Type` exceptions during runtime compilation by replacing it with `Add-IcingaAddTypeLib` + ## 1.8.0 (2022-02-08) [Issue and PRs](https://github.com/Icinga/icinga-powershell-plugins/milestone/11?closed=1) diff --git a/icinga-powershell-plugins.psd1 b/icinga-powershell-plugins.psd1 index d33054aa..fd2ca516 100644 --- a/icinga-powershell-plugins.psd1 +++ b/icinga-powershell-plugins.psd1 @@ -7,7 +7,7 @@ Copyright = '(c) 2021 Icinga GmbH | GPLv2' Description = 'A collection of Icinga Plugins for general Windows checks for Icinga for Windows.' PowerShellVersion = '4.0' - RequiredModules = @(@{ModuleName = 'icinga-powershell-framework'; ModuleVersion = '1.7.0' }) + RequiredModules = @(@{ModuleName = 'icinga-powershell-framework'; ModuleVersion = '1.9.0' }) NestedModules = @( '.\plugins\Invoke-IcingaCheckBiosSerial.psm1', '.\plugins\Invoke-IcingaCheckCertificate.psm1', diff --git a/provider/disks/Get-IcingaDiskAttributes.psm1 b/provider/disks/Get-IcingaDiskAttributes.psm1 index 08e5985a..672d5702 100644 --- a/provider/disks/Get-IcingaDiskAttributes.psm1 +++ b/provider/disks/Get-IcingaDiskAttributes.psm1 @@ -74,17 +74,15 @@ function Get-IcingaDiskAttributes() } "@ - if ((Test-IcingaAddTypeExist 'IcingaDiskAttributes') -eq $FALSE) { - try { - Add-Type -TypeDefinition $IcingaDiskAttributesClass; - } catch { - [string]$ExErrorId = $_.FullyQualifiedErrorId; + try { + Add-IcingaAddTypeLib -TypeName 'IcingaDiskAttributes' -TypeDefinition $IcingaDiskAttributesClass; + } catch { + [string]$ExErrorId = $_.FullyQualifiedErrorId; - Exit-IcingaThrowCritical ` - -Message 'Failed to process disk related checks. Based on the error your local Windows disk partition has no space left' ` - -FilterString $ExErrorId ` - -SearchString 'System.IO.IOException'; - } + Exit-IcingaThrowCritical ` + -Message 'Failed to process disk related checks. Based on the error your local Windows disk partition has no space left' ` + -FilterString $ExErrorId ` + -SearchString 'System.IO.IOException'; } [bool]$DiskOffline = $FALSE; diff --git a/provider/disks/Get-IcingaUNCPathSize.psm1 b/provider/disks/Get-IcingaUNCPathSize.psm1 index ef10ed6f..4e37ed07 100644 --- a/provider/disks/Get-IcingaUNCPathSize.psm1 +++ b/provider/disks/Get-IcingaUNCPathSize.psm1 @@ -12,24 +12,22 @@ function Get-IcingaUNCPathSize() -Force; } - if ((Test-IcingaAddTypeExist -Type 'IcingaUNCPath') -eq $FALSE) { - # Register our kernel32.dll Windows API function call - Add-Type -TypeDefinition @" - using System; - using System.Runtime.InteropServices; - - public static class IcingaUNCPath { - [DllImport("kernel32.dll", PreserveSig = true, CharSet = CharSet.Auto)] - - public static extern int GetDiskFreeSpaceEx( - IntPtr lpDirectoryName, // UNC Path for share - out long lpFreeBytesAvailable, // Free Bytes available on path - out long lpTotalNumberOfBytes, // Bytes available on target disk / path - out long lpTotalNumberOfFreeBytes // Total available space on target disk / path - ); - } + # Register our kernel32.dll Windows API function call + Add-IcingaAddTypeLib -TypeName 'IcingaUNCPath' -TypeDefinition @" + using System; + using System.Runtime.InteropServices; + + public static class IcingaUNCPath { + [DllImport("kernel32.dll", PreserveSig = true, CharSet = CharSet.Auto)] + + public static extern int GetDiskFreeSpaceEx( + IntPtr lpDirectoryName, // UNC Path for share + out long lpFreeBytesAvailable, // Free Bytes available on path + out long lpTotalNumberOfBytes, // Bytes available on target disk / path + out long lpTotalNumberOfFreeBytes // Total available space on target disk / path + ); + } "@ - } # Setup variables as object which we can use to reference data into $ShareFree = New-Object -TypeName long; @@ -39,7 +37,7 @@ function Get-IcingaUNCPathSize() # Create a pointer object to our share [System.IntPtr]$ptrPath = [System.Runtime.InteropServices.Marshal]::StringToHGlobalAuto($Path); - # Call our function we registered within the Add-Type definition + # Call our function we registered within the Add-IcingaAddTypeLib definition [IcingaUNCPath]::GetDiskFreeSpaceEx($ptrPath, [ref]$ShareFree, [ref]$ShareSize, [ref]$TotalFree) | Out-Null; $ShareFreePercent = 0;