KPI Generation

# =================================================================================
# Description: Statistics of SharePoint (Sites, Size and Users)
# =================================================================================
Add-PsSnapin Microsoft.SharePoint.Powershell –ErrorAction SilentlyContinue
Clear-Host
Write-Host ("Start of Script.")

$WebApp  = "http://"  #URL WebApplication
$region  = "APAC"  #Region
$webApps  = "apac, cn, hk, in"  #WebApplication
$urlGroup  = "http://"  #URL group report to receive by email
$groupName  = "Team SharePoint APAC Statistics"  #Group name report to receive by email
$smtpServer = "intsmtp2.hk.hsbc"  #SMTP to send emails

$size = 0
$Users = 0
$Sites = 0
$Number = 0

function CountUsers(){
 Write-Host ("`nRead SQL database with users ") -NoNewline
 $SqlConnection = New-Object System.Data.SqlClient.SqlConnection
 $SqlConnection.ConnectionString = "Server=" + $site.ContentDatabase.Server + ";Integrated Security=True"
 $SqlConnection.Open()
 $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
 $query = "DECLARE @TableOut TABLE(tp_Login nvarchar(255))"
 foreach($database in Get-SPDatabase | where {$_.Type -eq "content database"}){
  Write-Host (".") -NoNewline
  $query += "INSERT INTO @TableOut(tp_Login)
       SELECT DISTINCT [tp_Login]
         FROM ["+$database.Name+"].[dbo].[UserInfo]
       WHERE tp_DomainGroup = 0 AND tp_IsActive = 1
          AND tp_ExternalTokenLastUpdated > DATEADD(month, -6, GETDATE())"
 }
 $SqlCmd.CommandText = $query +
 "SELECT COUNT(DISTINCT(tp_Login)) AS Qut_Usuarios_Ativos FROM @TableOut"
 $SqlCmd.Connection = $SqlConnection
 $Users = $SqlCmd.ExecuteScalar()
 $SqlConnection.Close()
 return $Users
}

#Count Users
 $site = Get-SPSite -Identity $WebApp
 $Users = CountUsers

#Count Size
 Get-SPSite -Limit All |ForEach-Object {$size=0}{ $size+=$_.Usage.Storage/1GB}
 $size = "{0:N3}" -f $size
  
#Site Count
 $Sites = (Get-SPSite -Limit all).Count
 
#Count Number of In-active sites (90 days)
 foreach ($TopSite in Get-SPSite -limit all ){
  $Days = ((get-date) - $TopSite.LastContentModifiedDate).days
  If ($Days -ge 90) {
  $Number++
  $output += write-host ($TopSite.url + " Days Since Last Modified " + $Days)
  }
 }


 

#Results
 Write-Host ("`n`nSize....: " + $size)
 Write-Host ("Sites...: " + $Sites)
 Write-Host ("Users...: " + $Users)
 Write-host ("Number of Inactive sites: " + $Number)

#--- Writes the list of statistics Group http://******/Lists/Statistics
 $uri = "*****/teamspace/_vti_bin/lists.asmx?WSDL"
 $listName = "Statistics"
 # Create the service
 $service = New-WebServiceProxy -Uri $uri -Namespace SpWs -UseDefaultCredential
 # Create xml query to retrieve list
 $xmlDoc = new-object System.Xml.XmlDocument
 $query = $xmlDoc.CreateElement("Query")
 $viewFields = $xmlDoc.CreateElement("ViewFields")
 $queryOptions = $xmlDoc.CreateElement("QueryOptions")
 $query.set_InnerXml("FieldRef Name='Full Name'")
 $rowLimit = "1000"
 $list = $null
 $service = $null
 try {
  $service = New-WebServiceProxy -Uri $uri -Namespace SpWs -UseDefaultCredential
 } catch {
  Write-Error $_ -ErrorAction:'SilentlyContinue'
 }
 # Get name attribute values (guids) for list and view
 $ndlistview = $service.getlistandview($listName, "")
 $strlistid = $ndlistview.childnodes.item(0).name
 $strviewid = $ndlistview.childnodes.item(1).name
 # Now, we use the service object to retrieve the list.
 if ( $service -ne $null ){
  try {
   $list = $service.GetListItems($strlistid, "", $query, $viewFields, $rowLimit, $queryOptions, "")
  } catch {
   Write-Error $_ -ErrorAction:'SilentlyContinue'
  }
 }
 # Create an xmldocument object and construct a batch element and its attributes.
 $xmldoc = new-object system.xml.xmldocument
 # note that an empty viewname parameter causes the method to use the default view
 $batchelement = $xmldoc.createelement("Batch") # Capital B
 $batchelement.setattribute("onerror", "continue")
 $batchelement.setattribute("listversion", "1")
 $batchelement.setattribute("viewname", $strviewid)

 [string]$monthGr = Get-Date -format MMM
 [string]$yearGr = Get-Date -format yyyy
 # Specify methods for the batch post using caml. to update or delete, specify the id of the item,
 # and to update or add, specify the value to place in the specified column
 $xml = "<Method ID='1' Cmd='New'>" +
        "<Field Name='ID'>1</Field>" +
    "<Field Name='Title'>" + $monthGr + " " + $yearGr + "</Field>" +
             "<Field Name='Region'>" + $region + "</Field>" +
             "<Field Name='Web_x0020_Applications'>" + $webApps + "</Field>" +
             "<Field Name='Active_x0020_Users'>" + $Users + "</Field>" +
             "<Field Name='Disk_x0020_Space'>" + ('{0:N0}' -f [int]$size) + "</Field>" +
             "<Field Name='Top_x0020_Level_x0020_Sites'>" + $Sites + "</Field>" +
     "</Method>"
 # Set the xml content
 $batchelement.innerxml = $xml
 $ndreturn = $null
 $ndreturn = $service.updatelistitems($strlistid, $batchelement)
 
 
# Send e-mail to URL and GROUP
 $smtp = new-object Net.Mail.SmtpClient($smtpServer)
 $msg = new-object Net.Mail.MailMessage
 $msg.From = "**@.hk"
 
 # List e-mails to destination
 $siteGroup = Get-SPSite($urlGroup)
 $groups = $siteGroup.RootWeb.SiteGroups
 foreach ( $grp in $groups ){
  if ( $grp.name -eq $groupName ){
   foreach ( $user in $grp.users ){
    $msg.To.Add($user.Email)
      }
  }
 }
 $siteGroup.Dispose()
 $msg.Subject = "Sharepoint APAC Statistics " + (Get-Date -Format "MMMM yyy")
 $msg.IsBodyHTML = $True
 $msg.Body  = "<p style='font-family:Sans-Serif, Tahoma, Arial; font-size:12px;'>" + $region + " statistics SharePoint automatically generated by the system on " + (Get-Date -Format "dd MMMM yyyy HH:mm") + ".</p>"
 $msg.Body += "<table cellpadding='4' cellspacing='0' style='font-family:Sans-Serif, Tahoma, Arial; font-size:12px;'>"
 $msg.Body += "<tr>"
 $msg.Body += "<td width='200' style='border-bottom:1px solid #CCCCCC;'>Total number of active users</td><td width='100' style='border-bottom:1px solid #CCCCCC;'><b>" + ('{0:N0}' -f [int]$Users) + "</b></td>"
 $msg.Body += "</tr><tr>"
 $msg.Body += "<td style='border-bottom:1px solid #CCCCCC;'>Total number of Site Collection</td><td style='border-bottom:1px solid #CCCCCC;'><b>" + ('{0:N0}' -f [int]$Sites) + "</b></td>"
 $msg.Body += "</tr><tr>"
 $msg.Body += "<td style='border-bottom:1px solid #CCCCCC;'>Total disk space usage</td><td style='border-bottom:1px solid #CCCCCC;'><b>" + ('{0:N0}' -f [int]$size) + " GB</b></td>"
 $msg.Body += "</tr><tr>"
 $msg.Body += "</tr><tr>"
 $msg.Body += "<td colspan='2'><a href='http:///Statistics/Year%202013.aspx'>Statistcs posted in the SharePoint Global</a></td>"
 $msg.Body += "</tr><tr>"
 $msg.Body += "<td colspan='2' style='font-size:14px;'><br><br><b>Teams<span style='color:#FF0000;'>Space</span></b> SharePoint APAC</td>"
 $msg.Body += "</tr><tr>"
 $msg.Body += "<td colspan='2' style='font-size:14px;'><a href='http://style='text-decoration:none; color:#696969;'>http://</a></td>"
 $msg.Body += "</tr><tr>"
 $msg.Body += "<td colspan='2' style='font-size:14px;'><a href=style='text-decoration:none; color:#696969;'>http://teams.</a></td>"
 $msg.Body += "</tr><tr>"
 $msg.Body += "<td colspan='2' style='font-size:14px;'><a href='httpc' style='text-decoration:none; color:#696969;'>http</a></td>"
 $msg.Body += "</tr><tr>"
 $msg.Body += "<td colspan='2' style='font-size:14px;'><a href=style='text-decoration:none; color:#696969;'>http://a></td>"
 $msg.Body += "</tr></table>"
 $smtp.Send($msg)
 
 
$site.Dispose()
Write-Host ("Final of Script.")


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值