在应用Windows更新之前,数据库检查



这是我要做的。

  1. 查询数据库以检查运行的作业
  2. 如果没有工作,请运行Windows更新
  3. 如果有运行它的作业填充变量
  4. 如果没有工作正在运行,则变量保持null。

我遇到的问题是,如果在查询上发生错误,则变量将保持null,然后运行Windows更新。

我正在寻找想法如何正确运行此支票,并且仅在变量为null时才运行Windows更新。

  $result = Invoke-Sqlcmd -ServerInstance  'ipaddress,port' -Database 'instance' -Query $testQuery -QueryTimeout 15

if (!$result)
{
    Get-WUInstall -WindowsUpdate Software -AcceptAll
}

如果分配$result可能会给您错误,那么我只会跟踪$error变量,例如:

$error.clear()
$result = Invoke-Sqlcmd -ServerInstance  'ipaddress,port' -Database 'instance' -Query $testQuery -QueryTimeout 15

if (!$result -and !$error)
{
    Get-WUInstall -WindowsUpdate Software -AcceptAll
}

测试案例:没有错误,没有结果

$error.clear()
$result = $null
if (!$result -and !$error) {
Write-Host "Updating Windows..."
}
Updating Windows...

测试案例:结果错误

$error.clear()
$result = Invoke-Sqlcmd -ServerInstance  'ipaddress,port' -Database 'instance' -Query $testQuery -QueryTimeout 15
Invoke-Sqlcmd : The term 'Invoke-Sqlcmd' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
if (!$result -and !$error) {
Write-Host "Updating Windows...."
}
[bool]$error
True
[bool]$result
False

$error.clear()只是清空$error变量。如果您期望该变量在整个脚本中跟踪错误,这可能不是完全明智的。我只能假设,因为我只看到了您在做什么的片段。

相关内容

最新更新