我正在尝试将从上的AD获得的Guid添加到许多远程用户。我已经从注册表中的Profilelist获得了Guid和SID,但每次我尝试完成它时,我都会得到不正确的路径,我找不到更好的方法来用谷歌搜索它。
我在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_MACHINESOFTWAREMicrosoftWindows NTCurrentVersionProfileList'
$profilepath = $sid
$fullpath = $regpath + $profilepath
$credential = Get-Credential -Credential domanadmin.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:UsersnameofaccountIamusingHKEY_LOCAL_MACHINESOFTWAREMicrosoftWindows NTCurrentVersionProfileListS-1-5-21-1810327472-1994124132-3348413286-12785Test'.
At line:9 char:1
+ New-Item -Path $fullpath -Name Test -Value '14'
我解决不了这个问题。非常感谢任何建议。我不明白它为什么要添加到计数的C:\Users\name中
这里的主要问题是您忘记在路径前面添加注册表提供程序,如果没有添加,系统将尝试在磁盘上查找路径。
代替
$regpath = 'HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindows NTCurrentVersionProfileList'
使用
$regpath = 'Registry::HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindows NTCurrentVersionProfileList'
或(*(
$regpath = 'HKLM:SOFTWAREMicrosoftWindows NTCurrentVersionProfileList'
我还建议使用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在使用短HKLM:
提供程序路径时无法处理-Type
参数。。但是,当使用Registry::HKEY_LOCAL_MACHINE
时,该确实有效