Powershell将心跳更新到云监视



我正在尝试创建一个函数来将服务器的心跳更新到 cloudwatch。但是从下面的函数来看,这个函数并没有中断。我确信我犯了一些错误。但是我对Powershell很陌生,请您帮助纠正它。

function Get-spooler{
$INSTANCE_ID=$(Invoke-WebRequest -Uri http://169.254.169.254/latest/meta-data/instance-id)
$service=Get-Service -Name Spooler 
write-host $INSTANCE_ID
write-host $service
write-host $service.Status
while($service.Status -eq 'Running')
{
aws --region ap-southeast-1 cloudwatch put-metric-data --metric-name Test13 --value 1 --namespace ssm  --dimensions InstanceId=$INSTANCE_ID
if ($service.Status -eq 'Stopped')
{
aws --region ap-southeast-1 cloudwatch put-metric-data --metric-name Test13 --value 0 --namespace ssm  --dimensions InstanceId=$INSTANCE_ID
}}}

$service在您分配静态对象时存储该对象。因此,如果服务在运行时$service=Get-Service -Name Spooler$service.status将始终等于"正在运行"。

尝试:while((Get-Service -Name Spooler).Status -eq 'Running')

最新更新