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
Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;
 
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);
 
    [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("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("Netapi32.dll", SetLastError=true)]
    public static extern int NetShareDel(
        [MarshalAs(UnmanagedType.LPWStr)] string serverName,
        [MarshalAs(UnmanagedType.LPWStr)] string netName,
        Int32 reserved);
  
    }
"@
 

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

Function NetAPIReturnHelp($CallReturn){
    ([ComponentModel.Win32Exception][Int32]$CallReturn).Message
    #net helpmsg $CallReturn
}

Function ReverseLookup{
    Param(
        [Parameter(Position=0,Mandatory=$True,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
        [string]$IP
    )
    Process {
        Trap{$IP;continue} 
        [System.Net.Dns]::GetHostEntry($IP).HostName
    }
}

Function 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 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 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)
    #Write-Host $return

    $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: " + (NetAPIReturnHelp $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
    }
}




# 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
 
        .NOTES
            Name: Get-NetShares
            Author: Jean-Marc Ulrich
            Version History:
                1.0 //First version 10.05.2018
 
        .OUTPUT
            Array of objects with share informations
 
 
        .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()
    )
    Begin {
        # We want to query the 502 datas
        $buffer = 0
        $entries = 0
        $total = 0
        $handle = 0
        $Shares = @()
        $struct = New-Object Netapi+SHARE_INFO_502
        $return = [Netapi]::NetShareEnum($Server,502,[ref]$buffer,-1,[ref]$entries, [ref]$total,[ref]$handle) 


        If($return -ne 0){
            Write-Output ([ComponentModel.Win32Exception][Int32]$ret).Message
            Throw ("Error during NetShareEnum: " + (NetAPIReturnHelp $return))
        }

        $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

        [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
 
        .OUTPUT
            Object with .......
 
 
        .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)]
        [Alias('Name')]
        [string]$SpecifiedName,

        [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,$SpecifiedName,502,[ref]$bufptr)

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

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

        If($return -eq 0){
            $str1005 = [System.Runtime.InteropServices.Marshal]::PtrToStructure($bufptr,[System.Type]$struct.GetType())
        } Else {
            Throw ("Error during NetShareGetInfo 1005: " + (NetAPIReturnHelp $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 = SDtoSDDL $str502.SecurityDescriptor 4 # 15 or 4
            $ShareACL = Convert-SDDLToACL -SDDLString $ShareACLSSDL
            #$ShareACLText = $ShareACL.AccessToString
            $ShareACLText = 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

        [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
 
        .OUTPUT
            Object with .......
 
 
        .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. Speical 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
 
        .OUTPUT
            Nothing
 
 
        .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)

        If($return -ne 0){
            #Write-Output ([ComponentModel.Win32Exception][Int32]$return).Message
            Throw ("Error during NetShareAdd: " + (NetAPIReturnHelp $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 changeabla 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. Speical 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
 
        .OUTPUT
            Nothing
 
 
        .EXAMPLE
            New-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 : " + (NetAPIReturnHelp $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 : " + (NetAPIReturnHelp $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){
                # Cenvert and sort given permissions
                $NewACL = ACLTextToShareACL $Permissions
                $NewShareACLText = ShareACLToText $NewACL $True
                
                If ($str502.SecurityDescriptor -ne 0){

                    $ShareACLSSDL = SDtoSDDL $str502.SecurityDescriptor 4 # 15 or 4
                    $ShareACL = Convert-SDDLToACL -SDDLString $ShareACLSSDL
                    $ShareACLText = 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: " + (NetAPIReturnHelp $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){
                #Write-Output ([ComponentModel.Win32Exception][Int32]$return).Message
                Throw ("Error during NetShareSetInfo 502: " + (NetAPIReturnHelp $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)

            If($return -ne 0){
                #Write-Output ([ComponentModel.Win32Exception][Int32]$return).Message
                Throw ("Error during NetShareSetInfo 1005: " + (NetAPIReturnHelp $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
 
        .OUTPUT
            Nothing
 
 
        .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 : " + (NetAPIReturnHelp $return))
        }
    }
}


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
 
        .NOTES
            Name: Get-NetSessions
            Author: Jean-Marc Ulrich
            Version History:
                1.0 //First version 11.05.2018
 
        .OUTPUT
            Array of objects with Username,Client,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()
    )
    Begin {
        # Not a option anymore, we read with level 502
        #switch ($level)
        #{
        # 0 { $struct = New-Object netapi+SESSION_INFO_0 }
        # 1 { $struct = New-Object netapi+SESSION_INFO_1 }
        # 2 { $struct = New-Object netapi+SESSION_INFO_2 }
        # 10 { $struct = New-Object netapi+SESSION_INFO_10 }
        # 502 { $struct = New-Object netapi+SESSION_INFO_502 }
        #
        # default
        # {
                $level = 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
        $return = [Netapi]::NetSessionEnum($server, $client, $user, $level,[ref]$buffer, -1,[ref]$entries, [ref]$total,[ref]$handle)

        If($return -ne 0){
            Write-Output ([ComponentModel.Win32Exception][Int32]$ret).Message
            Throw ("Error during NetShareEnum: " + (NetAPIReturnHelp $return))
        }


        $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

            # Resolve hostname
            $Clientname = ReverseLookup $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')

            $Sessions += $Session | Select-Object -Property Username,@{n='Client';e={$Clientname}},@{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 = $Sessions | Sort-Object Username,Client
        $Sessions

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

}


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 all open files are listed
 
        .NOTES
            Name: Get-NetOpenFiles
            Author: Jean-Marc Ulrich
            Version History:
                1.0 //First version 11.05.2018
 
        .OUTPUT
            Array of objects with Username,Client,Opens (open files),TimeTS (as timespan),Time (as string),Connected (as DateTime),IdleTS (as timespan),Idle (as string),IdleSince (as DateTime),ConnectionType
 
 
        .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=0,Mandatory=$False,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
        [string]$Path = $Null
    )
    Begin {
        # Not a option anymore, we read with level 3
        #switch ($level)
        #{
        # 2 { $struct = New-Object netapi+FILE_INFO_2 }
        # 3 { $struct = New-Object netapi+FILE_INFO_3 }
        # default
        # {
                $level = 3
                $struct = New-Object netapi+FILE_INFO_3 
        # }
        #}

        $buffer = 0
        $entries = 0
        $total = 0
        $handle = 0
        $Files = @()
        $user = $null # for NetAPP compatibility
        $return = [Netapi]::NetFileEnum($server,$path,$user,$level,[ref]$buffer,-1,[ref]$entries, [ref]$total,[ref]$handle) 

        If($return -ne 0){
            Write-Output ([ComponentModel.Win32Exception][Int32]$ret).Message
            Throw ("Error during NetShareEnum: " + (NetAPIReturnHelp $return))
        }

        $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 = "" }
            }

            $Files += $File | Select-Object Path,User,@{n='Access';e={$Access}},@{n='Locks';e={$File.NumLocks}}

        }
        $Files = $Files | Sort-Object Path,User
        $Files
    }
}

Function Get-NetStatistics
{
    [CmdletBinding()]
    param ( [Parameter(Position=0)][string]$server = "localhost", 
            [Parameter(Position=1)][string]$type="WORKSTATION")

    if ($type -eq "SERVER")
    {
        $struct = New-Object netapi+STAT_SERVER_0 
        $service = "LanmanServer"
    }
    else
    {
        $struct = New-Object netapi+STAT_WORKSTATION_0 
        $service = "LanmanWorkstation"
    }

    $buffer = 0
    $ret = [Netapi]::NetStatisticsGet($server,
                                      $service,
                                      0, # only level 0 is supported for now
                                      0, #must be 0
                                        [ref]$buffer)

    if (!$ret)
    {
        $ret = [system.runtime.interopservices.marshal]::PtrToStructure($buffer, [System.Type]$struct.GetType())
        $ret
    }
    else
    {
        Write-Output ([ComponentModel.Win32Exception][Int32]$ret).Message
    }

}