首页 > 解决方案 > 尝试将获取的 Guid 添加到获取的 SID 到 Powershell 中的远程注册表项到多个远程笔记本电脑

问题描述

我正在尝试将我从 AD 获得的 Guid 添加到许多远程用户。我从注册表中的 Profilelist 中获得了 Guid 和 SID,但是每次我尝试完成它时,我都会得到不正确的路径,并且我找不到更好的方法来将它告诉 Google。

我在 Powershell 中有以下内容:

$user = Read-Host -Prompt 'Input the user name'
$guid = Get-ADUser $user -Properties * | Select ObjectGUID
$sid = (New-Object System.Security.Principal.NTAccount($user)).Translate([System.Security.Principal.SecurityIdentifier]).value
$regpath = 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\'
$profilepath = $sid
$fullpath = $regpath + $profilepath
$credential = Get-Credential -Credential doman\admin.account
Enter-PSSession -ComputerName laptopnumber -Credential $credential
New-Item -Path $path3 -Name Test -Value '14'

当我为 $fullpath 写入输出时,我得到了我期望的确切路径。但是,当我运行脚本时,出现以下错误:

New-Item : Could not find a part of the path 'C:\Users\nameofaccountIamusing\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\S-1-5-21-1810327472-1994124132-3348413286-12785\Test'.
At line:9 char:1
+ New-Item -Path $fullpath -Name Test -Value '14'

我无法解决这个问题。任何建议都非常感谢。我不明白它为什么要添加到 C:\Users\nameofaccountIamusing

标签: powershellregistry

解决方案


这里的主要问题是您忘记在路径前添加注册表提供程序,如果没有,系统将尝试在磁盘上查找路径。

代替

$regpath = 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\'

利用

$regpath = 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList'

(*)

$regpath = 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList'

我还建议使用Join-Pathcmdlet 连接部分以创建完整路径。

因为Get-ADUser默认情况下返回具有以下属性的对象:DistinguishedName, Enabled, GivenName, Name, ObjectClass, ObjectGUID, SamAccountName, SID, Surname, UserPrincipalName,因此无需先获取ObjectGUID并从中找出用户 SID。

只要这样做$adUser = Get-ADUser -Filter "SamAccountName -eq '$user'" -ErrorAction SilentlyContinue,如果返回一个对象,您可以像这样获得 SID:$adUser.SID

(*) 使用 PowerShell < 7 时,Set-ItemProperty用于创建新注册表值的 cmdlet在使用短提供程序路径时无法处理该参数。但是在使用-TypeHKLM:Registry::HKEY_LOCAL_MACHINE


推荐阅读