As the title says, I have a script that works fine on a server when testing in ISE, but when added as a PS script component I get a failure.
Here's the script I'm using:
$pageLife = (get-counter -Counter "\SQLServer:Buffer Manager\Page Life Expectancy").CounterSamples.CookedValue
$lazyWrites = (get-counter -Counter "\SQLServer:Buffer Manager\Lazy writes/sec").CounterSamples.CookedValue
If ($pageLife -lt '300' -AND $lazyWrites -gt '60') {
# This is a bad condition and should exit with an error
Write-Host "Message.PageLife: Critical"
Write-Host "Message.LazyWrites: Critical"
Write-Host "Statistic.PageLife: $pageLife"
Write-Host "Statistic.LazyWrites: $lazyWrites"
Exit 3
}
Else {
# This is a good condition and should exit without an error
Write-Host "Message.PageLife: OK"
Write-Host "Message.LazyWrites: OK"
Write-Host "Statistic.PageLife: $pageLife"
Write-Host "Statistic.LazyWrites: $lazyWrites"
Exit 0
}
This returns the appropriate values in ISE, but when testing the script component on the exact same server I get this error back in SAM:
Output: ============================================== Message.PageLife: OK Message.LazyWrites: OK Statistic.PageLife: Statistic.LazyWrites: Errors: ============================================== Get-Counter : The specified object was not found on the computer. At line:1 char:14 + $pageLife = (Get-Counter -Counter "\SQLServer:Buffer Manager\Page Life Expectanc ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidResult: (:) [Get-Counter], Exception + FullyQualifiedErrorId : CounterApiError,Microsoft.PowerShell.Commands.GetCounterCommand Get-Counter : The specified object was not found on the computer. At line:2 char:16 + $lazyWrites = (Get-Counter -Counter "\SQLServer:Buffer Manager\Lazy writes/sec") ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidResult: (:) [Get-Counter], Exception + FullyQualifiedErrorId : CounterApiError,Microsoft.PowerShell.Commands.GetCounterCommand |
From what I can tell it doesn't like the Get-Counter command. What would cause a different behavior when running this on the server vs running as a PS script from SAM?
A