1

我花了几个小时试图解决这个问题。我正在运行 PowerShell 来验证各种服务是否正在运行。我想从 Windows 任务计划程序每 5 分钟运行一次。

它检查其他服务器上的服务,以及一些在它运行的同一台机器上的服务。当我在任务调度程序下运行它时,在我运行交互的相同用户 ID 下,我得到不同的结果。以交互方式显示本地计算机上的所有服务正在运行。通过任务调度程序运行时,它告诉我找不到服务。

这只是一个更大程序的一个片段。我从 CSV 文件中获取服务器/服务名称,然后在最后发送一封漂亮的 HTML 电子邮件。我添加了 Add-Content 以创建跟踪文件来证明这种情况。

foreach ($line in $csv) {
    $reportStatus = "" 
    $ServerCount = $ServerCount + 1  

    #$Service = (get-service -Name $line.ServiceName -ComputerName $line.ServerName)
    #this is slower than above, but it gives us the processId which we can use to find out what time the service/process started 
    write-host "Verifying: " $line.ServerName $line.ServiceName 
    $myDate = Get-Date
    Add-Content D:\scripts\ServiceMonitorTrace.txt "$myDate  $($line.ServerName) $($line.ServiceName)"
    $Service = (get-wmiobject win32_service -ComputerName $line.ServerName -filter "name = '$($line.ServiceName)'")
    if ($Service -eq $null) 
    {
        $reportStatus = "Service Not Found: name = '$($line.ServiceName)'"
        $trColor = "Yellow"
        $ErrorCount = $ErrorCount + 1  
        $CriticalErrorCount = $CriticalErrorCount + 1
        $CreationDate = "NA" 
        Write-Host "----> $reportStatus " 
        Add-Content D:\scripts\ServiceMonitorTrace.txt "$myDate  $reportStatus" 
    }
  }

新的更简单的版本(有完全相同的问题): $Service = (get-wmiobject win32_service -ComputerName "DAL-BIZ-APP01" -filter "name = 'LanManServer'") if ($Service -eq $null) { $reportStatus = "未找到服务" } else { $reportStatus = "找到服务" } $myDate = Get-Date Write-Host $reportStatus Add-Content D:\scripts\ServiceTestTrace.txt "$myDate $reportStatus"

互动结果:

10/31/2013 09:34:00  DAL-BIZ-APP01 MSDTC
10/31/2013 09:34:00  DAL-BIZ-APP01 BTSSvc$BizTalkHost_QT_Default

预定的作业结果:

10/31/2013 09:25:42  DAL-BIZ-APP01 MSDTC
10/31/2013 09:25:42  Service Not Found: name = 'MSDTC'
10/31/2013 09:25:42  DAL-BIZ-APP01 BTSSvc$BizTalkHost_QT_Default

我从包含以下内容的命令文件运行它:

powershell -command "& 'D:\Scripts\ServerMonitor.ps1'" d:\Scripts\ServerMonitorConfig.csv

从非管理员命令提示符窗口或调度程序运行命令文件似乎也有不同的结果。

如果有人想尝试新的更简单的版本,只需替换两个计算机名称:

$Service = (get-wmiobject win32_service -ComputerName "DAL-BIZ-APP01" -filter "name = 'LanManServer'")
if ($Service -eq $null) 
{
  $reportStatus = "Service not found" 
}
else 
{
  $reportStatus = "Service found" 
}
$myDate = Get-Date
Write-Host $reportStatus
Add-Content D:\scripts\ServiceTestTrace.txt "$myDate  DAL-BIZ-APP01 $reportStatus" 

$Service = (get-wmiobject win32_service -ComputerName "DAL-BIZ-APP02" -filter "name = 'LanManServer'")
if ($Service -eq $null) 
{
  $reportStatus = "Service not found" 
}
else 
{
  $reportStatus = "Service found" 
}
$myDate = Get-Date
Write-Host $reportStatus
Add-Content D:\scripts\ServiceTestTrace.txt "$myDate  DAL-BIZ-APP02 $reportStatus" 

结果:

10/31/2013 16:07:48  DAL-BIZ-APP01 Service found
10/31/2013 16:07:48  DAL-BIZ-APP02 Service found
10/31/2013 16:08:03  DAL-BIZ-APP01 Service not found
10/31/2013 16:08:03  DAL-BIZ-APP02 Service found

16:07:48 来自命令提示符,16:08:03 来自任务调度程序。

4

1 回答 1

0

我将此添加到代码中:

if ($error -ne $null) 
{
    Write-Host "----> $($error[0].Exception) " 
    Add-Content $TraceFilename "$myDate TRCE1 $($error[0].Exception)" 
}

现在我可以看到被吞下的原因:

2013 年 11 月 13 日 11:35:37 TRCE1 System.Management.ManagementException: 在 Microsoft.PowerShell.Commands 的 System.Management.ManagementObjectCollection.ManagementObjectEnumerator.MoveNext() 的 System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus errorCode)拒绝访问。 GetWmiObjectCommand.BeginProcessing()

我还没有弄清楚“拒绝访问”,但至少我很高兴我现在看到了真正的错误,即“get-wmiobject win32_service ...”结果为空的原因。

我正在这里的新线程中跟进“访问被拒绝”: 访问被拒绝-get-wmiobject win32_service(Powershell)

于 2013-11-13T17:43:14.547 回答