C# 如何在PowerShell中使用Windows API函数?
我想使用PowerShell获取当前数据。我可以使用C# 如何在PowerShell中使用Windows API函数?,c#,powershell,advapi32,C#,Powershell,Advapi32,我想使用PowerShell获取当前数据。我可以使用auditpol.exe,但它的输出因操作系统语言而异,这使得解析变得困难 存储在HKEY\U Local\U Machine\Security\Policy\PolAdtEv中的REG\U NONE值中。我可以尝试在的帮助下解析该值。但是,我首选的方法是使用advapi32.dll的Windows API函数AuditQuerySystemPolicy 在本文的帮助下,我在PowerShell中创建了一个类型,如下所示 $MemberDefi
auditpol.exe
,但它的输出因操作系统语言而异,这使得解析变得困难
存储在HKEY\U Local\U Machine\Security\Policy\PolAdtEv
中的REG\U NONE值中。我可以尝试在的帮助下解析该值。但是,我首选的方法是使用advapi32.dll
的Windows API函数AuditQuerySystemPolicy
在本文的帮助下,我在PowerShell中创建了一个类型,如下所示
$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)]
private 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
成功地创建了该类型,但第一步——获取所有审计类别的GUID——失败了。我有不同类型的问题。例如,我试过
$guid = [Guid].MakeByRefType()
$count = [IntPtr]::Zero
[Win32.Advapi32]::AuditEnumerateCategories([ref]$guid, [ref]$count)
# Exception calling "AuditEnumerateCategories" with 2 Arguments: "The value "System.Guid&" of the type "System.RuntimeType" cannot be coverted to "System.IntPtr".
我还尝试将AuditEnumerateCategories
定义输出从IntPtr
更改为Guid
。ppAuditCategoriesArray
的输出为
指向单个缓冲区的指针,该缓冲区包含指向GUID结构的指针数组和结构本身
不幸的是,我不知道如何在PowerShell中处理这个问题 使用(.NET
)。例如,将数据从非托管内存块封送到新分配的指定类型的托管对象,请参见以下注释代码段:
### (unchanged)
### $MemberDefinition = …
### $Advapi32 = Add-Type …
$guid = [System.Int64]::MinValue ### or ### [System.Int64]0
$count = [System.Int32]::MaxValue ### or ### [System.Int64]0
$aux = [Win32.Advapi32]::AuditEnumerateCategories([ref]$guid, [ref]$count)
$guidArr = @() ### array to store GUIDs
$guidPtr = [int64]$guid ### pointer to next GUID
$guidOff = [System.Runtime.InteropServices.Marshal]::SizeOf([type][guid])
$ppszCategoryName = [System.Text.StringBuilder]::new()
for ( $i=0; $i -lt $count; $i++ ) {
$guidAux = [System.Runtime.InteropServices.Marshal]::
PtrToStructure( [System.IntPtr]$guidPtr,[type][guid] )
$guidArr += $guidAux ### update array of GUIDs for future use
$guidPtr += $guidOff ### shift pointer to next GUID
### debugging: try another method of [Win32.Advapi32] Runtime Type
$foo = [Win32.Advapi32]::AuditLookupCategoryName([ref]$guidAux,[ref]$ppszCategoryName)
### debugging: show partial results
Write-Host $('{0} {1,4} {2,4}' -f $guidAux.Guid,
$ppszCategoryName.Capacity, $ppszCategoryName.Length)
}
如果
$guid=[System.Int64]0$count=[System.Int64]0
然后AuditEnumerateCategories
方法分别返回IntPtr
和Int32
值(如$MemberDefinition
中声明的)。您解决过这个问题吗,在@JosefZ的帮助下,我现在在$count
中有了正确的计数,在$guid
中有了一些数字。然而,我还没有从中获得guid。