我遇到了一个问题,尽管努力了。
基本上,我创建了一个powershell脚本来更改远程Windows 10 Amazon Workspace上特定用户的HKU Hive中的更改值。该脚本加载蜂巢并完美地进行更改,但是在尝试卸载蜂巢时,我会遇到错误。我尝试了各种在不同论坛上建议的方法,但无济于事。这是我遇到的脚本的一部分:
$WorkSpace = "blahComputerName"
$PSS = New-PSSession -ComputerName $WorkSpace
$UserAcc = "XXXXX"
$SID = (Get-ADUser -server MyDomain.com -Identity $UserAcc).SID.Value
Invoke-Command -Session $PSS -ArgumentList $SID, $UserAcc -ScriptBlock {
New-PSDrive -PSProvider Registry -Name HKU -Root HKEY_USERS
reg load "HKU$($args[0])" "D:Users$($args[1])NTUser.Dat"
Clear-ItemProperty -Path
"HKU:$($args[0])SOFTWAREMicrosoftOfficeCommonUserInfo" -Name
"UserInitials"
[gc]::collect()
Start-Sleep -Seconds 5
reg unload "HKU$($args[0])"
Remove-PSDrive -Name HKU
}
Remove-PSSession -Id $PSS.Id
我还读到,使用 $ someings.handle.close()将关闭任何可能导致错误的提供商,但我看不到如何看到,这可能会导致错误在这种情况下使用它。
这是确切的错误:
错误:访问被拒绝。 categoryInfo:notspecified :(错误:访问被拒绝。 完全qualififiedErrid:nativecommanderror psComputerName:blahcomputername
我已经手动观察了已加载的远程注册表,然后显然已卸载,但是这个错误使我感到担忧,并希望解决它。我已经证明了它的 reg卸载" hku $($ args [0])正在引起错误,但找不到正确的解决方案。
脚本以所需的提高特权运行,因此不是那样。远程工作区处于登录状态。
任何建议将不胜感激。谢谢
尽管垃圾收集不错,但问题源于该命令的处理方式;
Clear-ItemProperty -Path
"HKU:$($args[0])SOFTWAREMicrosoftOfficeCommonUserInfo" -Name
"UserInitials"
我建议在变量中捕获它,例如;
$n = Clear-ItemProperty -Path "HKU:$($args[0])SOFTWAREMicrosoftOfficeCommonUserInfo" -Name "UserInitials"
应该让您像这样正确清理它;
$n.dispose()
$n.close()
注意以上可能是多余的(处置应关闭,关闭应拨打)。
在我弄清楚的其他示例的帮助下。这是需要做的:
$tempHive = 'HKLMTEMP_hive'
$ntUserFile = 'C:UsersSOME_USERNTUSER.DAT'
# Load hive
$startParams = @{
FilePath = 'reg.exe'
ArgumentList = "load `"$tempHive`" `"$ntUserFile`""
WindowStyle = 'Hidden'
Wait = $true
PassThru = $true
}
$process = Start-Process @startParams
if ($process.ExitCode) {
throw "Failed to load the temp hive '$tempHive' for '$ntUserFile': exit code $($process.ExitCode)"
}
# make registry hive drive mapping if needed
# New-Psdrive -name <blah> -PSProvider Registry -root <blih>
# close open handles for 'New-Item'
$result = New-Item -Path "HKLM:TEMP_hivenewkey"
$result.Handle.Close()
# no need to close open handles from 'New-ItemProperty'
# $null = New-ItemProperty @newParams
# wait for garbage clean up
[gc]::Collect()
[gc]::WaitForPendingFinalizers()
# if you did drive mapping with the mapped registry hive remove it before unload
# Remove-PSDrive <blah>
# unload the hive
$startParams = @{
FilePath = 'reg.exe'
ArgumentList = "unload `"$tempHive`""
WindowStyle = 'Hidden'
Wait = $true
PassThru = $true
}
$process = Start-Process @startParams
if ($process.ExitCode) {
throw "Failed to unload the temp hive '$tempHive' for '$ntUserFile': exit code $($process.ExitCode)"
}