Powershell 无法将变量插入Get项的路径
我试图获取一个由返回分隔的字符串列表,并用Powershell 无法将变量插入Get项的路径,powershell,Powershell,我试图获取一个由返回分隔的字符串列表,并用=符号将其拆分,两边匹配一个注册表项,该注册表项由Get Item查询。前半部分始终匹配良好,但引用后半部分仅匹配列表中的最后一项: 前半部分示例: [string[]]$Patch = (".accda=Access.ACCDAExtension.16 .accdb=Access.Application.16" -split "`n") Foreach ($a in $Patch) { $RegKey = ($a -split "=")[0]
=
符号将其拆分,两边匹配一个注册表项,该注册表项由Get Item查询。前半部分始终匹配良好,但引用后半部分仅匹配列表中的最后一项:
前半部分示例:
[string[]]$Patch = (".accda=Access.ACCDAExtension.16
.accdb=Access.Application.16" -split "`n")
Foreach ($a in $Patch) {
$RegKey = ($a -split "=")[0]
$Result = Get-Item -Path "Registry::HKEY_CLASSES_ROOT\$RegKey"
echo $Result
}
[string[]]$Patch = ("
.accda=Access.ACCDAExtension.16
.accdb=Access.Application.16
" -split "`n")
Foreach ($a in $Patch) {
$RegKey = ($a -split "=")[1]
$Result = Get-Item -Path "Registry::HKEY_CLASSES_ROOT\$RegKey"
}
这将返回:
Name Property
---- --------
.accda (default) : Access.ACCDAExtension.16
Content Type : application/msaccess.addin
.accdb (default) : Access.Application.16
Content Type : application/msaccess
Get-Item : Cannot find path 'HKEY_CLASSES_ROOT\Access.ACCDAExtension.16' because it does not exist.
At line:6 char:15
+ $Result = Get-Item -Path "Registry::HKEY_CLASSES_ROOT\$RegKey"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (HKEY_CLASSES_RO...DAExtension.16:String) [Get-Item], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand
Hive: HKEY_CLASSES_ROOT
Name Property
---- --------
Access.Application.16 (default) : Microsoft Access Database
下半部分示例:
[string[]]$Patch = (".accda=Access.ACCDAExtension.16
.accdb=Access.Application.16" -split "`n")
Foreach ($a in $Patch) {
$RegKey = ($a -split "=")[0]
$Result = Get-Item -Path "Registry::HKEY_CLASSES_ROOT\$RegKey"
echo $Result
}
[string[]]$Patch = ("
.accda=Access.ACCDAExtension.16
.accdb=Access.Application.16
" -split "`n")
Foreach ($a in $Patch) {
$RegKey = ($a -split "=")[1]
$Result = Get-Item -Path "Registry::HKEY_CLASSES_ROOT\$RegKey"
}
这将返回:
Name Property
---- --------
.accda (default) : Access.ACCDAExtension.16
Content Type : application/msaccess.addin
.accdb (default) : Access.Application.16
Content Type : application/msaccess
Get-Item : Cannot find path 'HKEY_CLASSES_ROOT\Access.ACCDAExtension.16' because it does not exist.
At line:6 char:15
+ $Result = Get-Item -Path "Registry::HKEY_CLASSES_ROOT\$RegKey"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (HKEY_CLASSES_RO...DAExtension.16:String) [Get-Item], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand
Hive: HKEY_CLASSES_ROOT
Name Property
---- --------
Access.Application.16 (default) : Microsoft Access Database
但是,该注册表项确实存在,如果手动运行该命令,它会找到它:
get-item Registry::HKEY_CLASSES_ROOT\Access.ACCDAExtension.16
Hive: HKEY_CLASSES_ROOT
Name Property
---- --------
Access.ACCDAExtension.16 (default) : Microsoft Access Add-in
我很确定这是由每行末尾的返回引起的
PowerShell 5.1版我已解决此问题,将我的列表设置为字符串列表:
[string[]]$Patch = (".accda=Access.ACCDAExtension.16",
".accdb=Access.Application.16" -split "`n")
Foreach ($a in $Patch) {
$RegKey = ($a -split "=")[1]
$Result = Get-Item -Path "Registry::HKEY_CLASSES_ROOT\$RegKey"
}
该问题可能是由每行末尾的返回引起的,也许我也可以将其从命令中过滤掉。更改此选项:
[string[]]$Patch = ("
.accda=Access.ACCDAExtension.16
.accdb=Access.Application.16
" -split "`n")
对此
[string[]]$Patch = (-split "
.accda=Access.ACCDAExtension.16
.accdb=Access.Application.16
")
这样你就没有任何空白了
我很快就尝试了,但它没有给我错误,执行得很好。您使用的是什么PS版本?不确定,但请尝试在任何PowerShell版本中使用Join Path
Join Path“注册表::HKEY\U CLASSES\U ROOT”“txtfile”
您发布的命令本身并不能解释您的症状。请使用缩小问题范围所需的其他信息更新您的问题。@AimusSage我正在运行5.1版。在设置$RegKey
和调用Get Item
之间,您还有什么要做的吗?