powershell 监控SharePoint,本地服务器和SQL服务,页面并向某人发送报告电子邮件。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了powershell 监控SharePoint,本地服务器和SQL服务,页面并向某人发送报告电子邮件。相关的知识,希望对你有一定的参考价值。

<?xml version="1.0"?>
<config>
	<webpages>
		<!--Pre-Production-->
		<web url="http://rbla-sp2010-002:9999/default.aspx" name="Central Administration" environement="Pre-Production"/>
		<web url="http://rbla-sp2010-002/default.aspx" name="HomePage" environement="Pre-Production"/>
		
		<!--Production-->
		<web url="http://rbla-sp2010-002:9999/default.aspx" name="Central Administration" environement="Production"/>
		<web url="http://rbla-sp2010-002/default.aspx" name="HomePage" environement="Production"/>
	</webpages>
</config>
# ----------------------------------------------
# Author: Romain Blanchard
# Date: 29.06.2012
# Description: Monitor SharePoint, local server & SQL services, pages and send report email to someone.
# ----------------------------------------------

# -- Initialize -- #
Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue

# -- Script -- #
$outwebpages = new-object psobject
$date = Get-Date -format yyyyMMdd
cd D:\logica\monitoring
$output = "rapport$date.html"

$style = "<style>BODY{font-family: Tahoma, Arial; font-size: 10pt;}"
$style = $style + "TABLE{border: 1px solid black; border-collapse: collapse;}"
$style = $style + "TH{border: 1px solid black; background: #dddddd; padding: 5px; }"
$style = $style + "TD{border: 1px solid black; padding: 5px; }"
$style = $style + "</style>"
 
function CheckURL( ){
param($url)
 
	Set-Variable -Name return -Value 0 -Scope 0 
	trap [Exception]{	
	Set-Variable -Name return -Value 1 -Scope 1
	$errormsg = $($_.Exception).ToString()
	$errormsg = ($errormsg.TrimStart("Error: System.Net.WebException: Le serveur distant a retourné une erreur :")).split(".")
	$short_errormsg = (($errormsg[0]).split(":"))[1]
		write-host "[$webenvironement] $webname --> Error :$short_errormsg"	| ConvertTo-html  -Body "<span style='color:red;'>[$webenvironement] $webname --> Error :$short_errormsg</span>" >> "$output"
		continue
	}
  
	$webclient = New-Object Net.WebClient
	# Specific credential Block
	# $webclient.Credentials = new-object System.Net.NetworkCredential("<login>", "<password>", "<domain>")
	# Specific credential Block END
	# Default credential Block
	$webclient.Credentials = [System.Net.CredentialCache]::DefaultCredentials   
	if($webclient.Proxy -ne $null)
	{        
		$webclient.Proxy.Credentials=[System.Net.CredentialCache]::DefaultNetworkCredentials    
	}
	else
	{       
		$webclient.UseDefaultCredentials = $true   
	}
	# Default credential Block END
	
	$response=$webclient.DownloadString($URL)
	
	if ($return -eq 0)
	{ 
		write-host "[$webenvironement] $webname --> OK"	| ConvertTo-html  -Body "<span style='color:green;'>[$webenvironement] $webname --> OK</span>" >> "$output"
	}
}

function SendReportMail{

	$smtpserveur = "<SMTPServer>"
	$from = "<FromEmail>"
	$to = "<ToEmail>"

	$message = new-object System.Net.Mail.MailMessage $from, $to
	$message.IsBodyHTML = $true
	$message.Subject = "Sharepoint Monitoring Report - $date"
	$message.Attachments.Add($output)
	$message.Body = Get-Content $output

	$client = new-object System.Net.Mail.SmtpClient $smtpserveur
	$client.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
	$client.Send($message)
	write-host "Done." -f Green
}

write-host ""
write-host "--------------------~Pre-Production Environment~--------------------" -ForeGroundColor Red | ConvertTo-html -Body "<span style='color:red; font-size: 18px'><b> -----Pre-Production Environment----- </b></span>" -Head $style | Out-String >> "$output"
write-host ""
write-host "---------- Services WFE ----------" -ForegroundColor Yellow
Get-Service -ComputerName <Computer> -DisplayName *SharePoint* | Select Status, Name, DisplayName | ConvertTo-html -Body "<br /><span style='color:blue; font-size: 16px'><b> -- Services WFE -- </b></span>" >> "$output"

write-host "---------- Services APP ----------" -ForegroundColor Yellow
write-host ""
Get-Service -ComputerName <Computer> -DisplayName *SharePoint* | Select Status, Name, DisplayName | ConvertTo-html -Body "<br /><span style='color:blue; font-size: 16px'><b> -- Services APP -- </b></span>" >> "$output"

write-host "---------- Services SQL ----------" -ForegroundColor Yellow
write-host ""
Get-Service -ComputerName <Computer> -DisplayName *SQL* | Select Status, Name, DisplayName | ConvertTo-html -Body "<br /><span style='color:blue; font-size: 16px'><b> -- Services SQL -- </b></span>" >> "$output"

write-host "---------- Status Databases ----------" -ForegroundColor Yellow
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | out-null
$SMOserver = New-Object ('Microsoft.SqlServer.Management.Smo.Server') -argumentlist rexpor-sql01-pprd
Get-SPContentDatabase | Select Name, @{Name="WebApp";Expression={($_.WebApplication.ToString().split("=")[1])}}, Status, @{Name="DBSize";Expression={"{0:N1}MB" -f($_.DiskSizeRequired/1mb)}}, @{Name="LogSize";Expression={"{0:N1}MB" -f(($SMOserver.Databases[$_.Name].logfiles | measure-object -property Size -sum).Sum/1024)}} | ConvertTo-html  -Body "<br /><span style='color:blue; font-size: 16px'><b> -- Status Databases -- </b></span>" >> "$output"

write-host "--------------------~Production Environment~--------------------" -ForeGroundColor Red | ConvertTo-html -Body "<br /><br /><span style='color:red; font-size: 18px'><b> -----Production Environment----- </b></span>" | Out-String >> "$output"
write-host ""
write-host "---------- Services WFE1 ----------" -ForegroundColor Yellow
write-host ""
Get-Service -ComputerName <Computer> -DisplayName *SharePoint* | Select Status, Name, DisplayName | ConvertTo-html -Body "<br /><span style='color:blue; font-size: 16px'><b> -- Services WFE1 -- </b></span>" >> "$output"

# write-host "---------- Sharepoint Database status ----------" -ForegroundColor Yellow
# [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | out-null
# $SMOserver = New-Object ('Microsoft.SqlServer.Management.Smo.Server') -argumentlist rexpor-sql01pro
# Get-SPContentDatabase | Select-Object Name, @{Name="WebApp";Expression={($_.WebApplication.ToString().split("=")[1])}}, Status, @{Name="DBSize";Expression={"{0:N1}MB" -f($_.DiskSizeRequired/1mb)}}, @{Name="LogSize";Expression={"{0:N1}MB" -f(($SMOserver.Databases[$_.Name].logfiles | measure-object -property Size -sum).Sum/1024)}} |
# Format-Table -auto | Out-String

write-host ""
write-host "--------------------~WebPages Testing~--------------------" | ConvertTo-html -Body "<br /><span style='color:blue; font-size: 16px'><b> -----WebPages Testing----- </b></span>" | Out-String >> "$output"
write-host ""
Select-Xml -Path "Webpages.xml" -Xpath "/config/webpages/web" | ForEach-Object { 
		$webname = $_.Node.name
		$weburl = $_.Node.url
		$webenvironement = $_.Node.environement
		CheckURL "$weburl"
}
SendReportMail

以上是关于powershell 监控SharePoint,本地服务器和SQL服务,页面并向某人发送报告电子邮件。的主要内容,如果未能解决你的问题,请参考以下文章

powershell SharePoint网站权限。正在进行的PowerShell项目,以分解sharepoint网站集和所有部分的所有权限

powershell Powershell用于将Powershell与SharePoint 2007一起使用

powershell SharePoint Powershell添加列表项

powershell Sharepoint Powershell Snippets

powershell 为SharePoint 2013创建SharePoint服务帐户(最低)

使用 Powershell 插入 Sharepoint 凭据