我有一个PowerShell脚本,检查运行它的服务器的CPU使用率,如果超过一定阈值,就会运行SQL存储过程并发送电子邮件。
该脚本在我的开发服务器上以最新版本的PowerShell正确运行。但是,在需要获取CPU值的生产服务器上运行Invoke-Sqlcmd
命令时遇到了问题。例如可以称为"LIVESERVER"。
它正在运行PowerShell 2.0,我认为这就是问题所在:
The term 'Invoke-Sqlcmd' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
由于这是业务关键型服务器,我不想对其进行任何更改(除非更新PowerShell而无需重新启动等操作非常简单)。
是否可能从我的开发服务器运行我的脚本并指定要获取CPU数据的服务器?我不确定如何编写语法以实现此目的。
以下是我的脚本:
# Email
$recipients = @("Ricky <ricky@email.com>")
# Loop 20 times
for ($i= 1; $i -le 20; $i++) {
# Find CPU average usage percentage for given time period
$cpuutil = (Get-Counter -Counter "\Processor(_Total)\% Processor Time" -SampleInterval 1 -MaxSamples 20 |
select -ExpandProperty countersamples |
select -ExpandProperty cookedvalue |
Measure-Object -Average).Average
# Display average CP output
$cpuutil
# If CPU average percentage is higher than given value, run stored procedure and
# e-mail to notify
if ($cpuutil -ge 60) {
Invoke-Sqlcmd -Query "SELECT * FROM [Vehicle].[dbo].[tblIndicator]" -ServerInstance "LIVESERVER"
}
if ($cpuutil -ge 60) {
Send-MailMessage -From "serverchecks@email.com" -To $recipients -Body "SP Ran" -Subject "Stored Procedure" -Dno onSuccess, onFailure -SmtpServer 111.1.1.111
} else {
Start-Sleep -s 10
}
}