NTFSSecurity.Init.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#region Internals
#region C# Code
$type_NTFS1 = @'
 using System;
 using System.IO;
 using System.Collections;
 using System.Runtime.InteropServices;
 using Microsoft.Win32.SafeHandles;
  
 namespace NTFS
 {
  public class DriveInfoExt
  {
   [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
   static extern bool GetDiskFreeSpace(string lpRootPathName,
    out uint lpSectorsPerCluster,
    out uint lpBytesPerSector,
    out uint lpNumberOfFreeClusters,
    out uint lpTotalNumberOfClusters);
  
   DriveInfo _drive = null;
   uint _sectorsPerCluster = 0;
   uint _bytesPerSector = 0;
   uint _numberOfFreeClusters = 0;
   uint _totalNumberOfClusters = 0;
  
   public uint SectorsPerCluster { get { return _sectorsPerCluster; } }
   public uint BytesPerSector { get { return _bytesPerSector; } }
   public uint NumberOfFreeClusters { get { return _numberOfFreeClusters; } }
   public uint TotalNumberOfClusters { get { return _totalNumberOfClusters; } }
   public DriveInfo Drive { get { return _drive; } }
   public string DriveName { get { return _drive.Name; } }
   public string VolumeName { get { return _drive.VolumeLabel; } }
  
   public DriveInfoExt(string DriveName)
   {
    _drive = new DriveInfo(DriveName);
  
    GetDiskFreeSpace(_drive.Name,
     out _sectorsPerCluster,
     out _bytesPerSector,
     out _numberOfFreeClusters,
     out _totalNumberOfClusters);
   }
  }
    
  public class FileInfoExt
  {
   [DllImport("kernel32.dll", SetLastError = true, EntryPoint = "GetCompressedFileSize")]
   static extern uint GetCompressedFileSize(string lpFileName, out uint lpFileSizeHigh);
  
   public static ulong GetCompressedFileSize(string filename)
   {
    uint high;
    uint low;
    low = GetCompressedFileSize(filename, out high);
    int error = Marshal.GetLastWin32Error();
  
    if (high == 0 && low == 0xFFFFFFFF && error != 0)
    {
     throw new System.ComponentModel.Win32Exception(error);
    }
    else
    {
     return ((ulong)high << 32) + low;
    }
   }
  }
 }
'@

#endregion
#endregion

Add-Type -TypeDefinition $type_NTFS1
Add-Type -Path $PSScriptRoot\Security2.dll
Add-Type -Path $PSScriptRoot\PrivilegeControl.dll -ReferencedAssemblies $PSScriptRoot\ProcessPrivileges.dll
Add-Type -Path $PSScriptRoot\ProcessPrivileges.dll

#using Update-FormatData and not FormatsToProcess in the PSD1 as FormatsToProcess does not offer
#putting format data in front of the default data. This is required to make the new formatter the default ones.
Update-FormatData -PrependPath $PSScriptRoot\NTFSSecurity.format.ps1xml