Skip to content

Commit dd0192d

Browse files
committed
Update Microsoft Active Directory.ps1
- Expanded ADSI Error logging further to include full error message instead of just error code
1 parent 713b396 commit dd0192d

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

Microsoft Active Directory.ps1

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,31 @@
2323
Add-Type -TypeDefinition @"
2424
using System;
2525
using System.Runtime.InteropServices;
26+
using System.Text;
2627
2728
public class AdsErrorHelper {
2829
[DllImport("ActiveDS.dll", CharSet = CharSet.Unicode)]
2930
public static extern int ADsGetLastError(out int error, System.Text.StringBuilder errorBuf, int errorBufLen, System.Text.StringBuilder nameBuf, int nameBufLen);
3031
}
32+
33+
public class Win32ErrorHelper {
34+
[DllImport("kernel32.dll", SetLastError = true)]
35+
public static extern int FormatMessage(
36+
int flags,
37+
IntPtr source,
38+
int messageId,
39+
int languageId,
40+
StringBuilder buffer,
41+
int size,
42+
IntPtr arguments);
43+
44+
public static string GetMessage(int errorCode) {
45+
StringBuilder buffer = new StringBuilder(1024);
46+
int flags = 0x00001000; // FORMAT_MESSAGE_FROM_SYSTEM
47+
int result = FormatMessage(flags, IntPtr.Zero, errorCode, 0, buffer, buffer.Capacity, IntPtr.Zero);
48+
return buffer.ToString();
49+
}
50+
}
3151
"@
3252

3353
function Get-ADSIError {
@@ -37,8 +57,10 @@ function Get-ADSIError {
3757

3858
[AdsErrorHelper]::ADsGetLastError([ref]$errorCode, $errorBuf, 1024, $nameBuf, 1024) | Out-Null
3959
log error "ErrorCode: $($errorCode) - ErrorMessage $($errorBuf.ToString())"
60+
log error "Win32 Description: $([Win32ErrorHelper]::GetMessage($ErrorCode))"
4061
}
4162

63+
4264
#
4365
# Helper functions
4466
#

0 commit comments

Comments
 (0)