密钥不存在时出现 powershell 错误

powershell error when key is not present

我已经制作了我的脚本

$officeversion = reg query "HKEY_CLASSES_ROOT\Outlook.Application\CurVer"
if ($officeversion -eq "    (Default)    REG_SZ    Outlook.Application.16"){
Write-Host "Office 2016"
Exit 0
}
if ($officeversion -eq "    (Default)    REG_SZ    Outlook.Application.15"){
Write-Host "Office 2013"
Exit 0
}
if ($officeversion -eq "    (Default)    REG_SZ    Outlook.Application.14"){
Write-Host "Office 2010"
Exit 0
}
if ($officeversion -eq "    (Default)    REG_SZ    Outlook.Application.12"){
Write-Host "WARNING: Office 2007"
Exit 1010
}
if ($officeversion -eq "    (Default)    REG_SZ    Outlook.Application.11"){
Write-Host "ALERT: Office 2003"
Exit 1010
}
if ($officeversion -eq "    (Default)    REG_SZ    Outlook.Application.10"){
Write-Host "ALERT: Office XP"
Exit 1010
}

else {
Write-Host "No Office Installed"
Exit 0
}

但是当他们的密钥不存在时,我收到以下错误: 注册:错误:系统无法找到指定的注册表项或值。 在 C:\Users\syslocal\Desktop\RepopulateTDL2.ps1:1 char:18 + ... iceversion = reg 查询 "HKEY_CLASSES_ROOT\Outlook.Application\CurVer" + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~ + CategoryInfo:未指定:(错误:系统...y 键或值。:字符串)[],RemoteException + FullyQualifiedErrorId : NativeCommandError

有没有人建议我可以在密钥不存在的情况下抑制此错误?

谢谢

您可以使用 PowerShells Registry provider 访问注册表。

New-PSDrive -PSProvider Registry -Root HKEY_CLASSES_ROOT -Name HKCR

然后您可以像对常规文件和目录一样使用 Get-ItemGet-ChildItemTest-Path 等命令。这样您就可以使用 PowerShell 错误处理,例如 -ErrorAction 参数或 try / catch 块和所有好东西。

Get-Item HKCR:\Outlook.Application\CurVer -ErrorAction Ignore

附带说明:对于上面的脚本,您应该查看 switch 语句,而不是使用许多 if 语句。

stderr 重定向到 $null

$officeversion = reg query "HKEY_CLASSES_ROOT\Outlook.Application\CurVer" 2> $null

正如其他人无疑会指出的那样,您甚至可能已经意识到,在 PowerShell 中使用 CMD 命令并不是 PowerShell 的使用方式。它不仅能够使用自己的工具集进行 运行 注册表查询。您希望尽可能坚持使用 PowerShell cmdlet 的主要原因是您不必担心这样的情况,其中程序 returns 错误与成功输出相同的流,而您作为编码员必须考虑到它。 PowerShell cmdlet 的优点是将不同的输出发送到不同的流。参见 Guide。如果可能的话,我建议转向这样的事情:

Get-ChildItem -Path "Registry::HKEY_CLASSES_ROOT\Outlook.Application\CurVer"

据我了解,在某些情况下最佳实践是不可能的,您想要寻找的解决方案称为 Try {} Catch {}。现在我无法重新创建您的错误消息,但我已经尽可能接近了。参见示例:

Try {
    $officeversion = reg query "HKEY_CLASSES_ROOT\Outlook.Application\CurVer"
}
Catch [System.Management.Automation.ItemNotFoundException] {
    # Registry path was not found
}
Catch {
    # Some other error was thrown
}
Finally {
    # Cleanup actions
    # This step happens regardless of whether or not an error was thrown
}

有关 Try {} Catch {}

的良好文档,请参阅 here

这有点管用,

感谢您指导我正确的方向,我最终得到了

$officeversion = (get-itemproperty -ErrorAction Ignore -literalpath HKCR:\Outlook.Application\CurVer).'(default)'

if ($officeversion -eq "Outlook.Application.16"){
Write-Host "Office 2016"
Exit 0
}
if ($officeversion -eq "Outlook.Application.15"){
Write-Host "Office 2013"
Exit 0
}
if ($officeversion -eq "Outlook.Application.14"){
Write-Host "Office 2010"
Exit 0
}
if ($officeversion -eq "Outlook.Application.12"){
Write-Host "WARNING: Office 2007"
Exit 1010
}
if ($officeversion -eq "Outlook.Application.11"){
Write-Host "ALERT: Office 2003"
Exit 1010
}
if ($officeversion -eq "Outlook.Application.10"){
Write-Host "ALERT: Office XP"
Exit 1010
}
if ($officeversion -eq "Outlook.Application.10"){
Write-Host "ALERT: Office XP"
Exit 1010
}
else {
Write-Host "No Office Installed"
Exit 0
}

以后还会看switch语句

您可以测试密钥是否存在 测试路径 "HKLM:\software\classes\outlook.application\curver"

您将对 $true 或 false 有陈述