Skip to content

Commit 64ceb81

Browse files
committed
Fixes Add-Type exceptions for runtime compiling
1 parent 326d381 commit 64ceb81

File tree

4 files changed

+33
-29
lines changed

4 files changed

+33
-29
lines changed

doc/31-Changelog.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ documentation before upgrading to a new release.
77

88
Released closed milestones can be found on [GitHub](https://github.com/Icinga/icinga-powershell-plugins/milestones?state=closed).
99

10+
## 1.9.0 (2022-05-03)
11+
12+
[Issue and PRs](https://github.com/Icinga/icinga-powershell-plugins/milestone/12?closed=1)
13+
14+
### Bugfixes
15+
16+
* [#283](https://github.com/Icinga/icinga-powershell-plugins/pull/283) Fixes random `Add-Type` exceptions during runtime compilation by replacing it with `Add-IcingaAddTypeLib`
17+
1018
## 1.8.0 (2022-02-08)
1119

1220
[Issue and PRs](https://github.com/Icinga/icinga-powershell-plugins/milestone/11?closed=1)

icinga-powershell-plugins.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
Copyright = '(c) 2021 Icinga GmbH | GPLv2'
88
Description = 'A collection of Icinga Plugins for general Windows checks for Icinga for Windows.'
99
PowerShellVersion = '4.0'
10-
RequiredModules = @(@{ModuleName = 'icinga-powershell-framework'; ModuleVersion = '1.7.0' })
10+
RequiredModules = @(@{ModuleName = 'icinga-powershell-framework'; ModuleVersion = '1.9.0' })
1111
NestedModules = @(
1212
'.\plugins\Invoke-IcingaCheckBiosSerial.psm1',
1313
'.\plugins\Invoke-IcingaCheckCertificate.psm1',

provider/disks/Get-IcingaDiskAttributes.psm1

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,15 @@ function Get-IcingaDiskAttributes()
7474
}
7575
"@
7676

77-
if ((Test-IcingaAddTypeExist 'IcingaDiskAttributes') -eq $FALSE) {
78-
try {
79-
Add-Type -TypeDefinition $IcingaDiskAttributesClass;
80-
} catch {
81-
[string]$ExErrorId = $_.FullyQualifiedErrorId;
77+
try {
78+
Add-IcingaAddTypeLib -TypeName 'IcingaDiskAttributes' -TypeDefinition $IcingaDiskAttributesClass;
79+
} catch {
80+
[string]$ExErrorId = $_.FullyQualifiedErrorId;
8281

83-
Exit-IcingaThrowCritical `
84-
-Message 'Failed to process disk related checks. Based on the error your local Windows disk partition has no space left' `
85-
-FilterString $ExErrorId `
86-
-SearchString 'System.IO.IOException';
87-
}
82+
Exit-IcingaThrowCritical `
83+
-Message 'Failed to process disk related checks. Based on the error your local Windows disk partition has no space left' `
84+
-FilterString $ExErrorId `
85+
-SearchString 'System.IO.IOException';
8886
}
8987

9088
[bool]$DiskOffline = $FALSE;

provider/disks/Get-IcingaUNCPathSize.psm1

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,22 @@ function Get-IcingaUNCPathSize()
1212
-Force;
1313
}
1414

15-
if ((Test-IcingaAddTypeExist -Type 'IcingaUNCPath') -eq $FALSE) {
16-
# Register our kernel32.dll Windows API function call
17-
Add-Type -TypeDefinition @"
18-
using System;
19-
using System.Runtime.InteropServices;
20-
21-
public static class IcingaUNCPath {
22-
[DllImport("kernel32.dll", PreserveSig = true, CharSet = CharSet.Auto)]
23-
24-
public static extern int GetDiskFreeSpaceEx(
25-
IntPtr lpDirectoryName, // UNC Path for share
26-
out long lpFreeBytesAvailable, // Free Bytes available on path
27-
out long lpTotalNumberOfBytes, // Bytes available on target disk / path
28-
out long lpTotalNumberOfFreeBytes // Total available space on target disk / path
29-
);
30-
}
15+
# Register our kernel32.dll Windows API function call
16+
Add-IcingaAddTypeLib -TypeName 'IcingaUNCPath' -TypeDefinition @"
17+
using System;
18+
using System.Runtime.InteropServices;
19+
20+
public static class IcingaUNCPath {
21+
[DllImport("kernel32.dll", PreserveSig = true, CharSet = CharSet.Auto)]
22+
23+
public static extern int GetDiskFreeSpaceEx(
24+
IntPtr lpDirectoryName, // UNC Path for share
25+
out long lpFreeBytesAvailable, // Free Bytes available on path
26+
out long lpTotalNumberOfBytes, // Bytes available on target disk / path
27+
out long lpTotalNumberOfFreeBytes // Total available space on target disk / path
28+
);
29+
}
3130
"@
32-
}
3331

3432
# Setup variables as object which we can use to reference data into
3533
$ShareFree = New-Object -TypeName long;
@@ -39,7 +37,7 @@ function Get-IcingaUNCPathSize()
3937
# Create a pointer object to our share
4038
[System.IntPtr]$ptrPath = [System.Runtime.InteropServices.Marshal]::StringToHGlobalAuto($Path);
4139

42-
# Call our function we registered within the Add-Type definition
40+
# Call our function we registered within the Add-IcingaAddTypeLib definition
4341
[IcingaUNCPath]::GetDiskFreeSpaceEx($ptrPath, [ref]$ShareFree, [ref]$ShareSize, [ref]$TotalFree) | Out-Null;
4442
$ShareFreePercent = 0;
4543

0 commit comments

Comments
 (0)