FileShareUtils.psm1

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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
# PowerShell Module FileShareUtils from Jean-Marc Ulrich
# https://github.com/CamFlyerCH/FileShareUtils
#
# Large parts of the used code where posted by
# Micky Balladelli micky@balladelli.com on https://balladelli.com/category/smb/
# Alexander in his Kazun PowerShell blog https://kazunposh.wordpress.com/
# Jordan Borean https://gist.github.com/jborean93/f60da33b08f8e1d5e0ef545b0a4698a0

# Code to access the netapi32, kernel32 and advapi32 functions
Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;
using System.IO;
using System.Security.AccessControl;
 
public class Netapi
{
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    public struct SHARE_INFO_0
    {
        [MarshalAs(UnmanagedType.LPWStr)] public String Name;
    }
 
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    public struct SHARE_INFO_1
    {
        [MarshalAs(UnmanagedType.LPWStr)] public string Name;
        public uint Type;
        [MarshalAs(UnmanagedType.LPWStr)] public string Remark;
    }
 
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    public struct SHARE_INFO_2
    {
        [MarshalAs(UnmanagedType.LPWStr)] public string Name;
        public uint Type;
        [MarshalAs(UnmanagedType.LPWStr)] public string Remark;
        public uint Permissions;
        public uint MaxUses;
        public uint CurrentUses;
        [MarshalAs(UnmanagedType.LPWStr)] public string Path;
        [MarshalAs(UnmanagedType.LPWStr)] public string Password;
   }
 
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    public struct SHARE_INFO_502
    {
        [MarshalAs(UnmanagedType.LPWStr)] public string Name;
        public uint Type;
        [MarshalAs(UnmanagedType.LPWStr)] public string Remark;
        public int Permissions;
        public int MaxUses;
        public int CurrentUses;
        [MarshalAs(UnmanagedType.LPWStr)] public string Path;
        [MarshalAs(UnmanagedType.LPWStr)] public string Password;
        public int Reserved;
        public IntPtr SecurityDescriptor;
    }
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    public struct SHARE_INFO_503
    {
        [MarshalAs(UnmanagedType.LPWStr)] public string Name;
        public uint Type;
        [MarshalAs(UnmanagedType.LPWStr)] public string Remark;
        public uint Permissions;
        public uint MaxUses;
        public uint CurrentUses;
        [MarshalAs(UnmanagedType.LPWStr)] public string Path;
        [MarshalAs(UnmanagedType.LPWStr)] public string Password;
        [MarshalAs(UnmanagedType.LPWStr)] public string ServerName;
        public uint Reserved;
        public IntPtr SecurityDescriptor;
    }
 
    [DllImport("Netapi32.dll",CharSet=CharSet.Unicode)]
    public static extern uint NetShareEnum(
        [In,MarshalAs(UnmanagedType.LPWStr)] string server,
        int level,
        out IntPtr bufptr,
        int prefmaxlen,
        ref Int32 entriesread,
        ref Int32 totalentries,
        ref Int32 resume_handle);
 
    [DllImport("Netapi32.dll",CharSet=CharSet.Unicode)]
    public static extern int NetApiBufferFree(IntPtr buffer);
 
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    public struct STAT_SERVER_0
    {
      public uint Start;
      public uint FOpens;
      public uint DevOpens;
      public uint JobsQueued;
      public uint SOpens;
      public uint STimedOut;
      public uint SerrorOut;
      public uint PWerrors;
      public uint PermErrors;
      public uint SysRrrors;
      public uint bytesSent_low;
      public uint bytesSent_high;
      public uint bytesRcvd_low;
      public uint BytesRcvd_high;
      public uint AvResponse;
      public uint ReqNufNeed;
      public uint BigBufNeed;
    }
 
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    public struct STAT_WORKSTATION_0
    {
      public long StatisticsStartTime;
      public long BytesReceived;
      public long SmbsReceived;
      public long PagingReadBytesRequested;
      public long NonPagingReadBytesRequested;
      public long CacheReadBytesRequested;
      public long NetworkReadBytesRequested;
      public long BytesTransmitted;
      public long SmbsTransmitted;
      public long PagingWriteBytesRequested;
      public long NonPagingWriteBytesRequested;
      public long CacheWriteBytesRequested;
      public long NetworkWriteBytesRequested;
      public uint InitiallyFailedOperations;
      public uint FailedCompletionOperations;
      public uint ReadOperations;
      public uint RandomReadOperations;
      public uint ReadSmbs;
      public uint LargeReadSmbs;
      public uint SmallReadSmbs;
      public uint WriteOperations;
      public uint RandomWriteOperations;
      public uint WriteSmbs;
      public uint LargeWriteSmbs;
      public uint SmallWriteSmbs;
      public uint RawReadsDenied;
      public uint RawWritesDenied;
      public uint NetworkErrors;
      public uint Sessions;
      public uint FailedSessions;
      public uint Reconnects;
      public uint CoreConnects;
      public uint Lanman20Connects;
      public uint Lanman21Connects;
      public uint LanmanNtConnects;
      public uint ServerDisconnects;
      public uint HungSessions;
      public uint UseCount;
      public uint FailedUseCount;
      public uint CurrentCommands;
    }
 
    [DllImport("Netapi32.dll",CharSet=CharSet.Unicode)]
    public static extern uint NetStatisticsGet(
        [In,MarshalAs(UnmanagedType.LPWStr)] string server,
        [In,MarshalAs(UnmanagedType.LPWStr)] string service,
        int level,
        int options,
        out IntPtr bufptr);
 
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    public struct SESSION_INFO_0
    {
        [MarshalAs(UnmanagedType.LPWStr)] public string Name;
    }
 
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    public struct SESSION_INFO_1
    {
        [MarshalAs(UnmanagedType.LPWStr)] public string Name;
        [MarshalAs(UnmanagedType.LPWStr)] public string Username;
        public uint NumOpens;
        public uint Time;
        public uint IdleTime;
        public uint UserFlags;
    }
 
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    public struct SESSION_INFO_2
    {
        [MarshalAs(UnmanagedType.LPWStr)] public string Name;
        [MarshalAs(UnmanagedType.LPWStr)] public string Username;
        public uint NumOpens;
        public uint Time;
        public uint IdleTime;
        public uint UserFlags;
        [MarshalAs(UnmanagedType.LPWStr)] public string ConnectionType;
    }
 
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    public struct SESSION_INFO_10
    {
        [MarshalAs(UnmanagedType.LPWStr)] public string Name;
        [MarshalAs(UnmanagedType.LPWStr)] public string Username;
        public uint Time;
        public uint IdleTime;
    }
 
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    public struct SESSION_INFO_502
    {
        [MarshalAs(UnmanagedType.LPWStr)] public string Name;
        [MarshalAs(UnmanagedType.LPWStr)] public string Username;
        public uint NumOpens;
        public uint Time;
        public uint IdleTime;
        public uint UserFlags;
        [MarshalAs(UnmanagedType.LPWStr)] public string ConnectionType;
        [MarshalAs(UnmanagedType.LPWStr)] public string Transport;
    }
 
    [DllImport("Netapi32.dll",CharSet=CharSet.Unicode)]
    public static extern uint NetSessionEnum(
        [In,MarshalAs(UnmanagedType.LPWStr)] string server,
        int client,
        int user,
        int level,
        out IntPtr bufptr,
        int prefmaxlen,
        ref Int32 entriesread,
        ref Int32 totalentries,
        ref Int32 resume_handle);
 
    [DllImport("Netapi32.dll", SetLastError=true)]
    public static extern int NetSessionDel(
        [MarshalAs(UnmanagedType.LPWStr)] string serverName,
        [MarshalAs(UnmanagedType.LPWStr)] string UncClientName,
        [MarshalAs(UnmanagedType.LPWStr)] string username);
 
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    public struct FILE_INFO_2
    {
        public uint FileID;
    }
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    public struct FILE_INFO_3
    {
        public uint FileID;
        public uint Permissions;
        public uint NumLocks;
        [MarshalAs(UnmanagedType.LPWStr)] public string Path;
        [MarshalAs(UnmanagedType.LPWStr)] public string User;
    }
 
    [DllImport("Netapi32.dll",CharSet=CharSet.Unicode)]
    public static extern uint NetFileEnum(
        [In,MarshalAs(UnmanagedType.LPWStr)] string server,
        [In,MarshalAs(UnmanagedType.LPWStr)] string path,
        int user,
        int level,
        out IntPtr bufptr,
        int prefmaxlen,
        ref Int32 entriesread,
        ref Int32 totalentries,
        ref Int32 resume_handle);
 
    [DllImport("Netapi32.dll", CharSet = CharSet.Unicode)]
    public extern static int NetFileClose(string servername, uint fileid);
 
    [DllImport("advapi32.dll", CharSet = CharSet.Unicode)]
    public static extern bool GetSecurityDescriptorDacl(
        IntPtr pSecurityDescriptor,
        [MarshalAs(UnmanagedType.Bool)] out bool bDaclPresent,
        ref IntPtr pDacl,
        [MarshalAs(UnmanagedType.Bool)] out bool bDaclDefaulted
    );
 
    [DllImport("advapi32.dll", CharSet = CharSet.Unicode)]
    public static extern bool GetAclInformation(
        IntPtr pAcl,
        ref ACL_SIZE_INFORMATION pAclInformation,
        uint nAclInformationLength,
        ACL_INFORMATION_CLASS dwAclInformationClass
    );
 
    [DllImport("advapi32.dll", CharSet = CharSet.Unicode)]
    public static extern int GetAce(
        IntPtr aclPtr,
        int aceIndex,
        out IntPtr acePtr
    );
 
    [DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    public static extern int GetLengthSid(
        IntPtr pSID
    );
 
    [DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool ConvertSidToStringSid(
        [MarshalAs(UnmanagedType.LPArray)] byte[] pSID,
        out IntPtr ptrSid
    );
 
    [DllImport("advapi32.dll", CharSet = CharSet.Unicode)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool IsValidSecurityDescriptor(
        IntPtr pSecurityDescriptor
    );
 
    [DllImport("advapi32.dll", CharSet = CharSet.Unicode)]
    public static extern bool ConvertSecurityDescriptorToStringSecurityDescriptor(
        IntPtr pSecurityDescriptor,
        int SDRevision,
        int SecurityInfo,
        out IntPtr StringSDL,
        out int StringLength
    );
 
    [DllImport("advapi32.dll", CharSet = CharSet.Unicode)]
    public static extern int ConvertStringSecurityDescriptorToSecurityDescriptor(
        IntPtr StringSDL,
        int SDRevision,
        out IntPtr pSecurityDescriptor,
        out int pSecurityDescriptorSize
    );
 
 
    [StructLayout(LayoutKind.Sequential)]
    public struct ACL_SIZE_INFORMATION
    {
        public uint AceCount;
        public uint AclBytesInUse;
        public uint AclBytesFree;
    }
 
    [StructLayout(LayoutKind.Sequential)]
    public struct ACE_HEADER
    {
        public byte AceType;
        public byte AceFlags;
        public short AceSize;
    }
 
    [StructLayout(LayoutKind.Sequential)]
    public struct ACCESS_ALLOWED_ACE
    {
        public ACE_HEADER Header;
        public int Mask;
        public int SidStart;
    }
 
    public enum ACL_INFORMATION_CLASS
    {
        AclRevisionInformation = 1,
        AclSizeInformation
    }
 
    public enum CacheType : uint
    {
        Manual = 0x00,
        Documents = 0x10,
        Programs = 0x20,
        None = 0x30,
    }
 
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    public struct SHARE_INFO_1005
    {
        public uint shi1005_flags;
    }
 
    public const int FLAGS_ACCESS_BASED_DIRECTORY_ENUM = 0x0800;
 
    public enum Share_Type : uint
    {
        STYPE_DISKTREE = 0x00000000, // Disk Drive
        STYPE_PRINTQ = 0x00000001, // Print Queue
        STYPE_DEVICE = 0x00000002, // Communications Device
        STYPE_IPC = 0x00000003, // InterProcess Communications
        STYPE_SPECIAL = 0x80000000, // Special share types (C$, ADMIN$, IPC$, etc)
        STYPE_TEMPORARY = 0x40000000 // Temporary share
    }
 
    public enum LogicalShareRights : uint
    {
        FullControl = 0x001f01ff,
        Read = 0x001200a9,
        Change = 0x001301bf
    }
 
    [DllImport("Netapi32.dll", SetLastError=true)]
    public static extern int NetShareGetInfo(
        [MarshalAs(UnmanagedType.LPWStr)] string serverName,
        [MarshalAs(UnmanagedType.LPWStr)] string netName,
        Int32 level,
        out IntPtr bufPtr );
 
    [DllImport("Netapi32.dll", CharSet = CharSet.Unicode)]
    public static extern int NetShareSetInfo(
        [MarshalAs(UnmanagedType.LPWStr)] string serverName,
        [MarshalAs(UnmanagedType.LPWStr)] string netName,
        Int32 level,
        IntPtr buf,
        IntPtr parm_err );
 
    [DllImport("Netapi32.dll", CharSet = CharSet.Unicode)]
    public static extern int NetShareAdd(
        [MarshalAs(UnmanagedType.LPWStr)] string serverName,
        Int32 level,
        IntPtr buf,
        IntPtr parm_err );
 
    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool GetDiskFreeSpaceEx(string lpDirectoryName,
        out ulong lpFreeBytesAvailable,
        out ulong lpTotalNumberOfBytes,
        out ulong lpTotalNumberOfFreeBytes);
 
    [DllImport("Netapi32.dll", SetLastError=true)]
    public static extern int NetShareDel(
        [MarshalAs(UnmanagedType.LPWStr)] string serverName,
        [MarshalAs(UnmanagedType.LPWStr)] string netName,
        Int32 reserved);
 
}
 
namespace Win32
{
    public class NativeHelpers
    {
        [StructLayout(LayoutKind.Sequential)]
        public struct IO_STATUS_BLOCK
        {
            public UInt32 Status;
            public UInt32 Information;
        }
 
        [StructLayout(LayoutKind.Sequential)]
        public struct NT_Trans_Data
        {
            public UInt32 NumberOfSnapShots;
            public UInt32 NumberOfSnapShotsReturned;
            public UInt32 SnapShotArraySize;
            // Omit SnapShotMultiSZ because we manually get that string based on the struct results
        }
    }
 
    public class NativeMethods
    {
        [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
        public static extern SafeFileHandle CreateFileW(
            string lpFileName,
            FileSystemRights dwDesiredAccess,
            FileShare dwShareMode,
            IntPtr lpSecurityAttributes,
            FileMode dwCreationDisposition,
            UInt32 dwFlagsAndAttributes,
            IntPtr hTemplateFile);
 
        [DllImport("ntdll.dll", SetLastError = true, CharSet = CharSet.Unicode)]
        public static extern UInt32 NtFsControlFile(
            SafeFileHandle hDevice,
            IntPtr Event,
            IntPtr ApcRoutine,
            IntPtr ApcContext,
            ref NativeHelpers.IO_STATUS_BLOCK IoStatusBlock,
            UInt32 FsControlCode,
            IntPtr InputBuffer,
            UInt32 InputBufferLength,
            IntPtr OutputBuffer,
            UInt32 OutputBufferLength);
 
        [DllImport("ntdll.dll")]
        public static extern UInt32 RtlNtStatusToDosError(
            UInt32 Status);
    }
}
 
"@


# Helper - Functions ===========================================================================

Function Get-LastWin32ExceptionMessage {
    <#
        .SYNOPSIS
            Converts a Win32 Status Code to a more descriptive error message.
 
        .PARAMETER ErrorCode
            The Win32 Error Code to convert
 
        .NOTES
            Name: Get-LastWin32ExceptionMessage
            Author: Jordan Borean (@jborean93) <jborean93@gmail.com>
 
        .EXAMPLE
            $LastError = [System.Runtime.InteropServices.Marshal]::GetLastWin32Error()
            Get-LastWin32Exception -ErrorCode $LastError
    #>

    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [System.Int32]
        $ErrorCode
    )

    $Exp = New-Object -TypeName System.ComponentModel.Win32Exception -ArgumentList $ErrorCode
    $ExpMsg = "{0} (Win32 ErrorCode {1} - 0x{1:X8})" -f $Exp.Message, $ErrorCode
    return $ExpMsg
}

Function Get-DNSGet-DNSReverseLookup{
    Param(
        [Parameter(Position=0,Mandatory=$True,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
        [string]$IP
    )
    Process {
        IF($IP.Contains(".")){
            Trap{$IP;continue}
            [System.Net.Dns]::GetHostEntry($IP).HostName
        } Else {
            $IP
        }
    }
}

Function Convert-ShareACLToText($ShareACL,$SortACL){
    #Seperators in the String
    $InACLseperator = ","
    $InACEseperator = "|"
    $ShareACLText = ""

    $ShareACLArray = $ShareACL.Access

    If($ShareACLArray.Count -ne 0){
        If($SortACL){
            $ShareACLArray = $ShareACLArray | Sort-Object -Property @{Expression="AccessControlType";Descending=$true}, @{Expression="IdentityReference";Descending=$false}
        }

        # Build string
        ForEach($ShareACE in $ShareACLArray){
            $AccessRight = $ShareACE.FileSystemRights
            If($AccessRight -eq "ReadAndExecute, Synchronize") {$AccessRight = "Read"}
            If($AccessRight -eq "Modify, Synchronize") {$AccessRight = "Change"}
            If($ShareACE.AccessControlType -ne "Allow") {$AccessRight = "Deny-" + $AccessRight}

            $ShareACLText += ($ShareACE.IdentityReference.Value + $InACEseperator + $AccessRight + $InACLseperator)
        }

        # Cut last seperator
        $Return = ($ShareACLText.SubString(0,$ShareACLText.Length - 1))
    } Else {$Return = ""}
    $Return
}

Function Convert-ACLTextToShareACL($UnsortedACLText){
    #Seperators in the String
    $InACLseperator = ","
    $InACEseperator = "|"
    $NewACLObject = New-Object -TypeName System.Security.AccessControl.DirectorySecurity

    $ACETexts = $UnsortedACLText -split $InACLseperator

    ForEach($ACEText in $ACETexts){
        If($ACEText){
            $OutputIdentityReference = $ACEText.substring(0,$ACEText.indexof($InACEseperator))
            $OutputFileSystemRights = $ACEText.substring($ACEText.indexof($InACEseperator)+$InACEseperator.length,$ACEText.length-($ACEText.indexof($InACEseperator)+$InACEseperator.length))

            If($OutputFileSystemRights.substring(0,4) -eq "Deny"){
                $OutputFileSystemRights = "FullControl"
                $OutputAccessControlType = "Deny"
            } Else {
                $OutputAccessControlType = "Allow"
            }

            If($OutputFileSystemRights -eq "Read") {$OutputFileSystemRights = "ReadAndExecute, Synchronize"}
            If($OutputFileSystemRights -eq "Change") {$OutputFileSystemRights = "Modify, Synchronize"}

            $rule = New-Object System.Security.AccessControl.FileSystemAccessRule($OutputIdentityReference, $OutputFileSystemRights, "None", "None", $OutputAccessControlType)
            Try{
                $NewACLObject.AddAccessRule($rule)
            } Catch {
                $ErrorMessage = $error[0].ToString()
                Throw ("Error for Identity $OutputIdentityReference : " + $ErrorMessage)
            }
        }
    }
    $NewACLObject
}

Function Convert-SDtoSDDL($ptr2SD,$SECURITY_INFORMATION){
    #OWNER_SECURITY_INFORMATION 1 der Eigentümer wird konvertiert
    #GROUP_SECURITY_INFORMATION 2 die primäre Gruppe wird konvertiert
    #DACL_SECURITY_INFORMATION 4 die DACL Zugriffskontrollliste wird konvertiert
    #SACL_SECURITY_INFORMATION 8 die System Zugriffskontrollliste wird konvertiert
    #LABEL_SECURITY_INFORMATION 16 die Mandantory Zugriffskontrolleinträge werden konvertiert
    #$SECURITY_INFORMATION = 4
    $SDDL_REVISION_1 = 1

    $return = [Netapi]::IsValidSecurityDescriptor($ptr2SD)

    $sddlptr = [IntPtr]::Zero
    $sddllength = [IntPtr]::Zero
    $return = [Netapi]::ConvertSecurityDescriptorToStringSecurityDescriptor($ptr2SD,$SDDL_REVISION_1,$SECURITY_INFORMATION,[ref]$sddlptr,[ref]$sddllength)
    If($return -ne $True){
        Throw ("Error during ConvertSecurityDescriptorToStringSecurityDescriptor: " + (Get-LastWin32ExceptionMessage $return))
    }
    $sddl = [System.Runtime.Interopservices.Marshal]::PtrToStringAuto($sddlptr)
    $sddl
}

Function Convert-SDDLToACL {
    <#
        .Synopsis
            Convert SDDL String to ACL Object
        .DESCRIPTION
            Converts one or more SDDL Strings to a standard powershell format.
        .EXAMPLE
            Convert-SDDLToACL -SDDLString (get-acl .\path).sddl
        .EXAMPLE
            Convert-SDDLToACL -SDDLString “O:S-1-5-21-1559760989-2529464504-629046386-3226G:DUD:(A;OICIID;FA;;;SY)(A;OICIID;FA;;;BA)(A;OICIID;FA;;;S-1-5-21-1557760989-2587764504-629046386-1166)”
        .NOTES
            Robert Amartinesei
            https://poshscripter.wordpress.com/2017/04/27/sddl-conversion-with-powershell/
    #>

    [Cmdletbinding()]
    param (
        #One or more strings of SDDL syntax.
        [string[]]$SDDLString
    )
    foreach ($SDDL in $SDDLString) {
        $ACLObject = New-Object -TypeName System.Security.AccessControl.DirectorySecurity
        $ACLObject.SetSecurityDescriptorSddlForm($SDDL)
        $ACLObject
    }
}

Function Invoke-EnumerateSnapshots {
    <#
        .SYNOPSIS
            Invokes NtFsControlFile with the handle and buffer size specified.
 
        .DESCRIPTION
            This cmdlet is defined to invoke NtFsControlFile with the
            FSCTL_SRV_ENUMERATE_SNAPSHOTS control code.
 
        .PARAMETER Handle
            A SafeFileHandle of the opened UNC path. This should be retrieved with
            CreateFileW.
 
        .PARAMETER BufferSize
            The buffer size to initialise the output buffer. This should be a minimum
            of ([System.Runtime.InteropServices.Marshal]::SizeOf([Type][Win32.NativeHelpers+NT_Trans_Data]) + 4).
            See Examples on how to invoke this
 
        .PARAMETER ScriptBlock
            The script block to invoke after the raw output buffer is converted to the
            NT_Trans_Data structure.
 
        .NOTES
            Name: Invoke-EnumerateSnapshots
            Author: Jordan Borean (@jborean93) <jborean93@gmail.com>
            OUTPUT : Array of strings with the full snapshot paths
 
        .EXAMPLE
            $BufferSize = [System.Runtime.InteropServices.Marshal]::SizeOf([Type][Win32.NativeHelpers+NT_Trans_Data]) + 4)
            Invoke-EnumerateSnapshots -Handle $Handle -BufferSize $BufferSize -ScriptBlock {
                $TransactionData = $args[1]
 
                if ($TransactionData.NumberOfSnapShots -gt 0) {
                    $NewBufferSize = $BufferSize + $TransactionData.SnapShotArraySize
 
                    Invoke-EnumerateSnapshots -Handle $Handle -BufferSize $NewBufferSize -ScriptBlock {
                        $OutBuffer = $args[0]
                        $TransactionData = $args[1]
 
                        $SnapshotPtr = [System.IntPtr]::Add($OutBuffer, $TransDataSize)
                        $SnapshotString = [System.Runtime.InteropServices.Marshal]::PtrToStringUni($SnapshotPtr,
                            $TransactionData.SnapShotArraySize / 2)
 
                        $SnapshotString.Split([char[]]@("`0"), [System.StringSplitOptions]::RemoveEmptyEntries)
                    }
                }
            }
    #>

    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [Microsoft.Win32.SafeHandles.SafeFileHandle]
        $Handle,

        [Parameter(Mandatory = $true)]
        [System.Int32]
        $BufferSize,

        [Parameter(Mandatory = $true)]
        [ScriptBlock]
        $ScriptBlock
    )

    # Allocate new memory based on the buffer size
    $OutBuffer = [System.Runtime.InteropServices.Marshal]::AllocHGlobal($BufferSize)
    try {
        $IOBlock = New-Object -TypeName Win32.NativeHelpers+IO_STATUS_BLOCK

        # Call NtFsControlFile with the handle and FSCTL_SRV_ENUMERATE_SNAPSHOTS code
        $Result = [Win32.NativeMethods]::NtFsControlFile($Handle, [System.IntPtr]::Zero, [System.IntPtr]::Zero,
            [System.IntPtr]::Zero, [Ref]$IOBlock, 0x00144064, [System.IntPtr]::Zero, 0, $OutBuffer, $BufferSize)

        if ($Result -ne 0) {
            # If the result was not 0 we need to convert the NTSTATUS code to a Win32 code
            $Win32Error = [Win32.NativeMethods]::RtlNtStatusToDosError($Result)
            $Msg = Get-LastWin32ExceptionMessage -ErrorCode $Win32Error
            Write-Error -Message "NtFsControlFile failed - $Msg"
            return
        }

        # Convert the OutBuffer pointer to a NT_Trans_Data structure
        $TransactionData = [System.Runtime.InteropServices.Marshal]::PtrToStructure(
            $OutBuffer,
            [Type][Win32.NativeHelpers+NT_Trans_Data]
        )

        # Invoke out script block that parses the data and outputs whatever it needs. We pass in both the
        # OutBuffer and TransactionData as arguments
        &$ScriptBlock $OutBuffer $TransactionData
    } finally {
        # Make sure we free the unmanaged memory
        [System.Runtime.InteropServices.Marshal]::FreeHGlobal($OutBuffer)
    }
}



# Module - Functions =============================================================================



Function Get-NetShares{
    <#
        .SYNOPSIS
            Retrieves a list of all network shares from a local or remote computer
 
        .DESCRIPTION
            Retrieves basic parameters of all shares from a local or remote computer
            by using the NetAPI32.dll (without WMI)
 
        .PARAMETER Server
            Computername on the network, local if left blank
 
        .PARAMETER Level
            NetShareEnum info level. Defaults 502, but you can use 1 for listing shares without admin rights
 
        .NOTES
            Name: Get-NetShares
            Author: Jean-Marc Ulrich
            Version History:
                1.0 //First version 10.05.2018
                1.1 //Changed to support servers with more than ~250 shares
                1.2 //Added level parameter to list shares without admin rights
 
        .EXAMPLE
            Get-NetShares -Server 'srv1234'
 
            Description
            -----------
            Gets the information of all shares of a remote server
    #>

    [CmdletBinding()]
    Param (
        [Parameter(Position=0,Mandatory=$False,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
        [string]$Server = ($env:computername).toLower(),

        [Parameter(Position=1,Mandatory=$False,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
        [ValidateSet(502,1)]
        [Int]$Level = 502
    )
    Begin {
        # We want to query the 502 datas
        $buffer = 0
        $entries = 0
        $total = 0
        $handle = 0
        $Shares = @()
        If($Level -eq 1){
            $struct = New-Object Netapi+SHARE_INFO_1
        } else {
            $struct = New-Object Netapi+SHARE_INFO_502
        }
        $ReadFinished = $False

        While($ReadFinished -eq $False){
            $return = [Netapi]::NetShareEnum($Server, $level,[ref]$buffer,-1,[ref]$entries, [ref]$total,[ref]$handle)

            If($return -ne 0 -and $return -ne 234){
               Throw ("Error during NetShareEnum: " + (Get-LastWin32ExceptionMessage $return))
            }

            If($entries -eq $total -and $return -eq 0){$ReadFinished = $True}

            $offset = $buffer.ToInt64()
            $increment = [System.Runtime.Interopservices.Marshal]::SizeOf([System.Type]$struct.GetType())

            For ($i = 0; $i -lt $entries; $i++)
            {
                # Define output object
                $Share = New-Object -TypeName PSObject
                $ptr = New-Object system.Intptr -ArgumentList $offset

                $str502 = [system.runtime.interopservices.marshal]::PtrToStructure($ptr, [System.Type]$struct.GetType())
                $offset = $ptr.ToInt64()
                $offset += $increment

                # Get the easy data
                $Share | Add-Member Server $Server
                $Share | Add-Member Name $str502.Name
                $Share | Add-Member Path $str502.Path
                $Share | Add-Member Description $str502.Remark
                $Share | Add-Member CurrentUses $str502.CurrentUses

                # Get the type
                If (($str502.Type -band 0x00000001) -ne 0 -and ($str502.Type -band 0x00000002) -ne 0){
                    $Type = "IPC"
                } Else {
                    If ($str502.Type -band 0x00000001){
                        $Type = "Print Queue"
                    } Else {
                        If ($str502.Type -band 0x00000002){
                            $Type = "Device"
                        } Else {
                            $Type = "Disk Drive"
                        }
                    }
                }
                If (($str502.Type -band 0x40000000) -ne 0){
                    $Type = $Type + " Temporary"
                }
                If (($str502.Type -band 0x80000000) -ne 0){
                    $Type = $Type + " Special"
                }
                $Share | Add-Member Type $Type

                $Shares += $Share
            }
        }

        $Shares | Sort-Object Path,Name

        # Cleanup memory
        [Netapi]::NetApiBufferFree($buffer) | Out-Null
    }
}


Function Get-NetShare{
    <#
        .SYNOPSIS
            Retrieves specified network share information from a local or remote computer
 
        .DESCRIPTION
            Retrieves all parameters and permissions form one defined network share
            by using the NetAPI32.dll (without WMI)
 
        .PARAMETER Name
            Name of the share
 
        .PARAMETER Server
            Computername on the network, local if left blank
 
        .NOTES
            Name: Get-NetShare
            Author: Jean-Marc Ulrich
            Version History:
                1.0 //First version 10.05.2018
 
        .EXAMPLE
            Get-NetShare -Name 'TestShare' -Server 'srv1234'
 
            Description
            -----------
            Gets the information of the share named "TestShare" of a remote server
    #>

    [CmdletBinding()]
    Param (
        [Parameter(Position=0,Mandatory=$True,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
        [string]$Name,

        [Parameter(Position=1,Mandatory=$False,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
        [string]$Server = ($env:computername).toLower()
    )
    Begin {
        # We want to query the 502 data first
        $bufptr = [IntPtr]::Zero
        $struct = New-Object Netapi+SHARE_INFO_502
        $return = [Netapi]::NetShareGetInfo($Server,$Name,502,[ref]$bufptr)

        If($return -eq 0){
            $str502 = [System.Runtime.InteropServices.Marshal]::PtrToStructure($bufptr,[System.Type]$struct.GetType())
        } Else {
            Throw ("Error during NetShareGetInfo for $Name : " + (Get-LastWin32ExceptionMessage $return))
        }

        # Now read the flags
        $bufptr = [IntPtr]::Zero
        $struct = New-Object Netapi+SHARE_INFO_1005
        $return = [Netapi]::NetShareGetInfo($Server,$Name,1005,[ref]$bufptr)

        If($return -eq 0){
            $str1005 = [System.Runtime.InteropServices.Marshal]::PtrToStructure($bufptr,[System.Type]$struct.GetType())
        } Else {
            Throw ("Error during NetShareGetInfo 1005: " + (Get-LastWin32ExceptionMessage $return))
        }

        # Define output object
        $Share = New-Object -TypeName PSObject

        # Get the easy data
        $Share | Add-Member Server $Server
        $Share | Add-Member Name $str502.Name
        $Share | Add-Member Path $str502.Path
        $Share | Add-Member Description $str502.Remark

        # Get infos contained in the flags
        $ShareFlags = $str1005.shi1005_flags

        If ($ShareFlags -band 0x0800){
            $Share | Add-Member ABE "Enabled"
        } Else {
            $Share | Add-Member ABE "Disabled"
        }

        # Offline (client side caching) configuration
        $Share | Add-Member CachingMode ([enum]::GetValues([Netapi+CacheType]) | Where-Object {$_.value__ -eq ($ShareFlags -band 0x0030)})

        # Prepare to read ACL
        If ($str502.SecurityDescriptor -ne 0){

            #$ShareACL = SDtoACL $str502.SecurityDescriptor
            #$ShareACLText = ACLArrayToACLText $ShareACL $True
            $ShareACLSSDL = Convert-SDtoSDDL $str502.SecurityDescriptor 4 # 15 or 4
            $ShareACL = Convert-SDDLToACL -SDDLString $ShareACLSSDL
            #$ShareACLText = $ShareACL.AccessToString
            $ShareACLText = Convert-ShareACLToText $ShareACL $True

        } # EndIf SD present


        $Share | Add-Member ShareACLText $ShareACLText

        $Share | Add-Member CurrentUses $str502.CurrentUses
        $Share | Add-Member ConcurrentUserLimit $str502.MaxUses

        If ($ShareFlags -band 0x2000){
            $Share | Add-Member BranchCache "Enabled"
        } Else {
            $Share | Add-Member BranchCache "Disabled"
        }

        $Share | Add-Member Flags $ShareFlags

        # Get the type
        Switch ($str502.Type){
            0x00000000    {$Share | Add-Member Type "Disk Drive"}
            0x00000001    {$Share | Add-Member Type "Print Queue"}
            0x00000002    {$Share | Add-Member Type "Device"}
            0x00000003    {$Share | Add-Member Type "IPC"}
            0x80000000    {$Share | Add-Member Type "Special"}
            0x40000000    {$Share | Add-Member Type "Temporary"}
        }

        $Share | Add-Member ShareSDDL $ShareACLSSDL

        $Share | Add-Member ShareACL $ShareACL

        # Cleanup memory
        [Netapi]::NetApiBufferFree($bufptr) | Out-Null

        $Share

    }
}

Function Get-NetFileShares{
    <#
        .SYNOPSIS
            Retrieves a list of all file shares from a local or remote computer with permissions and flags
 
        .DESCRIPTION
            Retrieves basic parameters of all shares from a local or remote computer
            by using the NetAPI32.dll (without WMI)
 
        .PARAMETER Server
            Computername on the network, local if left blank
 
        .NOTES
            Name: Get-NetFileShares
            Author: Jean-Marc Ulrich
            Version History:
                1.0 //First version 10.05.2018
 
        .EXAMPLE
            Get-NetFileShares -Server 'srv1234'
 
            Description
            -----------
            Gets the information of all shares of a remote server
    #>

    [CmdletBinding()]
    Param (
        [Parameter(Position=0,Mandatory=$False,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
        [string]$Server = ($env:computername).toLower()
    )
    Begin {
        $FileShareList = Get-NetShares -Server $Server | Sort-Object Server,Path,Name

        ForEach ($FileShare in $FileShareList){
            IF ($FileShare.Type -eq "Disk Drive"){
                Get-NetShare -Name $FileShare.Name -Server $FileShare.Server | Select-Object -Property Server,Name,Path,Description,ABE,CachingMode,ShareACLText,CurrentUses
            }
        }
    }
}


Function New-NetShare{
    <#
        .SYNOPSIS
            Creates a network file share local or on a remote computer
 
        .DESCRIPTION
            Creates a network share
            by using the NetAPI32.dll (without WMI)
 
        .PARAMETER Server
            Computername on the network, local if left blank
 
        .PARAMETER Name
            Name of the share
 
        .PARAMETER Path
            Local disk path to share
 
        .PARAMETER Description
            The description/remark of the share
 
        .PARAMETER Permissions
            Permissions on the share itself. Special format: Every permission is seperated by a comma and the identity and the access right are seperated by a |
            Default: Everyone|FullControl
            Possible Permissions: Read, Change, FullControl, Deny-FullControl
            Possible Identities: Everyone, BUILTIN\Administrators, BUILTIN\Users, BUILTIN\xxxxx (server local users or groups), DOMAIN\UserName, ADCORP\GroupName, <NETBIOSDOMAINNAME>\<sAMAccountName> (domain objects)
 
        .PARAMETER ABE
            Access based enumaration, can be Enabled or Disabled (default)
 
        .PARAMETER CachingMode
            Offline Folder configuration, can be Manual (default), "None", "Documents" (all documents are automaticaly offline available), "Programs" ("Performance option", all files are automaticaly offline available)
 
        .PARAMETER MaxUses
            Allowed connections to the share. Default is -1 that equals maximum
 
 
        .NOTES
            Name: New-NetShare
            Author: Jean-Marc Ulrich
            Version History:
                1.0 //First version 17.05.2018
 
        .EXAMPLE
            New-NetShare -Server 'srv1234' -Name 'TestShare' -Path 'D:\Data'
 
            Description
            -----------
            Shares the path D:\Data on the server named srv1234 as TestShare
    #>

    [CmdletBinding()]
    Param (
        [Parameter(Position=0,Mandatory=$False,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
        [string]$Server = ($env:computername).toLower(),

        [Parameter(Position=1,Mandatory=$True,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
        [string]$Name,

        [Parameter(Position=2,Mandatory=$True,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
        [string]$Path,

        [Parameter(Position=3,Mandatory=$False,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
        [string]$Description,

        [Parameter(Position=4,Mandatory=$False,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
        [string]$Permissions,  # = "Everyone|FullControl"

        [Parameter(Position=5,Mandatory=$False,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
        [ValidateSet("Disabled", "Enabled", IgnoreCase = $true)]  # FolderEnumerationMode AccessBased, Unrestricted
        [string]$ABE = "Disabled",

        [Parameter(Position=6,Mandatory=$False,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
        [ValidateSet("Manual", "None", "Documents", "Programs", IgnoreCase = $true)]
        [string]$CachingMode = "Manual",

        [Parameter(Position=7,Mandatory=$False,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
        [Int]$MaxUses = -1

    )
    Begin {
        $struct = New-Object Netapi+SHARE_INFO_502
        $struct.Name = $Name
        $struct.Type = 0                 # 0 -> STYPE_DISKTREE
        $struct.Path = $Path
        $struct.Remark = $Description
        $struct.Permissions = 0
        $struct.MaxUses = $MaxUses
        $paramerror = 0

        $structsize = [System.Runtime.InteropServices.Marshal]::SizeOf($struct)
        [IntPtr]$bufptr = [System.Runtime.InteropServices.Marshal]::AllocCoTaskMem($structsize)
        [System.Runtime.InteropServices.Marshal]::StructureToPtr($struct,$bufptr,$false)

        $return = [Netapi]::NetShareAdd($Server,502,$bufptr,$paramerror)

        [Netapi]::NetApiBufferFree($bufptr) | Out-Null

        If($return -ne 0){
            Throw ("Error during NetShareAdd: " + (Get-LastWin32ExceptionMessage $return))
        }

        $return = Set-NetShare -Server $Server -Name $Name -Description $Description -Permissions $Permissions -ABE $ABE -CachingMode $CachingMode -MaxUses $MaxUses

    }
}

Function Set-NetShare{
    <#
        .SYNOPSIS
            Changes options on a network file share local or on a remote computer
 
        .DESCRIPTION
            Can modify all changeable options of a file share local or on a remote computer
            by using the NetAPI32.dll (without WMI)
 
        .PARAMETER Server
            Computername on the network, local if left blank
 
        .PARAMETER Name
            Name of the share
 
        .PARAMETER Description
            The description/remark of the share
 
        .PARAMETER Permissions
            Permissions on the share itself. Special format: Every permission is seperated by a comma and the identity and the access right are seperated by a |
            Default: Everyone|FullControl
            Possible Permissions: Read, Change, FullControl, Deny-FullControl
            Possible Identities: Everyone, BUILTIN\Administrators, BUILTIN\Users, BUILTIN\xxxxx (server local users or groups), DOMAIN\UserName, ADCORP\GroupName, <NETBIOSDOMAINNAME>\<sAMAccountName> (domain objects)
 
        .PARAMETER ABE
            Access based enumaration, can be Enabled or Disabled (default)
 
        .PARAMETER CachingMode
            Offline Folder configuration, can be Manual (default), "None", "Documents" (all documents are automaticaly offline available), "Programs" ("Performance option", all files are automaticaly offline available)
 
        .PARAMETER MaxUses
            Allowed connections to the share. Default is -1 that equals maximum
 
        .NOTES
            Name: Set-NetShare
            Author: Jean-Marc Ulrich
            Version History:
                1.0 //First version 17.05.2018
 
        .EXAMPLE
            Set-NetShare -Server 'srv1234' -Name 'TestShare' -Description "A test share" -ABE Enabled -CachingMode None -MaxUses 50 -Permissions "DOMAINNAME\Domain Admins|FullControl,Everyone|Change,BUILTIN\Administrators|FullControl"
 
            Description
            -----------
            Sets the given options and permissions a server named srv1234 on the share named TestShare
    #>

    [CmdletBinding()]
    Param (
        [Parameter(Position=0,Mandatory=$False,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
        [string]$Server = ($env:computername).toLower(),

        [Parameter(Position=1,Mandatory=$True,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
        [string]$Name,

        [Parameter(Position=2,Mandatory=$False,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
        [string]$Description,

        [Parameter(Position=3,Mandatory=$False,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
        [string]$Permissions,

        [Parameter(Position=4,Mandatory=$False,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
        [ValidateSet("Disabled", "Enabled", IgnoreCase = $true)]  # FolderEnumerationMode AccessBased, Unrestricted
        [string]$ABE,

        [Parameter(Position=5,Mandatory=$False,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
        [ValidateSet("Manual", "None", "Documents", "Programs", IgnoreCase = $true)]
        [string]$CachingMode,

        [Parameter(Position=6,Mandatory=$False,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
        [Int]$MaxUses

    )
    Begin {
        # Read all existing share informations

        # We want to query the 502 data first
        $bufptr502 = [IntPtr]::Zero
        $struct502 = New-Object Netapi+SHARE_INFO_502
        $return = [Netapi]::NetShareGetInfo($Server,$Name,502,[ref]$bufptr502)

        If($return -eq 0){
            $str502 = [System.Runtime.InteropServices.Marshal]::PtrToStructure($bufptr502,[System.Type]$struct502.GetType())
        } Else {
            Throw ("Error during NetShareGetInfo for $Name on $Server : " + (Get-LastWin32ExceptionMessage $return))
        }

        # Now read the flags
        $bufptr1005 = [IntPtr]::Zero
        $struct1005 = New-Object Netapi+SHARE_INFO_1005
        $return = [Netapi]::NetShareGetInfo($Server,$Name,1005,[ref]$bufptr1005)

        If($return -eq 0){
            $str1005 = [System.Runtime.InteropServices.Marshal]::PtrToStructure($bufptr1005,[System.Type]$struct1005.GetType())
        } Else {
            Throw ("Error during NetShareGetInfo 1005 for $Name on $Server : " + (Get-LastWin32ExceptionMessage $return))
        }
        $ShareFlags = $str1005.shi1005_flags

        # Prepare for modifications
        $Write502 = $False
        $Write1005 = $False
        $paramerror = 0

        # Check for changes in the 502 structure
        If($PSBoundParameters.ContainsKey('Description')){
            If($str502.Remark -ne $Description){
                $str502.Remark = $Description
                $Write502 = $True
            }
        }

        If($PSBoundParameters.ContainsKey('MaxUses')){
            If($str502.MaxUses -ne $MaxUses){
                $str502.MaxUses = $MaxUses
                $Write502 = $True
            }
        }

        If($PSBoundParameters.ContainsKey('Permissions')){
            If($Permissions){
                # Convert and sort given permissions
                $NewACL = Convert-ACLTextToShareACL $Permissions
                $NewShareACLText = Convert-ShareACLToText $NewACL $True

                If ($str502.SecurityDescriptor -ne 0){

                    $ShareACLSSDL = Convert-SDtoSDDL $str502.SecurityDescriptor 4 # 15 or 4
                    $ShareACL = Convert-SDDLToACL -SDDLString $ShareACLSSDL
                    $ShareACLText = Convert-ShareACLToText $ShareACL $True
                }

                If($NewShareACLText -ne $ShareACLText){

                    $sddlptr = [IntPtr]::Zero
                    $sddlptr = [System.Runtime.Interopservices.Marshal]::StringToHGlobalAuto($NewACL.Sddl)

                    $SDDL_REVISION_1 = 1
                    $NewSDptr = [IntPtr]::Zero
                    $NewSDsize = [IntPtr]::Zero
                    $return = [Netapi]::ConvertStringSecurityDescriptorToSecurityDescriptor($sddlptr,$SDDL_REVISION_1,[ref]$NewSDptr,[ref]$NewSDsize)
                    If($return -ne $True){
                        Throw ("Error during ConvertStringSecurityDescriptorToSecurityDescriptor: " + (Get-LastWin32ExceptionMessage $return))
                    }

                    $str502.SecurityDescriptor = $NewSDptr
                    $Write502 = $True
                }
            }
        }


        # Write 502 data if changed
        If($Write502){
            $structsize = [System.Runtime.InteropServices.Marshal]::SizeOf($str502)
            [IntPtr]$bufptr = [System.Runtime.InteropServices.Marshal]::AllocCoTaskMem($structsize)
            [System.Runtime.InteropServices.Marshal]::StructureToPtr($str502,$bufptr,$false)

            $return = [Netapi]::NetShareSetInfo($Server,$Name,502,$bufptr,$paramerror)

            If($return -ne 0){
                Throw ("Error during NetShareSetInfo 502: " + (Get-LastWin32ExceptionMessage $return))
            }
        }


        # Check for changes in the 1005 flags
        If($PSBoundParameters.ContainsKey('ABE')){
            If($ShareFlags -band [Netapi]::FLAGS_ACCESS_BASED_DIRECTORY_ENUM -AND $ABE -eq "Disabled"){
                $ShareFlags = $ShareFlags -bxor [Netapi]::FLAGS_ACCESS_BASED_DIRECTORY_ENUM
                $Write1005 = $True
            }
            If(!($ShareFlags -band [Netapi]::FLAGS_ACCESS_BASED_DIRECTORY_ENUM) -AND $ABE -eq "Enabled"){
                $ShareFlags = $ShareFlags -bor [Netapi]::FLAGS_ACCESS_BASED_DIRECTORY_ENUM
                $Write1005 = $True
            }
        }

        If($PSBoundParameters.ContainsKey('CachingMode')){
            $NewCachingFlags = [Netapi+CacheType]::($Cachingmode).value__
            If (($ShareFlags -band 0x0030) -ne $NewCachingFlags){
                $ShareFlags = $ShareFlags -bxor ($ShareFlags -band 0x0030) -bor $NewCachingFlags
                $Write1005 = $True
            }
        }

        # Write 1005 data if changed
        If($Write1005){
            $str1005.shi1005_flags = $ShareFlags
            $structsize = [System.Runtime.InteropServices.Marshal]::SizeOf($str1005)
            [IntPtr]$bufptr = [System.Runtime.InteropServices.Marshal]::AllocCoTaskMem($structsize)
            [System.Runtime.InteropServices.Marshal]::StructureToPtr($str1005,$bufptr,$false)

            $return = [Netapi]::NetShareSetInfo($Server,$Name,1005,$bufptr,$paramerror)

            [Netapi]::NetApiBufferFree($bufptr) | Out-Null

            If($return -ne 0){
                Throw ("Error during NetShareSetInfo 1005: " + (Get-LastWin32ExceptionMessage $return))
            }
        }

        # Cleanup memory
        [Netapi]::NetApiBufferFree($bufptr502) | Out-Null
        [Netapi]::NetApiBufferFree($bufptr1005) | Out-Null

    }
}

Function Redo-NetShare{
    <#
        .SYNOPSIS
            Creates a network file share local or on a remote computer. If a share with the same name already exists it will be modified if needed
 
        .DESCRIPTION
            Creates or modify a network share
            by using the NetAPI32.dll (without WMI)
 
        .PARAMETER Server
            Computername on the network, local if left blank
 
        .PARAMETER Name
            Name of the share
 
        .PARAMETER Path
            Local disk path to share
 
        .PARAMETER Description
            The description/remark of the share
 
        .PARAMETER Permissions
            Permissions on the share itself. Special format: Every permission is seperated by a comma and the identity and the access right are seperated by a |
            Default: Everyone|FullControl
            Possible Permissions: Read, Change, FullControl, Deny-FullControl
            Possible Identities: Everyone, BUILTIN\Administrators, BUILTIN\Users, BUILTIN\xxxxx (server local users or groups), DOMAIN\UserName, ADCORP\GroupName, <NETBIOSDOMAINNAME>\<sAMAccountName> (domain objects)
 
        .PARAMETER ABE
            Access based enumaration, can be Enabled or Disabled (default)
 
        .PARAMETER CachingMode
            Offline Folder configuration, can be Manual (default), "None", "Documents" (all documents are automaticaly offline available), "Programs" ("Performance option", all files are automaticaly offline available)
 
        .PARAMETER MaxUses
            Allowed connections to the share. Default is -1 that equals maximum
 
 
        .NOTES
            Name: Redo-NetShare
            Author: Jean-Marc Ulrich
            Version History:
                1.0 //First version 01.12.2018
 
        .EXAMPLE
            Redo-NetShare -Server 'srv1234' -Name 'TestShare' -Path 'D:\Data'
 
            Description
            -----------
            Shares the path D:\Data on the server named srv1234 as TestShare. If a share with the same name on the same server exists, it will be changed or recreated
    #>

    [CmdletBinding()]
    Param (
        [Parameter(Position=0,Mandatory=$False,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
        [string]$Server = ($env:computername).toLower(),

        [Parameter(Position=1,Mandatory=$True,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
        [string]$Name,

        [Parameter(Position=2,Mandatory=$True,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
        [string]$Path,

        [Parameter(Position=3,Mandatory=$False,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
        [string]$Description,

        [Parameter(Position=4,Mandatory=$False,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
        [string]$Permissions,  # = "Everyone|FullControl"

        [Parameter(Position=5,Mandatory=$False,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
        [ValidateSet("Disabled", "Enabled", IgnoreCase = $true)]  # FolderEnumerationMode AccessBased, Unrestricted
        [string]$ABE = "Disabled",

        [Parameter(Position=6,Mandatory=$False,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
        [ValidateSet("Manual", "None", "Documents", "Programs", IgnoreCase = $true)]
        [string]$CachingMode = "Manual",

        [Parameter(Position=7,Mandatory=$False,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
        [Int]$MaxUses = -1

    )
    Begin {

        Try{
            $ExistingShare = Get-NetShare -Server $Server -Name $Name
        } Catch {
            $error.clear()
        }

        If($ExistingShare){
            # Preserve values if not changed
            If(-Not $PSBoundParameters.ContainsKey('Description')){
                $Description = $ExistingShare.Description
            }
            If(-Not $PSBoundParameters.ContainsKey('Permissions')){
                $Permissions = $ExistingShare.ShareACLText
            }
            If(-Not $PSBoundParameters.ContainsKey('ABE')){
                $ABE = $ExistingShare.ABE
            }
            If(-Not $PSBoundParameters.ContainsKey('CachingMode')){
                $CachingMode = $ExistingShare.CachingMode
            }
            If(-Not $PSBoundParameters.ContainsKey('MaxUses')){
                $MaxUses = $ExistingShare.ConcurrentUserLimit
            }

            If($ExistingShare.Path -eq $Path){
                # Just modify Settings
                $return = Set-NetShare -Server $Server -Name $Name -Description $Description -Permissions $Permissions -ABE $ABE -CachingMode $CachingMode -MaxUses $MaxUses
            } Else {
                # Path is different, so the share must be erased and recreated
                $return = Remove-NetShare -Server $Server -Name $Name
                $return = New-NetShare -Server $Server -Name $Name -Path $Path -Description $Description -Permissions $Permissions -ABE $ABE -CachingMode $CachingMode -MaxUses $MaxUses
            }

        } Else {
            $return = New-NetShare -Server $Server -Name $Name -Path $Path -Description $Description -Permissions $Permissions -ABE $ABE -CachingMode $CachingMode -MaxUses $MaxUses
        }
        $return = $return
    }
}

Function Remove-NetShare{
    <#
        .SYNOPSIS
            Deletes a network file share local or on a remote computer
 
        .DESCRIPTION
            Removes / stops a share localy or on a remote computer
            by using the NetAPI32.dll (without WMI)
 
        .PARAMETER Server
            Computername on the network, local if left blank
 
        .PARAMETER Name
            Name of the share
 
        .NOTES
            Name: Remove-NetShare
            Author: Jean-Marc Ulrich
            Version History:
                1.0 //First version 18.05.2018
 
        .EXAMPLE
            Remove-NetShare -Server 'srv1234' -Name 'TestShare'
 
            Description
            -----------
            Deletes the share named TestShare on the server named srv1234
    #>

    [CmdletBinding()]
    Param (
        [Parameter(Position=0,Mandatory=$False,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
        [string]$Server = ($env:computername).toLower(),

        [Parameter(Position=1,Mandatory=$True,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
        [string]$Name
    )
    Begin {
        $reserved = 0
        $return = [Netapi]::NetShareDel($Server,$Name,$reserved)

        If($return -ne 0){
            Throw ("Error during NetShareDel for $Name on $Server : " + (Get-LastWin32ExceptionMessage $return))
        }
    }
}


Function Get-NetShareDiskspace{
    <#
        .SYNOPSIS
            Retrieves disk space infos from the specified network share
 
        .DESCRIPTION
            Retrieves the availabe space for the calling user and the total free space on the disk and the total disk space
 
        .PARAMETER Name
            Name of the share
 
        .PARAMETER Server
            Computername on the network, local if left blank
 
        .PARAMETER Unit
            Unit of the returned sizes, if not specified the vaules are in bytes, possible units are "KB","MB","GB" or "TB"
 
        .NOTES
            Name: Get-NetShareDiskspace
            Author: Jean-Marc Ulrich
            Version History:
                1.0 //First version 01.12.2018
 
        .EXAMPLE
            Get-NetShareDiskspace -Name 'TestShare' -Server 'srv1234' -Unit GB
 
            Description
            -----------
            Gets the disk space information of the share named "TestShare" of a remote server
    #>

    [CmdletBinding()]
    Param (
        [Parameter(Position=0,Mandatory=$True,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
        [string]$Name,

        [Parameter(Position=1,Mandatory=$False,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
        [string]$Server = ($env:computername).toLower(),

        [Parameter(Position=2,Mandatory=$False,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
        [ValidateSet("KB","MB","GB","TB",IgnoreCase = $true)]
        [string]$Unit
    )
    Begin {
        $Path = '\\' + $Server + '\' + $Name

           switch($Unit){
            "KB" {$UnitDevider = 1kb;break}
            "MB" {$UnitDevider = 1mb;break}
            "GB" {$UnitDevider = 1gb;break}
            "TB" {$UnitDevider = 1tb;break}
            default {$UnitDevider = 1;break}
        }

        $UserFree = New-Object System.UInt64
        $DiskFree = New-Object System.UInt64
        $DiskSize = New-Object System.UInt64

        $result = [Netapi]::GetDiskFreeSpaceEx($Path,([ref]$UserFree),([ref]$DiskSize),([ref]$DiskFree))

        If(!$result){
            $LastError = [ComponentModel.Win32Exception][Runtime.InteropServices.Marshal]::GetLastWin32Error()
            Throw ("Error during GetDiskFreeSpaceEx for path $Path : " + $LastError)
        }

        # Define output object
        $ShareDiskInfo = New-Object -TypeName PSObject

        $ShareDiskInfo | Add-Member Server      $Server
        $ShareDiskInfo | Add-Member Name        $Name
        $ShareDiskInfo | Add-Member Path        $Path
        $ShareDiskInfo | Add-Member UserFree    ([UInt64]($UserFree/$UnitDevider))
        $ShareDiskInfo | Add-Member DiskFree    ([UInt64]($DiskFree/$UnitDevider))
        $ShareDiskInfo | Add-Member DiskSize    ([UInt64]($DiskSize/$UnitDevider))

        $ShareDiskInfo

    }
}


Function Get-NetSessions{
    <#
        .SYNOPSIS
            Retrieves all open server sessions from a local or remote computer
 
        .DESCRIPTION
            Retrieves all usefull session informations from a local or remote computer
            by using the NetAPI32.dll (without WMI)
 
        .PARAMETER Server
            Computername on the network, local if left blank
 
        .PARAMETER Level
            NetSessionEnum info level. Defaults 502, but you can use 1 for compatibility reasons
 
        .NOTES
            Name: Get-NetSessions
            Author: Jean-Marc Ulrich
            Version History:
                1.0 //First version 11.05.2018
                1.1 //Add level option 01.12.2018
 
            OUTPUT : Array of objects with Username,Client,ClientIP,Opens (open files),TimeTS (as timespan),Time (as string),Connected (as DateTime),IdleTS (as timespan),Idle (as string),IdleSince (as DateTime),ConnectionType
 
 
        .EXAMPLE
            Get-NetSessions -Server 'srv1234'
 
            Description
            -----------
            Gets the information of all shares of a remote server
    #>

    [CmdletBinding()]
    Param (
        [Parameter(Position=0,Mandatory=$False,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
        [string]$Server = ($env:computername).toLower(),

        [Parameter(Position=1,Mandatory=$False,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
        [ValidateSet(502,10,1,0)]
        [Int]$Level = 502
    )
    Begin {
        If($Level -eq 0){
            $struct = New-Object netapi+SESSION_INFO_0
        }
        If($Level -eq 1){
            $struct = New-Object netapi+SESSION_INFO_1
        }
        If($Level -eq 10){
            $struct = New-Object netapi+SESSION_INFO_10
        }
        If($Level -eq 502){
            $struct = New-Object netapi+SESSION_INFO_502
        }

        $buffer = 0
        $entries = 0
        $total = 0
        $handle = 0
        $now = Get-Date
        $Sessions = @()
        $client = $null # for NetAPP compatibility
        $user = $null # for NetAPP compatibility
        $ReadFinished = $False

        While($ReadFinished -eq $False){

            $return = [Netapi]::NetSessionEnum($server, $client, $user, $level,[ref]$buffer, -1,[ref]$entries, [ref]$total,[ref]$handle)

            If($return -ne 0 -and $return -ne 234){
                Throw ("Error during NetSessionEnum: " + (Get-LastWin32ExceptionMessage $return))
            }

            If($entries -eq $total -and $return -eq 0){$ReadFinished = $True}

            $offset = $buffer.ToInt64()
            $increment = [System.Runtime.Interopservices.Marshal]::SizeOf([System.Type]$struct.GetType())

            for ($i = 0; $i -lt $entries; $i++)
            {
                $ptr = New-Object system.Intptr -ArgumentList $offset
                $Session = [system.runtime.interopservices.marshal]::PtrToStructure($ptr, [System.Type]$struct.GetType())

                $offset = $ptr.ToInt64()
                $offset += $increment

                $ClientIP = $session.Name
                # Resolve hostname
                $Clientname = Get-DNSGet-DNSReverseLookup $Session.Name

                $TimeTS = New-TimeSpan -Seconds $Session.Time
                $Time = [String]([Int]$TimeTS.TotalHours) + ":" + [String]($TimeTS.Minutes)
                $Connected = ($Now - $TimeTS)       # .ToString('yyyy-MM-dd HH:mm')

                $IdleTS = New-TimeSpan -Seconds $Session.IdleTime
                $Idle = [String]([Int]$IdleTS.TotalHours) + ":" + [String]($IdleTS.Minutes)
                $IdleSince = ($Now - $IdleTS)       # .ToString('yyyy-MM-dd HH:mm')

                If($Level -eq 1){
                    $Sessions += $Session | Select-Object -Property Username,@{n='Client';e={$Clientname}},@{n='ClientIP';e={$ClientIP}},@{n='Opens';e={$_.NumOpens}},@{n='TimeTS';e={$TimeTS}},@{n='Time';e={$Time}},@{n='Connected';e={$Connected}},@{n='IdleTS';e={$IdleTS}},@{n='Idle';e={$Idle}},@{n='IdleSince';e={$IdleSince}}
                } else {
                    $Sessions += $Session | Select-Object -Property Username,@{n='Client';e={$Clientname}},@{n='ClientIP';e={$ClientIP}},@{n='Opens';e={$_.NumOpens}},@{n='TimeTS';e={$TimeTS}},@{n='Time';e={$Time}},@{n='Connected';e={$Connected}},@{n='IdleTS';e={$IdleTS}},@{n='Idle';e={$Idle}},@{n='IdleSince';e={$IdleSince}},ConnectionType
                }
            }
        }

        $Sessions | Sort-Object Username,Client

        # Cleanup memory
        [Netapi]::NetApiBufferFree($buffer) | Out-Null
    }

}


Function Close-NetSession{
    <#
        .SYNOPSIS
            Force-closes an open session on a server
 
        .DESCRIPTION
            Closes an open session on a local or remote computer by specifying user AND client IP
            by using the NetAPI32.dll (without WMI)
 
        .PARAMETER Server
            Computername on the network, local if left blank
 
        .PARAMETER User
            The user that sessions should be ended on the server
 
        .PARAMETER ClientIP
            The client that sessions should be ended on the server (IP Address)
 
        .NOTES
            Name: Close-NetSession
            Author: Jean-Marc Ulrich
            Version History:
                1.0 //First version 23.03.2020
 
        .EXAMPLE
            Close-NetSession -Server 'srv1234' -User 'TestUser' -ClientIP '11.22.33.44'
 
            Description
            -----------
            Closes the open session(s) of user "TestUser" comming from IP 11.22.33.44 on server srv1234
    #>

    [CmdletBinding()]
    Param (
        [Parameter(Position=0,Mandatory=$False,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
        [string]$Server = ($env:computername).toLower(),
        [Parameter(Position=1,Mandatory=$True,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
        [string]$User,
        [Parameter(Position=2,Mandatory=$True,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
        [string]$ClientIP
    )
    Begin {
        If($ClientIP){
            $ClientIPUNC = "\\" + $ClientIP
        }

        $return = [Netapi]::NetSessionDel($Server,$ClientIPUNC,$User)

        If($return -ne 0){
            Throw ("Error during NetSessionDel for user $User and client $ClientIP on $Server : " + (Get-LastWin32ExceptionMessage $return))
        }
    }
}


Function Get-NetOpenFiles{
    <#
        .SYNOPSIS
            Retrieves all open files from a local or remote computer
 
        .DESCRIPTION
            Retrieves all usefull informations about open files from a local or remote computer
            by using the NetAPI32.dll (without WMI)
 
        .PARAMETER Server
            Computername on the network, local if left blank
 
        .PARAMETER Path
            Beginning (left part) of a server local path to filter the results, if left undefined no path filter is applied
 
        .PARAMETER User
            Username to filter the results, if left undefined all open files are listed, if left undefined no user filter is applied
 
        .PARAMETER WithID
            If this switch is set a FileID will also be returned
 
        .NOTES
            Name: Get-NetOpenFiles
            Author: Jean-Marc Ulrich
            Version History:
                1.0 //First version 11.05.2018
                1.1 //Added user filter and output of the FileID 23.03.2020
 
            OUTPUT : Array of objects with Path, Username, Access Rights, Locks and optional the FileID
 
 
        .EXAMPLE
            Get-NetOpenFiles -Server 'srv1234' -Path "C:\vol_fs_cifs_srv001234_001\data\Progs"
 
            Description
            -----------
            Gets the information of open files in a specific folder
    #>

    [CmdletBinding()]
    Param (
        [Parameter(Position=0,Mandatory=$False,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
        [string]$Server = ($env:computername).toLower(),
        [Parameter(Position=1,Mandatory=$False,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
        [string]$Path = $Null,
        [Parameter(Position=2,Mandatory=$False,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
        [string]$User = $Null,
        [Parameter(Position=3,Mandatory=$False,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
        [switch]$WithID
    )
    Begin {
        $level = 3
        $struct = New-Object netapi+FILE_INFO_3

        $buffer = 0
        $entries = 0
        $total = 0
        $handle = 0
        $Files = @()
        # $user = $null # for NetAPP compatibility
        $ReadFinished = $False

        While($ReadFinished -eq $False){

            $return = [Netapi]::NetFileEnum($server,$path,$null,$level,[ref]$buffer,-1,[ref]$entries, [ref]$total,[ref]$handle)

            If($return -ne 0 -and $return -ne 234){
                Throw ("Error during NetFileEnum: " + (Get-LastWin32ExceptionMessage $return))
            }

            If($entries -eq $total -and $return -eq 0){$ReadFinished = $True}

            $offset = $buffer.ToInt64()
            $increment = [System.Runtime.Interopservices.Marshal]::SizeOf([System.Type]$struct.GetType())

            for ($i = 0; $i -lt $entries; $i++)
            {
                $ptr = New-Object system.Intptr -ArgumentList $offset
                $File = [system.runtime.interopservices.marshal]::PtrToStructure($ptr, [System.Type]$struct.GetType())

                $offset = $ptr.ToInt64()
                $offset += $increment

                Switch ($File.Permissions){
                    1 { $Access = "Read" }
                    2 { $Access = "Write" }
                    3 { $Access = "Write" }
                    4 { $Access = "Create" }

                    default { $Access = "" }
                }

                If($User){
                    If($File.User -ine $User){
                        Continue
                    }
                }

                If($WithID){
                    $Files += $File | Select-Object Path,User,@{n='Access';e={$Access}},@{n='Locks';e={$File.NumLocks}},@{n='FileID';e={$File.FileID}}
                } else {
                    $Files += $File | Select-Object Path,User,@{n='Access';e={$Access}},@{n='Locks';e={$File.NumLocks}}
                }
            }
        }

        $Files | Sort-Object Path,User

        # Cleanup memory
        [Netapi]::NetApiBufferFree($buffer) | Out-Null

    }
}


Function Close-NetOpenFiles{
    <#
        .SYNOPSIS
            Force-closes an open path or file
 
        .DESCRIPTION
            Closes an open file or folder from a local or remote computer
            by using the NetAPI32.dll (without WMI)
 
        .PARAMETER Server
            Computername on the network, local if left blank
 
        .PARAMETER FileID
            FileID to close (Get the FileID by using Get-NetOpenFiles with the option -WithID)
 
        .NOTES
            Name: Close-NetOpenFiles
            Author: Jean-Marc Ulrich
            Version History:
                1.0 //First version 23.03.2020
 
        .EXAMPLE
            Close-NetOpenFiles -Server 'srv1234' -FileID 2751
 
            Description
            -----------
            Closes the open file handle with the ID 2721 on server srv1234
    #>

    [CmdletBinding()]
    Param (
        [Parameter(Position=0,Mandatory=$False,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
        [string]$Server = ($env:computername).toLower(),
        [Parameter(Position=1,Mandatory=$True,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
        [UInt32]$FileID
    )
    Begin {
        $return = [Netapi]::NetFileClose($Server,$FileID)

        If($return -ne 0){
            Throw ("Error during NetFileClose for $FileID on $Server : " + (Get-LastWin32ExceptionMessage $return))
        }
    }
}

Function Get-SnapshotPath{
    <#
        .SYNOPSIS
            Get all VSS snapshot paths for the path specified.
 
        .DESCRIPTION
            Scans the UNC or Local path for a list of VSS snapshots and the path that
            can be used to reach these files.
 
        .PARAMETER Path
            The UNC or mapped drive path to search.
 
        .NOTES
            Name: Get-SnapshotPath
            Author: Jordan Borean (@jborean93) <jborean93@gmail.com>
            OUTPUT : Array of strings with the full snapshot paths
 
        .EXAMPLE
            Get-SnapshotPath -Path \\server\share
            Get-SnapshotPath -Path C:\Windows
    #>

    [CmdletBinding()]
    Param (
        [Parameter(Mandatory = $true)]
        [System.String]
        $Path
    )

    If (-not (Test-Path -LiteralPath $Path)) {
        Write-Error -Message "Could not find UNC path '$Path'" -Category ObjectNotFound
        return
    }

    # Create a SafeFileHandle of the path specified and make sure it is valid
    $Handle = [Win32.NativeMethods]::CreateFileW(
        $Path,
        [System.Security.AccessControl.FileSystemRights]"ListDirectory, ReadAttributes, Synchronize",
        [System.IO.FileShare]::ReadWrite,
        [System.IntPtr]::Zero,
        [System.IO.FileMode]::Open,
        0x02000000,  # FILE_FLAG_BACKUP_SEMANTICS
        [System.IntPtr]::Zero
    )
    if ($Handle.IsInvalid) {
        $LastError = [System.Runtime.InteropServices.Marshal]::GetLastWin32Error()
        $Msg = Get-LastWin32ExceptionMessage -ErrorCode $LastError
        Write-Error -Message "CreateFileW($Path) failed - $Msg"
        return
    }

    try {
        # Set the initial buffer size to the size of NT_Trans_Data + 2 chars. We do this so we can get the actual buffer
        # size that is contained in the NT_Trans_Data struct. A char is 2 bytes (UTF-16) and we expect 2 of them
        $TransDataSize = [System.Runtime.InteropServices.Marshal]::SizeOf([Type][Win32.NativeHelpers+NT_Trans_Data])
        $BufferSize = $TransDataSize + 4

        # Invoke NtFsControlFile at least once to get the number of snapshots and total size of the NT_Trans_Data
        # buffer. If there are 1 or more snapshots we invoke it again to get the actual snapshot strings
        Invoke-EnumerateSnapshots -Handle $Handle -BufferSize $BufferSize -ScriptBlock {
            $TransactionData = $args[1]

            if ($TransactionData.NumberOfSnapShots -gt 0) {
                # There are snapshots to retrieve, reset the buffer size to the original size + the return array size
                $NewBufferSize = $BufferSize + $TransactionData.SnapShotArraySize

                # Invoke NtFsControlFile with the larger buffer size but now we can parse the NT_Trans_Data
                Invoke-EnumerateSnapshots -Handle $Handle -BufferSize $NewBufferSize -ScriptBlock {
                    $OutBuffer = $args[0]
                    $TransactionData = $args[1]

                    $SnapshotPtr = [System.IntPtr]::Add($OutBuffer, $TransDataSize)
                    $SnapshotString = [System.Runtime.InteropServices.Marshal]::PtrToStringUni($SnapshotPtr,
                        $TransactionData.SnapShotArraySize / 2)

                    Write-Output -InputObject ($SnapshotString.Split([char[]]@("`0"), [System.StringSplitOptions]::RemoveEmptyEntries))
                }
            }
        } | ForEach-Object -Process { Join-Path -Path $Path -ChildPath $_ }
    } finally {
        # Technically not needed as a SafeFileHandle will auto dispose once the GC is called but it's good to be
        # explicit about these things
        $Handle.Dispose()
    }
}

Function Get-SnapshotItems{
    <#
        .SYNOPSIS
            Retrieves all previous versions of a folder or file
 
        .DESCRIPTION
            Retrieves file or folder items of previous versions of a given object including a timestamp when the snapshot was taken
 
        .PARAMETER Path
            The UNC or mapped drive path to search.
 
        .NOTES
            Name: Get-SnapshotItems
            Author: Jean-Marc Ulrich
            Version History:
                1.0 //First version 20.03.2021
 
            OUTPUT : Array of file or folder objects with additional value SnapshotCreationTime
 
 
        .EXAMPLE
            Get-SnapshotItems -Path "\\FileServer001\data\Org"
 
            Description
            -----------
            Gets a list of available snapshots of the folder
    #>

    [CmdletBinding()]
    Param (
        [Parameter(Position=0,Mandatory=$True,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
        [string]$Path
    )
    Begin {
        $SnapshotsFolders = @()

        # Check if the given path item exists now
        Try{
            $SourceItem = Get-Item -LiteralPath $Path -ErrorAction Stop
            $SearchMode = $False

            # Check if path given is a folder or a file that makes search more complicated
            IF($SourceItem.PSIsContainer){
                $FolderPath = $SourceItem.FullName
                $SourceFile = $NULL
            } Else {
                # Prepare file search
                $FolderPath = Split-Path ($SourceItem.FullName) -Parent
                $SourceFile = $SourceItem
                $LastVersionDate = $SourceItem.LastWriteTimeUtc
            }
        } Catch {
            $FolderPath = $Path
            $SearchMode = $True

            #Searching existing parent folder
            While(!$SearchPath){
                $FolderPath = Split-Path $FolderPath -Parent
                $SourceItem = Get-Item -LiteralPath $FolderPath -ErrorAction SilentlyContinue

                If($SourceItem){
                    $SearchPath = $Path.Replace($FolderPath,'')
                }
            }
        }

        # Use Get-SnapshotPath to get full path of existing Snapshots
        $SnapPaths = Get-SnapshotPath -Path $FolderPath

        # Get item for each result
        ForEach($SnapPath in $SnapPaths){
            $FolderObj = Get-Item -LiteralPath $SnapPath
            $SnapshotDateTime = [datetime]::parseexact($FolderObj.Name, '@GMT-yyyy.MM.dd-HH.mm.ss', $null)
            $SnapshotsFolders += $FolderObj | Select-Object *,@{n='SnapshotCreationTime';e={$SnapshotDateTime}}
        }

        # Search mode needs to check each snapshot for given path
        If($SearchMode){
            $TempResult = @()

            # look for search path or file in every snapshot of this share
            ForEach($SnapshotsFolder in $SnapshotsFolders){
                $TestItem = Get-Item -LiteralPath ($SnapshotsFolder.FullName + $SearchPath) -ErrorAction SilentlyContinue

                IF($TestItem){
                    IF($TestItem.PSIsContainer){
                        $TempResult += $TestItem | Select-Object *,@{n='SnapshotCreationTime';e={$SnapshotsFolder.SnapshotCreationTime}}
                    } Else {
                        $TempResult += Get-Item -LiteralPath (Split-Path ($SnapshotsFolder.FullName + $SearchPath) -Parent) -ErrorAction SilentlyContinue | Select-Object *,@{n='SnapshotCreationTime';e={$SnapshotsFolder.SnapshotCreationTime}}
                        $SourceFile = $TestItem
                    }
                }
            }
            $SnapshotsFolders = $TempResult
        }

        # If the item of intrest is or used to be a file ...
        If($SourceFile){

            # Check every file version
            ForEach($SnapshotsFolder in $SnapshotsFolders){
                Try{
                    $TestFile = Get-Item -LiteralPath ($SnapshotsFolder.FullName + "\" + $SourceFile.Name) -ErrorAction SilentlyContinue
                } Catch {
                    $error.clear()
                    Continue
                }

                # Compare Modify dates
                If($LastVersionDate -ne $TestFile.LastWriteTimeUtc){
                    $TestFile | Select-Object *,@{n='SnapshotCreationTime';e={$SnapshotsFolder.SnapshotCreationTime}}
                    $LastVersionDate = $TestFile.LastWriteTimeUtc
                }
            }

        } Else {
            $SnapshotsFolders
        }
    }
}