Powershell安全性获取服务不同结果交互式任务与计划任务
我花了几个小时试图解决这个问题。我正在运行PowerShell以验证各种服务是否正在运行。我想从Windows任务计划程序每隔5分钟运行一次 它检查其他服务器上的服务,以及运行它的同一台机器上的一些服务。当我在TaskScheduler下运行它时,在运行interactive的同一用户ID下,我会得到不同的结果。以交互方式显示本地计算机上的所有服务正在运行。当运行任务调度器时,它告诉我找不到服务 这只是一个更大程序的一个片段。我从CSV文件中获取服务器/服务名称,然后在最后它发送一封漂亮的HTML电子邮件。我添加了addcontent来创建一个跟踪文件来证明这一点Powershell安全性获取服务不同结果交互式任务与计划任务,powershell,wmi,wmi-service,Powershell,Wmi,Wmi Service,我花了几个小时试图解决这个问题。我正在运行PowerShell以验证各种服务是否正在运行。我想从Windows任务计划程序每隔5分钟运行一次 它检查其他服务器上的服务,以及运行它的同一台机器上的一些服务。当我在TaskScheduler下运行它时,在运行interactive的同一用户ID下,我会得到不同的结果。以交互方式显示本地计算机上的所有服务正在运行。当运行任务调度器时,它告诉我找不到服务 这只是一个更大程序的一个片段。我从CSV文件中获取服务器/服务名称,然后在最后它发送一封漂亮的HTM
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_服务-ComputerName DAL-BIZ-APP01-filter name='LanManServer'
if$Service-eq$null
{
$reportStatus=未找到服务
}
其他的
{
$reportStatus=找到服务
}
$myDate=获取日期
写入主机$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
计划作业结果:
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 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
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来自任务计划程序 我将此添加到代码中:
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.ThrowWithExtendedInfoManagementStatus拒绝访问
错误代码在
System.Management.ManagementObjectCollection.ManagementObjectEnumerator.MoveNext
位于Microsoft.PowerShell.Commands.GetWmiObjectCommand.BeginProcessing
我还没有弄清楚拒绝访问的原因,但至少我很高兴现在看到了真正的错误,即get-wmiobject win32_服务结果的原因。。。是空的
我正在跟踪一个新线程中被拒绝的访问:
当您以交互方式运行此程序时,是否在提升的PowerShell窗口中运行?我刚刚检查了一下,发现在我的系统上,至少有一个服务显示在提升的控制台中,而标准控制台中没有列出。另外,如果您正在检查多个服务器,您应该将服务器名称添加到“未找到的服务”行中-它应该可以帮助您准确地确定问题发生的位置。计划程序是通过命令文件运行的,我刚才在我的问题中添加了这一点。我可以在非管理员命令提示符下运行命令文件,它运行良好。我还想强调的是,它在远程服务器上运行良好,只有本地服务器无法运行。对于最终输出,我发送了一封HTML格式的电子邮件,因此非常容易阅读。上面的跟踪/输出只是为了快速调试。将新的示例代码放在原始帖子中。您是否在任务的“权限”窗口的“常规”选项卡中选中了“以最高权限运行”框?我不确定这是否是原因,但我通过勾选复选框解决了类似的问题