我花了好几个小时试图解决这个问题。我正在运行PowerShell来验证各种服务是否正在运行。我想从Windows任务计划程序每5分钟运行一次。
它检查其他服务器上的服务,以及运行它的同一台机器上的服务。当我在任务调度程序下运行它时,在我正在运行交互的相同用户标识下,我得到不同的结果。以交互方式显示本地计算机上的所有服务正在运行。当通过任务调度程序运行时,它告诉我找不到服务。
这只是一个大型程序的一个片段。我从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 =“未找到服务” } 其他 { $ reportStatus =“发现服务” } $ myDate =获取日期 Write-Host $ reportStatus 添加内容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
从非管理员命令提示符窗口或调度程序运行命令文件似乎也有不同的结果。
New Simpler Version如果有人想尝试,只需替换两个计算机名称:
$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来自任务调度程序。
答案 0 :(得分: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: System.Management.ManagementException.ThrowWithExtendedInfo
拒绝访问 errorCode)at System.Management.ManagementObjectCollection.ManagementObjectEnumerator.MoveNext() 在Microsoft.PowerShell.Commands.GetWmiObjectCommand.BeginProcessing()
我还没有想出“拒绝访问”,但至少我很高兴我现在看到了真正的错误,即“get-wmiobject win32_service ...”的结果为空的原因。
我在这里的新主题中跟进“拒绝访问”: Access Denied - get-wmiobject win32_service (Powershell)