powershell - 尝试将获取的 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
解决方案
这里的主要问题是您忘记在路径前添加注册表提供程序,如果没有,系统将尝试在磁盘上查找路径。
代替
$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-Path
cmdlet 连接部分以创建完整路径。
因为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在使用短提供程序路径时无法处理该参数。但是在使用-Type
HKLM:
Registry::HKEY_LOCAL_MACHINE
推荐阅读
- sapui5 - 如何使用 UI5 Tooling 仅生成 Component-preload.js
- regex - 在 windows cmder 中使用 sed 替换特殊字符....并使用 url 参数出现奇怪的错误
- javascript - Javascript中带有许多小数的数字的奇怪行为
- python - Python:在终端中运行另一个软件的命令
- javascript - Swift / Camera 错误 com.apple.tccd 沙盒扩展
- javascript - 想要以 1 秒的精度减少剩余时间
- amazon-web-services - 访问 aws_eks_node_group 创建的 aws_autoscaling_group 组
- pipenv - 为什么 pipenv install -e 在包目录中创建 pyproject.toml 文件?
- postgresql - 从 PostgreSQL 数据库加载 SSIS
- javascript - 如何在使用 django 时验证 csrf 令牌在浏览器中运行良好并做出反应