I have a PowerShell script that checks the CPU level of the server it is running on, and then if it is above a certain threshold it will run a SQL stored procedure and e-mai
This should help your debugging!
if (-not (Get-Command Invoke-Sqlcmd -ErrorAction SilentlyContinue)) {
Write-Error "Unabled to find Invoke-SqlCmd cmdlet"
}
if (-not (Get-Module -Name SqlServer | Where-Object {$_.ExportedCommands.Count -gt 0})) {
Write-Error "The SqlServer module is not loaded"
}
if (-not (Get-Module -ListAvailable | Where-Object Name -eq SqlServer)) {
Write-Error "Can't find the SqlServer module"
}
Import-Module SqlServer -ErrorAction Stop