Skip to content

Commit

Permalink
Fixes Add-Type exceptions for runtime compiling
Browse files Browse the repository at this point in the history
  • Loading branch information
LordHepipud committed Feb 9, 2022
1 parent 326d381 commit 64ceb81
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 29 deletions.
8 changes: 8 additions & 0 deletions doc/31-Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion icinga-powershell-plugins.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
18 changes: 8 additions & 10 deletions provider/disks/Get-IcingaDiskAttributes.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
34 changes: 16 additions & 18 deletions provider/disks/Get-IcingaUNCPathSize.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down

0 comments on commit 64ceb81

Please sign in to comment.