I have written a Powershell script with a defined function. It runs just fine when executed in a powershell window, but when PRTG runs it, it says the command (the function I defined later in the script) is not recognized. Is there some restriction on how functions work in Powershell when used by PRTG?

Here is the beginning of my code: (Get-ScheduledTask is a function I defined right after this code)

Param(
    [string]$MyComputerName,
    [string]$MytaskName
)

$task = ((Get-ScheduledTask -ComputerName $MyComputerName) | select TaskName, LastRunTime | Where-Object {$_.TaskName -eq $MytaskName})

write-host (New-Timespan -Start $task.LastRunTime).Minutes,":OK"

Article Comments

Hello,

Thank you very much for your KB-Post. First, please try defining the funcition before actually referring to it in your script code. And then please also try the following included at the beginning of the script:

Import-Module Microsoft.PowerShell.Management; Import-Module Microsoft.PowerShell.Utility

Does it work then?

best regards.


Jul, 2016 - Permalink

Thanks for the suggestion(s). I moved the function above the script code calling the function and it works now.

Strange.


Jul, 2016 - Permalink