键不存在时出现powershell错误

时间:2017-05-13 19:00:37

标签: powershell if-statement hide

我制作了我的剧本

$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
}

但是当它们没有键时,我得到以下错误: reg:错误:系统无法找到指定的注册表项或值。 在C:\ Users \ syslocal \ Desktop \ RepopulateTDL2.ps1:1 char:18 + ... iceversion = reg query" HKEY_CLASSES_ROOT \ Outlook.Application \ CurVer" + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~     + CategoryInfo:NotSpecified :(错误:系统...键或值。:String)[],RemoteException     + FullyQualifiedErrorId:NativeCommandError

是否有人建议如果密钥不存在我可以抑制此错误?

谢谢

5 个答案:

答案 0 :(得分:2)

正如其他人无疑会指出的那样,您甚至可能已经意识到,在PowerShell中使用CMD命令并不是PowerShell的使用方式。它能够使用自己的工具集运行注册表查询。您希望尽可能坚持PowerShell cmdlet的主要原因是您不必担心这样的情况,程序将错误返回到与成功输出相同的流,并且您作为编码人员必须考虑到它。 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

答案 1 :(得分:1)

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

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

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

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

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

答案 2 :(得分:0)

$null重定向到$officeversion = reg query "HKEY_CLASSES_ROOT\Outlook.Application\CurVer" 2> $null

String query = "CREATE TEMPORARY TABLE my_table USING org.apache.spark.sql.cassandra OPTIONS (table \"my_table\",keyspace \"my_keyspace\", pushdown \"true\")";
                spark.sparkSession.sql(query);
                spark.sparkSession
                                .sql("INSERT INTO my_keyspace.my_table (column0, column1) VALUES ('value0', 'value1');

答案 3 :(得分:0)

这种方式有效,

感谢我指导我走向正确的方向

$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语句

答案 4 :(得分:0)

您可以使用以下命令测试密钥是否存在 测试路径“ HKLM:\ software \ classes \ outlook.application \ curver”

您将拥有对$ true或false的声明