我正在PowerShell中使用非托管的Windows API RegLoadKey来脱机装载注册表配置单元。
这很好,在PowerShell之外,一切都如预期一样工作——一旦安装,我就可以在regedit.exe中浏览到它们,并对它们进行操作等等
但是,如果我使用Get-ChildItem,我就不能再卸载配置单元了。RegUnloadKey返回一个值5;访问被拒绝";。如果我只是不从PowerShell 中触摸注册表项,则不会发生这种情况
代码非常非常简单,我可以这样复制它:
Add-Type -Name LoadHive -NameSpace RegistryHelper -MemberDefinition @"
[DllImport("advapi32.dll", SetLastError = true)]
public static extern Int32 RegLoadKey(UInt32 hKey, String lpSubKey, String lpFile);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern Int32 RegUnLoadKey(UInt32 hKey, string lpSubKey);
"@
$hiveMountPoint = 2147483651 (This is the built-in constant for HKEY_USERS)
$hiveMountName = "TEMP-HIVE"
$hivePath = [Filepath to offline hive]
[RegistryHelper.LoadHive]::RegLoadKey($hiveMountPoint, $hiveMountName, $hivePath)
$hiveKeys = Get-ChildItem "REGISTRY::HKEY_USERSTEMP-HIVE"
[RegistryHelper.LoadHive]::RegUnLoadKey($hiveMountPoint, $hiveMountName)
我正在使用RegUnLoadKey进行卸载。有人能解释一下为什么Get-ChildItem把我锁在这里吗?
Get-ChildItem
与底层提供程序(FileSystem、Registry…(无关。因此,即使使用Interop
加载配置单元,Powershell也不会意识到这一点,并且Registry Provider
将返回RegistryKey
对象。这些对象包含一个Handle
属性,该属性可能会阻止您正常关闭配置单元。在卸载配置单元以关闭所有句柄之前,应该对存储在$hiveKeys
中的所有密钥调用Close
方法。