我正在尝试在PowerShell中构建一组应用程序服务器的运行状况检查系统。我有8台服务器,每台服务器运行不同的应用程序,需要运行自己的特定服务。我希望这台服务器运行一组基本命令:
$RequiredServices = "Service1","Service2"
Get-Service -Name $RequiredServices | Select-Object name,status
Test-Connection -ComputerName $DomainController
然后,我想通过XML或HTML将此信息发送到SOAP或REST等本地Web服务,因此我可以快速快速拍摄网页上的所有应用程序服务。我可以弄清楚网页部分,但我对将这些数据发送到网络服务的最佳方法感到困惑? Invoke-WebRequest
?
您可以将测试结果传递给请求正文中的Web服务,这是调用Web服务将结果作为json传递给正文的示例。
正文可以表示您希望的任何数据结构,下面只是一个示例。
$Body = @{
"TestName" = $TestName
"Results" = @(
@{
"ServiceName" = $ServiceName
"Status" = $Status
}
)
} | ConvertTo-Json
$contentType = "application/json"
Invoke-WebRequest -TimeoutSec $TimeoutSec `
-Method PUT `
-Uri $Uri `
-ContentType $contentType `
-UseBasicParsing `
-Body $Body
注意:如果您多次呼叫休息api,则需要清理连接,因此您应该使用以下命令进行呼叫前置和尾随:
$ServicePoint = [System.Net.ServicePointManager]::FindServicePoint($Uri)
# Make call here
$ServicePoint.CloseConnectionGroup("") | Out-Null
有关详细信息,请参阅here。
正如@Clijsters指出的那样,Invoke-RestMethod是Invoke-WebRequest的另一种选择