Situation
I want the value of an audit policy in Windows, e.g., 0cce9240-69ae-11d9-bed3-50505450303
→ Success
or 1
.
Hereby, I have to use PowerShell.
As the auditpol
is language dependent, I was looking for a language independent method.
Thus, I came across this helpful article to use the Win32.Advapi32
module.
Code
$MemberDefinition = @'
[DllImport("advapi32.dll", SetLastError = true)]
public static extern bool AuditEnumerateCategories(
out IntPtr ppAuditCategoriesArray,
out uint pCountReturned);
[DllImport("advapi32.dll", SetLastError = true)]
public static extern bool AuditLookupCategoryName(
ref Guid pAuditCategoryGuid,
out StringBuilder ppszCategoryName);
[DllImport("advapi32.dll", SetLastError = true)]
public static extern bool AuditEnumerateSubCategories(
ref Guid pAuditCategoryGuid,
bool bRetrieveAllSubCategories,
out IntPtr ppAuditSubCategoriesArray,
out uint pCountReturned);
[DllImport("advapi32.dll", SetLastError = true)]
public static extern bool AuditLookupSubCategoryName(
ref Guid pAuditSubCategoryGuid,
out StringBuilder ppszSubCategoryName);
[DllImport("advapi32.dll")]
public static extern void AuditFree(
IntPtr buffer);
[DllImport("advapi32.dll", SetLastError = true)]
public static extern bool AuditQuerySystemPolicy(
Guid pSubCategoryGuids,
uint PolicyCount,
out IntPtr ppAuditPolicy);
'@
$Advapi32 = Add-Type -MemberDefinition $MemberDefinition -Name 'Advapi32' -Namespace 'Win32' -UsingNamespace System.Text -PassThru
$neBuild = [System.Text.StringBuilder]::new()
$res1 = [Win32.Advapi32]::AuditLookupSubCategoryName([ref]"0cce9240-69ae-11d9-bed3-505054503030",[ref]$neBuild)
Write-Host "$neBuild"
[IntPtr]$test_out = [IntPtr]::Zero
$result = [Win32.Advapi32]::AuditQuerySystemPolicy("0cce9240-69ae-11d9-bed3-505054503030", 1,[ref]$test_out)
Problem
On the one hand, the output of the Write-Host
statement is Kerberos Service Ticket Operations
and, therefore, I'm assuming that the general import is working.
On the other hand, $test_out
is always 0, no matter to which value I configure the Kerberos Service Ticket Operations
.
Question
Did I do anything wrong here?
Is the import, e.g. public static extern bool AuditQuerySystemPolicy
, flawed?
Do I have to initialize the passed parameters differently?
Thank you for any help!