vai*_*olo 3 powershell monitoring scom
我Invoke-WebRequest在 SCOM PowerShell 脚本中使用它来定期监视 URI 的可用性。我的脚本相当简单(因为我对 PS 知之甚少:-)):
$scomapi = new-object -comObject "MOM.ScriptAPI"
$scompb = $scomapi.CreatePropertyBag()
$fullHostName = "https://" + <full path to monitored web endpoint>
$result = Invoke-WebRequest $fullHostName
if($result.content) {
$scompb.AddValue("ConfigurationReachable",$true);
} else {
$scompb.AddValue("ConfigurationReachable",$false);
}
$scomapi.AddItem($scompb)
$scomapi.ReturnItems()
Run Code Online (Sandbox Code Playgroud)
为了测试这个脚本,我在hosts运行 SCOM 代理(我想要进行监视)的客户端上的文件中进行了手动更改。有趣的是,即使主机无法访问,脚本也能成功获取 Web 端点(通过从该计算机进行 ping 测试来测试)。
我直接从命令行做了一些进一步的测试,没有任何变化。即使我没有 ping 到远程地址,Invoke-WebRequest仍然成功并获取网页。那么我在这里做错了什么?
根据评论中的讨论,问题是缓存;只是问题不在于被缓存的 IP(至少不是唯一的问题);内容也被缓存;因此,系统不会通过网络服务器来获取资源,而是会作弊并在本地获取资源。您可以通过添加-Headers @{"Cache-Control"="no-cache"}到您的invoke-webrequest.
请参阅下面的示例测试脚本;cache-control尝试在主机文件调整之前和之后使用或不使用标头运行。
cls
$urlHost = 'server.mydomain.com'
$endpointUrl = ("https://{0}/path/to/resource.jpg" -f $urlHost)
#can be set once at the start of the script
[System.Net.ServicePointManager]::DnsRefreshTimeout = 0
#I don't have Clear-DnsClientCache; but the below should do the same thing
#Should be called inside any loop before the invoke-webrequest to ensure
#flush your machine's local dns cache each time
ipconfig /flushdns
#prove that our hosts update worked:
#optional, but will help in debugging
Test-Connection $urlHost -Count 1 | select ipv4address
#ensure we don't have a remembered result if invoke-request is in a loop
$result = $null
#make the actual call
#NB: -headers parameter takes a value telling the system not to get anything
#from the cache, but rather to send the request back to the root source.
$result = Invoke-WebRequest $endpointUrl -Headers @{"Cache-Control"="no-cache"}
#output the result; 200 means all's good (google http status codes for info on other values)
("`nHTTP Status Code: {0}`n" -f $result.StatusCode)
#output actual result; optional, but may be useful to see what's being returned (e.g. is it your page/image, or a 404 page / something unexpected
$result
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7335 次 |
| 最近记录: |