Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $URL='192.168.0.1' #URL or IP of a web service
- $vm='test VM' #VCenter vm name
- $logpath="E:\log\web.monitoring.txt" #log file
- $VCenterserver = "vcenter.contoso.com"
- $VCenteruser= "CONTOSO\www-mon"
- $VCenterpassword= "Pa$$w0rd"
- $time = try
- {
- $request = $null
- $timeout_msg = $null
- $result1 = Measure-Command { $request = Invoke-WebRequest -Uri $URL }
- $result1.TotalMilliseconds
- }
- catch
- {
- $request = $_.Exception.Response
- $timeout_msg = $_.Exception.Message
- $time = -1
- }
- $date=(Get-Date).ToString()
- if ($timeout_msg -ne $null)
- {
- Write-Output "Time = $($date);StatusCode = $($timeout_msg)"| Out-File -filepath $logpath -Append -noClobber
- }
- else
- {
- Write-Output "Time = $($date);StatusCode = $($request.StatusCode);StatusDescription = $($request.StatusDescription);ResponseLength = $($request.RawContentLength);TimeTaken = $($time)" | Out-File -filepath $logpath -Append -noClobber
- }
- if ($request.StatusCode -ne 200)
- {
- Add-PSSnapin VMware.VimAutomation.Core
- Add-PSSnapin VMware.VimAutomation.Vds
- Connect-VIServer -Server $VCenterserver -User $VCenteruser -Password $VCenterpassword -Force -Protocol https
- if (((Get-VM -Name $vm).PowerState) -like 'PoweredOff')
- {
- Write-Output "VM is currently Powered off" | Out-File -filepath $logpath -Append -noClobber
- }
- Else
- {
- Restart-VM -VM $vm -RunAsync -Confirm:$false
- Write-Output "VM was on, web server unresponsive, Reseted VM" | Out-File -filepath $logpath -Append -noClobber
- }
- Disconnect-VIServer -Server $VCenterserver -Force -Confirm:$false
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement