Ad Widget

Collapse

Powershell Item not working

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • acarnahan
    Junior Member
    • Jun 2021
    • 2

    #1

    Powershell Item not working

    I'm trying to create an Item that will pull in the number of minutes a process is running, then i can create a trigger if it is running over 8min. The command I use below works if i just paste the powershell part on the system, but when i run it from Zabbix, i get the error just below the command.

    system.run[powershell.exe -NoLogo "((New-TimeSpan -Start (Get-Process -Name cmd).StartTime)).TotalMinutes"]


    New-TimeSpan : Cannot convert 'System.Object[]' to the type 'System.DateTime' required by parameter 'Start'. Specified
    method is not supported.
    At line:1 char:23
    + ((New-TimeSpan -Start (Get-Process -Name cmd).StartTime)).TotalMinute ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: ( [New-TimeSpan], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Command s.NewTimeSpanCommand


    Any thoughts?
  • acarnahan
    Junior Member
    • Jun 2021
    • 2

    #2
    Ok, i got the result i wanted but had to go to vbscript to get it. In case it helps anyone else.


    My Item:
    system.run[cscript.exe //Nologo C:\scripts\CheckCmdRuntime.vbs"]

    My Trigger:


    CheckCmdRuntime.vbs is this:

    Function WMIDateStringToDate(dtmStart)
    WMIDateStringToDate = CDate(Mid(dtmStart, 5, 2) & "/" & Mid(dtmStart, 7, 2) & "/" & Left(dtmStart, 4) & " " & Mid (dtmStart, 9, 2) & ":" & Mid(dtmStart, 11, 2) & ":" & Mid(dtmStart, 13, 2))
    End Function


    strComputer = "VIDEO"
    Set objWMIService = GetObject("winmgmts:\" & strComputer & "\root\cimv2")
    Set colProcessList = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name like 'cmd.exe'")
    For Each objProcess in colProcessList
    ' Wscript.Echo "Process\ID: " & objProcess.Name & " \ " & objProcess.ProcessID
    ' Wscript.Echo "CreationDate: " & objProcess.CreationDate
    Set dateTime = CreateObject("WbemScripting.SWbemDateTime")
    dateTime.SetVarDate(now())
    ' wscript.echo "DateTime: " & dateTime
    procTime = ObjProcess.CreationDate
    ' wscript.echo procTime
    strReturn = WMIDateStringToDate(procTime)
    ltDateTime = cdate(dateTime.GetVarDate (true))
    ' wscript.echo ltDateTime
    ' wscript.echo strReturn
    result = DateDiff("n", strReturn, ltDateTime)
    'wscript.echo result


    If result >= 8 Then
    wscript.echo "True"
    Else
    ' wscript.echo "False"
    End If

    Next

    Comment

    • tim.mooney
      Senior Member
      • Dec 2012
      • 1427

      #3
      If find it's often possible to avoid quoting issues by defining a UserParameter instead of using system.run[] directly. My guess is your first attempt with PowerShell would have been possible using that method.

      Comment

      Working...