Split-Path with non standard key names

会有一股神秘感。 提交于 2019-12-25 01:18:19

问题


How does one deal with paths like this HKEY_LOCAL_MACHINE\SOFTWARE\MozillaPlugins\@microsoft.com/GENUINE when using Split-Path? That forward slash near the end gets treated just like a backslash so GENUINE is treated as a separate key, rather than @microsoft.com/GENUINE being the key. Perhaps a better question is, how does one use Get-ItemProperty in this case? I have always used Split-Path to separate the Key from the Property in a path like HKEY_LOCAL_MACHINE\SOFTWARE\Key\Property but that fails here. My hope is to be able to provide a single string that I can split up in code, and support older versions of PowerShell as well. I know the brute force approach would be to just look for the last instance of backslash, and everything to the left is the key, and to the right is the property, but I wonder if there is a more elegant option. Or, something I am missing that would make that approach not even work?

EDIT: So, to verify, I CAN get this to work

$path = 'HKLM:\SOFTWARE\MozillaPlugins\@microsoft.com/GENUINE\GeckoVersion'
$location = $path.LastIndexOf('\')
$key = $path.Substring(0,$location)
$property = $path.Substring($location+1)
$currentValue = (Get-ItemProperty -path:$key -name:$property).$property
Write-Host "$key "
Write-Host "$property"
Write-Host "$currentValue"

But, one it seems like Split-Path should handle this better (the path IS a valid path, after all). And two, I'm not sure brute forcing on the last instance of \ doesn't have some hidden down side.

EDIT: I am considering using the following approach to keep the XML simple (i.e. a single node) while also allowing specification of Key or Property, as well as backward compatibility with a simple path. So... This is what I had before

<Rule>[node] regPropertyExists:"HKLM\SOFTWARE\MozillaPlugins\@microsoft.com/GENUINE\GeckoVersion" -eq True</Rule>

And this is what I am doing for a Property specific path

<Rule>[node] regPropertyExists:"HKLM\SOFTWARE\MozillaPlugins\@microsoft.com/GENUINE P:GeckoVersion" -eq True</Rule>

As long as no one has P:with a space in front as part of a key or property name I'm good. And THAT seems like an edge case I can live with. I am also supporting Key specific paths like this

<Rule>[node] regKeyExists:"K:HKLM\SOFTWARE\MozillaPlugins\@microsoft.com/GENUINE" -eq True</Rule>

And this is a backwards compatible path

<Rule>[node] regPropertyExists:"HKLM\SOFTWARE\Px Tasks\Delete_Ex\Test" -eq True</Rule>

where the code tests for both a key and a property being found, and if I can't resolve to a single item I throw an error.

Anyone see any gotchas in that approach?

来源:https://stackoverflow.com/questions/49629753/split-path-with-non-standard-key-names

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!