2016-02-12 77 views
0

我是新来的PowerShell脚本,我需要编写一个脚本来得到一个网页的内容,如果内容不是“它工作”,以重新启动服务。PowerShell脚本用于阅读网站的内容和重新启动服务

这是我的代码如下所示:

$WebResponse= Invoke-WebRequest https://webpage.phpenter code here 
$WebResponse.Content 
$service = 'Service' 
$logoutput = 'service restarted' 
$Date = Get-Date 
If ($WebResponse.Content -eq "It Works") { 
Write-Host "OK" 
} 
Else { 
Restart-Service $service -Force 
} 
+1

好了,有什么问题..? – Sean

+0

该脚本不起作用,它每次都重新启动服务,它忽略了“It works”内容。所以我做错了什么,我需要帮助 – Leibovici

+0

你肯定'Content'只包含字符串“这工作”? – Sean

回答

0

$WebResponse.Content将包含整个HTML文件,其中包含之前和“这工作”后标签。

尝试

If ($WebResponse.Content.Contains("It Works")) { 
    Write-Host "OK" 
} else { 
    Restart-Service $service -Force 
} 
0

OK,我做了一些修改,我的剧本,现在它的工作原理。多谢你们!

$WebResponse= Invoke-WebRequest  https://websit.com 
$service = 'service' 
$logoutput = 'service restarted' 
$Date = Get-Date 
if ($WebResponse -match "It Works") { 
Write-Host "OK" 
} else { 
Restart-Service $service -Force 
"$Date, $logoutput" | Out-File -FilePath C:\log.txt -Append 
}