简体   繁体   English

powershell脚本中的WMI查询在计划任务中运行时不返回任何对象

[英]WMI query in powershell script returns no object when run in a scheduled task

I have a powershell script that runs successfully. 我有一个成功运行的powershell脚本。 I have tested it to death. 我已经测试过它。 The problem comes when running it as a scheduled task. 将其作为计划任务运行时会出现问题。 The WMI query returns no object. WMI查询不返回任何对象。 I have tested with the a powershell console running under the context of the account used to run the scheduled task and it runs successfully under those conditions as well. 我已经使用在用于运行计划任务的帐户的上下文下运行的powershell控制台进行了测试,并且它也在这些条件下成功运行。 Only when running as a scheduled task does the WMI query fail. 仅当作为计划任务运行时,WMI查询才会失败。

...

Function getMSMQMessageCount($queueName) {
    Add-content $LogFile "Querying $queueName"

    $query = "SELECT MessagesinQueue FROM Win32_PerfRawData_MSMQ_MSMQQueue WHERE Name = '$queueName'"
    try{
        $wmiObject = Get-WmiObject -Query $query
        $wmiObject.MessagesinQueue
    }catch{
        Add-content $LogFile "MSMQ Enumeration error $($_.Exception)" 
    }
}

$messaging = getMSMQMessageCount 'server\\private$\\messaging.application'

Exception caught in the Function used to issue the query: 用于发出查询的函数中捕获的异常:

System.Management.Automation.RuntimeException: Property 'MessagesinQueue' cannot be found on this object. Make sure that it exists.
   at System.Management.Automation.PropertyReferenceNode.GetValue(PSObject obj, Object property, ExecutionContext context)
   at System.Management.Automation.PropertyReferenceNode.Execute(Array input, Pipe outputPipe, ExecutionContext context)
   at System.Management.Automation.ParseTreeNode.Execute(Array input, Pipe outputPipe, ArrayList& resultList, ExecutionContext context)
   at System.Management.Automation.StatementListNode.ExecuteStatement(ParseTreeNode statement, Array input, Pipe outputPipe, ArrayList& resultList, ExecutionContext context)

I haven't tested your code, but it sounds like it requires administrator-rights to retrieve the data. 我没有测试过你的代码,但听起来它需要管理员权限来检索数据。

To use your account's administrator-rights, you need to check the Run with highest privileges -checkbox on the General page of the scheduled task. 要使用您帐户的管理员权限,您需要在计划任务的“常规”页面上选中Run with highest privileges检查Run with highest privileges That is similar to answering yes in the UAC-dialog when you run it manually. 这类似于在手动运行时在UAC对话框中回答“是”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM