简体   繁体   English

“重命名项目”使用新名称进行复制

[英]'Rename-Item' makes a copy with the new name

Trying to make a script to backup specific folders and then rename the GUID key for the target user under HKLM...\\ProfileLists but the rename-item command makes a copy of the key and creates a new key with the appended name though having full access 尝试制作脚本来备份特定文件夹,然后在HKLM ... \\ ProfileLists下重命名目标用户的GUID密钥,但是redirect-item命令复制了该密钥,并使用附加名称创建了新密钥,尽管已具有完整名称。访问

Tried with -force, tried with move-item instead of rename but it gives the exact same results, a new identical key as original but with an appended name 使用-force进行了尝试,尝试使用move-item而不是重命名,但是它给出了完全相同的结果,一个与原始密钥相同的新密钥,但带有附加名称

if ((Test-Path $FULLPATH)) { 
    Rename-Item $FULLPATH -NewName "$SSID.bak" -Force 
    if ($?) { 
         Write-Host "$USERNAME was SUCCESSFULLY renamed in the registry" 
    } 
} 

Expected result is to only rename the GUID-key in the registry. 预期的结果是只重命名注册表中的GUID项。 Actual result is a duplicate key with the new duplicate to have the correct appended name. 实际结果是一个重复键,并且新的重复键具有正确的附加名称。

Rename-Item : The registry key at the specified path does not exist. Rename-Item:指定路径上的注册表项不存在。 At line:9 char:5 + Rename-Item $FULLPATH -NewName "$SSID.bak" -Force + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (HKEY_LOCAL_MACH...\\folderredirect:String) [Rename-Item], ArgumentException + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.RenameItemCommand 在第9行:char:5 +重命名项$ FULLPATH -NewName“ $ SSID.bak”-强制+ ~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo:InvalidArgument:(HKEY_LOCAL_MACH ... \\ folderredirect:String)[Rename-Item],ArgumentException + FullyQualifiedErrorId:系统。 ArgumentException,Microsoft.PowerShell.Commands.RenameItemCommand

Rename-Item : Object reference not set to an instance of an object. Rename-Item:对象引用未设置为对象的实例。 At line:9 char:5 + Rename-Item $FULLPATH -NewName "$SSID.bak" -Force + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Rename-Item], NullReferenceException + FullyQualifiedErrorId : System.NullReferenceException,Microsoft.PowerShell.Commands.RenameItemCommand 在第9行:char:5 +重命名项$ FULLPATH -NewName“ $ SSID.bak”-强制+ ~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo:未指定:(:) [Rename-Item],NullReferenceException + FullyQualifiedErrorId:System.NullReferenceException,Microsoft.PowerShell.Commands .RenameItemCommand

The key does exist and I can run the same command again as proof (due to the test-path). 密钥确实存在,我可以再次运行相同的命令作为证明(由于测试路径)。

Verbose output do confirm its copying 详细输出确实确认其复制

VERBOSE: Performing the operation "Copy Key" on target "the key in question"

It works for me. 这个对我有用。 Are your variables set in this way? 是否以这种方式设置变量?

$SSID = 'S-1-5-21-1212123708-1212126490-1212120831-1001'
$FULLPATH = 'HKLM:\Software\Microsoft\Windows NT\CurrentVersion\ProfileList\S-1-5-21-1212123708-1212126490-1212120831-1001'
rename-item $fullpath -newname "$SSID.bak"

I was unable to rename the keys with rename-item with full paths and not using variables either. 我无法使用具有完整路径的rename-item重命名键,也无法使用变量。 However resolved it by replacing rename-item to 但是通过将重命名项替换为

# Rename the Registry profile if ((Test-Path $FULLPATH)) { reg copy "HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList\\$SSID" "HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList\\$SSID-$(Get-Date -f yyy-MM-DD)" /s /f if (( $? -eq 'True' )) { reg delete "HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList\\$SSID" /f } if (( $? -eq 'True' )) { Write-Host "$USERNAME was SUCCESSFULLY renamed in the registry" }

had to use half the full path in the reg copy/delete commands as the syntax is different (no colon). 由于语法不同(无冒号),必须在reg copy / delete命令中使用完整路径的一半。 This worked 这工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM