使用Powershell 5.1,我试图在以下位置创建注册表值:
HKLM:\SOFTWARE\Classes\.
(注释尾随期)
很遗憾,Set-ItemProperty似乎忽略了结尾。并在以下位置创建值:
HKLM:\SOFTWARE\Classes
代替。
我以管理员身份运行Powershell ISE,并测试了各种引号,Path和LiteralPath,反引号转义和尾随/均无效。
我尝试在不同的键下尝试以下操作,以防Classs出现任何异常...
$PSVersionTable.PSVersion
New-Item -Path 'HKLM:\SOFTWARE\aTest\.' -ItemType RegistryKey -Force -ErrorAction SilentlyContinue > $null
New-Item -Path 'HKLM:\SOFTWARE\aTest\`.' -ItemType RegistryKey -Force -ErrorAction SilentlyContinue > $null
Set-ItemProperty -LiteralPath HKLM:\SOFTWARE\aTest\. -Name 'Test01' -Value 'emptyFile' -Verbose;
Set-ItemProperty -LiteralPath 'HKLM:\SOFTWARE\aTest\.' -Name 'Test02' -Value 'emptyFile' -Verbose;
Set-ItemProperty -LiteralPath "HKLM:\SOFTWARE\aTest\." -Name 'Test03' -Value 'emptyFile' -Verbose;
Set-ItemProperty -Path HKLM:\SOFTWARE\aTest\. -Name 'Test04' -Value 'emptyFile' -Verbose;
Set-ItemProperty -Path 'HKLM:\SOFTWARE\aTest\.' -Name 'Test05' -Value 'emptyFile' -Verbose;
Set-ItemProperty -Path "HKLM:\SOFTWARE\aTest\." -Name 'Test06' -Value 'emptyFile' -Verbose;
Set-ItemProperty -LiteralPath HKLM:\SOFTWARE\aTest\`. -Name 'Test07' -Value 'emptyFile' -Verbose;
Set-ItemProperty -LiteralPath 'HKLM:\SOFTWARE\aTest\`.' -Name 'Test08' -Value 'emptyFile' -Verbose;
Set-ItemProperty -LiteralPath "HKLM:\SOFTWARE\aTest\`." -Name 'Test09' -Value 'emptyFile' -Verbose;
Set-ItemProperty -Path HKLM:\SOFTWARE\aTest\`. -Name 'Test10' -Value 'emptyFile' -Verbose;
Set-ItemProperty -Path 'HKLM:\SOFTWARE\aTest\`.' -Name 'Test11' -Value 'emptyFile' -Verbose;
Set-ItemProperty -Path "HKLM:\SOFTWARE\aTest\`." -Name 'Test12' -Value 'emptyFile' -Verbose;
Set-ItemProperty -LiteralPath HKLM:\SOFTWARE\aTest\.\ -Name 'Test13' -Value 'emptyFile' -Verbose;
Set-ItemProperty -LiteralPath 'HKLM:\SOFTWARE\aTest\.\' -Name 'Test14' -Value 'emptyFile' -Verbose;
Set-ItemProperty -LiteralPath "HKLM:\SOFTWARE\aTest\.\" -Name 'Test15' -Value 'emptyFile' -Verbose;
Set-ItemProperty -Path HKLM:\SOFTWARE\aTest\.\ -Name 'Test16' -Value 'emptyFile' -Verbose;
Set-ItemProperty -Path 'HKLM:\SOFTWARE\aTest\.\' -Name 'Test17' -Value 'emptyFile' -Verbose;
Set-ItemProperty -Path "HKLM:\SOFTWARE\aTest\.\" -Name 'Test18' -Value 'emptyFile' -Verbose;
,但输出显示它忽略。除非从字面上转义(当它创建一个带有反引号的键时...
Major Minor Build Revision
----- ----- ----- --------
5 1 17134 228
VERBOSE: Performing the operation "Set Property" on target "Item: HKEY_LOCAL_MACHINE\SOFTWARE\aTest Property: Test01".
VERBOSE: Performing the operation "Set Property" on target "Item: HKEY_LOCAL_MACHINE\SOFTWARE\aTest Property: Test02".
VERBOSE: Performing the operation "Set Property" on target "Item: HKEY_LOCAL_MACHINE\SOFTWARE\aTest Property: Test03".
VERBOSE: Performing the operation "Set Property" on target "Item: HKEY_LOCAL_MACHINE\SOFTWARE\aTest Property: Test04".
VERBOSE: Performing the operation "Set Property" on target "Item: HKEY_LOCAL_MACHINE\SOFTWARE\aTest Property: Test05".
VERBOSE: Performing the operation "Set Property" on target "Item: HKEY_LOCAL_MACHINE\SOFTWARE\aTest Property: Test06".
VERBOSE: Performing the operation "Set Property" on target "Item: HKEY_LOCAL_MACHINE\SOFTWARE\aTest Property: Test07".
VERBOSE: Performing the operation "Set Property" on target "Item: HKEY_LOCAL_MACHINE\SOFTWARE\aTest\`. Property: Test08".
VERBOSE: Performing the operation "Set Property" on target "Item: HKEY_LOCAL_MACHINE\SOFTWARE\aTest Property: Test09".
VERBOSE: Performing the operation "Set Property" on target "Item: HKEY_LOCAL_MACHINE\SOFTWARE\aTest Property: Test10".
VERBOSE: Performing the operation "Set Property" on target "Item: HKEY_LOCAL_MACHINE\SOFTWARE\aTest\`. Property: Test11".
VERBOSE: Performing the operation "Set Property" on target "Item: HKEY_LOCAL_MACHINE\SOFTWARE\aTest Property: Test12".
VERBOSE: Performing the operation "Set Property" on target "Item: HKEY_LOCAL_MACHINE\SOFTWARE\aTest\ Property: Test13".
VERBOSE: Performing the operation "Set Property" on target "Item: HKEY_LOCAL_MACHINE\SOFTWARE\aTest\ Property: Test14".
VERBOSE: Performing the operation "Set Property" on target "Item: HKEY_LOCAL_MACHINE\SOFTWARE\aTest\ Property: Test15".
VERBOSE: Performing the operation "Set Property" on target "Item: HKEY_LOCAL_MACHINE\SOFTWARE\aTest\ Property: Test16".
VERBOSE: Performing the operation "Set Property" on target "Item: HKEY_LOCAL_MACHINE\SOFTWARE\aTest\ Property: Test17".
VERBOSE: Performing the operation "Set Property" on target "Item: HKEY_LOCAL_MACHINE\SOFTWARE\aTest\ Property: Test18".
请问我想念什么?
答案 0 :(得分:0)
考虑以下示例:
PS D:\PShell> New-Item -Path 'HKLM:\SOFTWARE\Test\.' -ItemType RegistryKey -WhatIf
What if: Performing the operation "New Item"
on target "Item: HKEY_LOCAL_MACHINE\SOFTWARE\Test".
PS D:\PShell> New-Item -Path 'HKLM:\SOFTWARE\Test\..' -ItemType RegistryKey -WhatIf
What if: Performing the operation "New Item"
on target "Item: HKEY_LOCAL_MACHINE\SOFTWARE".
PS D:\PShell> (Resolve-Path -LiteralPath 'HKLM:\Software\Test\.').Path
HKLM:\Software\Test
PS D:\PShell> (Resolve-Path -LiteralPath 'HKLM:\Software\Test\..').Path
HKLM:\Software
很明显,Powershell的Registry
提供程序采用一个单独的时间段(也是两倍的时间段)用作关键路径组件,类似于类似的FileSystem
的目录组件,如https://drive.google.com/drive/folders/1LH79C0JtpBBpAMdKqNjKfYfj3KTvLgAh?usp=sharing中所述:
- 在路径中使用句点作为目录组件来表示当前目录,例如
".\temp.txt"
。了解更多信息, 参见Naming Conventions。- 使用两个连续的句点(
..
)作为路径中的目录组件,以表示当前目录的父目录"..\temp.txt"
。有关更多信息,请参见Paths。
但是,Paths(包括.
和..
键)请参见以下示例:
==> reg query "HKCU\Software\Test\."
HKEY_CURRENT_USER\Software\Test\.
value_inside_PeriodKey REG_SZ
==> reg add "HKCU\Software\Test\.."
The operation completed successfully.
==> reg query "HKCU\Software\Test"
HKEY_CURRENT_USER\Software\Test
SomeTestString REG_SZ Test String
HKEY_CURRENT_USER\Software\Test\.
HKEY_CURRENT_USER\Software\Test\..
出于明显的原因(安全性),请注意在后面的示例中使用HKCU
代替HKLM
。