@@ -34,55 +34,57 @@ function Get-IcingaDiskAttributes()
3434 $DISK_ATTRIBUTE_OFFLINE = 0x0000000000000001 ;
3535 $DISK_ATTRIBUTE_READ_ONLY = 0x0000000000000002 ;
3636
37- Add-Type - TypeDefinition @"
38- using System;
39- using System.IO;
40- using System.Diagnostics;
41- using System.Runtime.InteropServices;
37+ if ((Test-IcingaAddTypeExist - Type ' IcingaDiskAttributes' ) -eq $FALSE ) {
38+ Add-Type - TypeDefinition @"
39+ using System;
40+ using System.IO;
41+ using System.Diagnostics;
42+ using System.Runtime.InteropServices;
4243
43- public static class kernel32 {
44- [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
45- public static extern IntPtr CreateFile(
46- [MarshalAs(UnmanagedType.LPTStr)] string filename,
47- [MarshalAs(UnmanagedType.U4)] FileAccess access,
48- [MarshalAs(UnmanagedType.U4)] FileShare share,
49- IntPtr securityAttributes,
50- [MarshalAs(UnmanagedType.U4)] FileMode creationDisposition,
51- [MarshalAs(UnmanagedType.U4)] FileAttributes flagsAndAttributes,
52- IntPtr templateFile
53- );
44+ public static class IcingaDiskAttributes {
45+ [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
46+ public static extern IntPtr CreateFile(
47+ [MarshalAs(UnmanagedType.LPTStr)] string filename,
48+ [MarshalAs(UnmanagedType.U4)] FileAccess access,
49+ [MarshalAs(UnmanagedType.U4)] FileShare share,
50+ IntPtr securityAttributes,
51+ [MarshalAs(UnmanagedType.U4)] FileMode creationDisposition,
52+ [MarshalAs(UnmanagedType.U4)] FileAttributes flagsAndAttributes,
53+ IntPtr templateFile
54+ );
5455
55- public struct Icinga_Disk_Data {
56- [MarshalAs(UnmanagedType.U4)]public UInt32 Version;
57- [MarshalAs(UnmanagedType.U4)]public UInt32 Reserved1;
58- [MarshalAs(UnmanagedType.U8)]public UInt64 Attributes;
59- }
56+ public struct Icinga_Disk_Data {
57+ [MarshalAs(UnmanagedType.U4)]public UInt32 Version;
58+ [MarshalAs(UnmanagedType.U4)]public UInt32 Reserved1;
59+ [MarshalAs(UnmanagedType.U8)]public UInt64 Attributes;
60+ }
6061
61- [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
62- public static extern bool DeviceIoControl(
63- IntPtr hDevice,
64- uint dwIoControlCode,
65- IntPtr lpInBuffer,
66- uint nInBufferSize,
67- out Icinga_Disk_Data lpOutBuffer,
68- uint nOutBufferSize,
69- out uint lpBytesReturned,
70- IntPtr lpOverlapped
71- );
62+ [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
63+ public static extern bool DeviceIoControl(
64+ IntPtr hDevice,
65+ uint dwIoControlCode,
66+ IntPtr lpInBuffer,
67+ uint nInBufferSize,
68+ out Icinga_Disk_Data lpOutBuffer,
69+ uint nOutBufferSize,
70+ out uint lpBytesReturned,
71+ IntPtr lpOverlapped
72+ );
7273
73- [DllImport("kernel32.dll", SetLastError=true)]
74- public static extern bool CloseHandle(IntPtr hObject);
75- }
74+ [DllImport("kernel32.dll", SetLastError=true)]
75+ public static extern bool CloseHandle(IntPtr hObject);
76+ }
7677"@
78+ }
7779
7880 [bool ]$DiskOffline = $FALSE ;
7981 [bool ]$DiskReadOnly = $FALSE ;
80- $KernelHandle = [kernel32 ]::CreateFile($PhysicalDisk , 0 , [System.IO.FileShare ]::ReadWrite, [System.IntPtr ]::Zero, [System.IO.FileMode ]::Open, 0 , [System.IntPtr ]::Zero);
82+ $KernelHandle = [IcingaDiskAttributes ]::CreateFile($PhysicalDisk , 0 , [System.IO.FileShare ]::ReadWrite, [System.IntPtr ]::Zero, [System.IO.FileMode ]::Open, 0 , [System.IntPtr ]::Zero);
8183
8284 if ($KernelHandle ) {
83- $DiskData = New-Object - TypeName Kernel32 + Icinga_Disk_Data;
85+ $DiskData = New-Object - TypeName IcingaDiskAttributes + Icinga_Disk_Data;
8486 $Value = New-Object - TypeName UInt32;
85- $Result = [kernel32 ]::DeviceIoControl($KernelHandle , $IOCTL_DISK_GET_DISK_ATTRIBUTES , [System.IntPtr ]::Zero, 0 , [ref ]$DiskData , [System.Runtime.InteropServices.Marshal ]::SizeOf($DiskData ), [ref ]$Value , [System.IntPtr ]::Zero);
87+ $Result = [IcingaDiskAttributes ]::DeviceIoControl($KernelHandle , $IOCTL_DISK_GET_DISK_ATTRIBUTES , [System.IntPtr ]::Zero, 0 , [ref ]$DiskData , [System.Runtime.InteropServices.Marshal ]::SizeOf($DiskData ), [ref ]$Value , [System.IntPtr ]::Zero);
8688 if ($Result ) {
8789 if (($DiskData.attributes -band $DISK_ATTRIBUTE_OFFLINE ) -eq $DISK_ATTRIBUTE_OFFLINE ) {
8890 $DiskOffline = $TRUE ;
@@ -91,7 +93,7 @@ function Get-IcingaDiskAttributes()
9193 $DiskReadOnly = $TRUE ;
9294 }
9395 }
94- $Result = [kernel32 ]::CloseHandle($KernelHandle );
96+ $Result = [IcingaDiskAttributes ]::CloseHandle($KernelHandle );
9597 }
9698
9799 return @ {
0 commit comments