janikvonrotz
11/14/2013 - 2:09 PM

PowerShell: Send Mail Report Of Unchecked SharePoint Files #SharePoint #PowerShell #EmbededPost

PowerShell: Send Mail Report Of Unchecked SharePoint Files #SharePoint #PowerShell #EmbededPost

<?xml version="1.0" encoding="utf-8"?>
<Content>

	<Metadata>
		<Title>Server Script Configuration File</Title>
		<Filename>vblw2k8sp01.script.config.xml</Filename>
		<Description></Description>
		<Tags>powershell, server, script, configuration</Tags>
		<Project></Project>
		<Author>Janik von Rotz</Author>
		<AuthorContact>http://janikvonrotz.ch</AuthorContact>
		<CreateDate>2013-08-14</CreateDate>
		<LastEditDate>2013-08-14</LastEditDate>
		<Version>1.0.0</Version>
	</Metadata>
	
	<Script Name="GlobalConfig">
	
		<SharePoint>
		
			<Mail From="noreply@sharepoint.domain.ch" SmtpServer="mail.domain.ch"></Mail>
		
			<SiteCollection Url="http://sharepoint.domain.ch" Feature="ReportUncheckedFiles"></SiteCollection>
			<SiteCollection Url="http://mysite.domain.ch" Feature=""></SiteCollection>
			
			<ADUserAndGroupSPAdministrators>F_Verantwortlicher SharePoint</ADUserAndGroupSPAdministrators>
			<ADUserAndGroupSPAdministrators>vukovic</ADUserAndGroupSPAdministrators>
			
		</SharePoint>
	
	</Script>

    
</Content>
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Date>2013-10-07T09:08:50.3896932</Date>
    <Author>Janik von Rotz(http://janikvonrotz.ch)</Author>
    <Description>Send Mail Report Of Unchecked SharePoint Files</Description>
  </RegistrationInfo>
  <Triggers>
	<CalendarTrigger>
      <StartBoundary>2000-01-01T21:00:00Z</StartBoundary>
      <Enabled>true</Enabled>
      <ScheduleByWeek>
        <DaysOfWeek>
          <Sunday />
        </DaysOfWeek>
        <WeeksInterval>1</WeeksInterval>
      </ScheduleByWeek>
    </CalendarTrigger>
  </Triggers>
  <Settings>
    <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
    <DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
    <AllowHardTerminate>true</AllowHardTerminate>
    <StartWhenAvailable>false</StartWhenAvailable>
    <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
    <IdleSettings>
      <StopOnIdleEnd>true</StopOnIdleEnd>
      <RestartOnIdle>false</RestartOnIdle>
    </IdleSettings>
    <AllowStartOnDemand>true</AllowStartOnDemand>
    <Enabled>true</Enabled>
    <Hidden>false</Hidden>
    <RunOnlyIfIdle>false</RunOnlyIfIdle>
    <WakeToRun>false</WakeToRun>
    <ExecutionTimeLimit>P1D</ExecutionTimeLimit>
    <Priority>7</Priority>
  </Settings>
  <Actions Context="Author">
    <Exec>
      <Command>$PSapps.PowerShell</Command>
      <Arguments>$('"&amp; '+"'$((Get-ChildItem -Path $PSscripts.Path -Filter "Send-MailReportOfUncheckedSharePointFiles.ps1" -Recurse).Fullname)'"+'"')</Arguments>
      <WorkingDirectory>$PSProfile.Path</WorkingDirectory>
    </Exec>
  </Actions>
</Task>
<#
$Metadata = @{
	Title = "Send Mail Report Of Unchecked SharePoint Files"
	Filename = "Send-MailReportOfUncheckedSharePointFiles.ps1"
	Description = ""
	Tags = ""
	Project = "powershell, script, sharepoint, report, unchecked, file"
	Author = "Janik von Rotz"
	AuthorContact = "http://janikvonrotz.ch"
	CreateDate = "2013-11-14"
	LastEditDate = "2013-01-13"
	Url = ""
	Version = "2.1.0"
	License = @'
This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 Switzerland License.
To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/ch/ or 
send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
'@
}
#>

try{

    #--------------------------------------------------#
    # modules
    #--------------------------------------------------#
    Add-Type -AssemblyName System.Web
    if((Get-PSSnapin 'Microsoft.SharePoint.PowerShell' -ErrorAction SilentlyContinue) -eq $null){Add-PSSnapin 'Microsoft.SharePoint.PowerShell'}
    Import-Module ActiveDirectory

    #--------------------------------------------------#
    # settings
    #--------------------------------------------------#
    $Config = Get-PPConfiguration
    $SPSiteFilter = $Config | %{$_.Content.Script.SharePoint.SiteCollection} | where{$_.Feature -contains "ReportUncheckedFiles"} | %{$_.Url}
    $MailConfig = $Config | %{$_.Content.Script.SharePoint.Mail} | select -First 1 

    #--------------------------------------------------#
    # main
    #--------------------------------------------------#
    
    # get unchecked files
    $UncheckedFilesByEmail = Get-SPSite | where{$SPSiteFilter -contains $_.Url} | Get-SPWeb -Limit All | %{
        $SPWeb = $_
        $_.lists | %{
            $_.CheckedOutFiles | %{           
                $_ | select *, @{L="FileUrl";E={$SPWeb.Site.Url + "/" + $_.Url}}, @{L="SiteUrl";E={($SPWeb.Site.Url + "/" + $_.Url) -replace "[^/]+$",""}}        
            }
        }
    } | sort CheckedOutByEmail
    
    # create mails for owner
    $UncheckedFilesByEmail | Group-Object CheckedOutByEmail | %{

        $Subject = "$($_.Count) nicht eingecheckte Dateien auf dem SharePoint"
        
        $BodyTable = $($_.group | select @{L="Name";E={$_.LeafName}}, @{L="FileUrl";E={"<a href='$($_.FileUrl)'>$($_.FileUrl)</a>"}}, @{L="SiteUrl";E={"<a href='$($_.SiteUrl)'>$($_.SiteUrl)</a>"}}, TimeLastModified, @{L="Size";E={Format-FileSize $_.Length}} | where{$_.TimeLastModified -lt $(Get-Date).AddDays(-7)})
        
        if($BodyTable){
        
            $Body = @"    
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <style>
    body{
        font-size: 11pt; 
        font-family: Calibri
    }
    table { 
        margin: 1em;
        border-collapse: collapse; 
    }
    td, th {
        margin: 0.3em; 
        border: 1px #ccc solid; 
    }
</style></head><body>

    <p>Guten Tag $($_.Group[0].CheckedOutByName)</p>

    <p>Sie erhalten diese E-Mail, weil Sie im Besitz von nicht eingecheckten Dateien sind,</br>
    welche als letztes vor einer Woche oder früher bearbeitet worden sind.</p>

    <p>Diese Dateien sind für andere weder bearbeitbar noch sichtbar, da diese noch nie eingecheckt worden sind.</br>
	Das Ein- und Aus-Checken gehört zum SharePoint Dokument Bearbeitungsprozess</p>
	
    <p>Wir bitten Sie diese Dateien einzuchecken oder zu löschen.</p>

    <p>Untersützung zu diesem Thema erhalten Sie <a href="http://office.microsoft.com/de-ch/sharepoint-workspace-help/auschecken-und-einchecken-von-dokumenten-in-ein-dateitool-HA010356922.aspx">hier</a>.</p>

    <h2>Übersicht Ihrer nicht eingecheckten Dateien</h2>

    $($BodyTable | ConvertTo-Html -Fragment)

    <p>ACHTUNG! Dieses E-Mail wurde von einem unbeaufsichtigtem Konto verschickt, Antworten an den Sender dieser E-Mail werden nicht bearbeitet.</p>

</body></html>
"@       
        
            Write-PPEventLog -Message "Send Mail to: $($_.Name) with subject: $Subject" -Source "Send Mail Report Of Unchecked SharePoint Files" -WriteMessage
            Send-MailMessage -To $_.Name -From $MailConfig.From -Subject $Subject -Body ([System.Web.HttpUtility]::HtmlDecode($Body)) -SmtpServer $MailConfig.SmtpServer -BodyAsHtml -Priority High -Encoding ([System.Text.Encoding]::UTF8)   
        }        
    }
        
        # send mail report to sharepoint administrator
    $Config | %{$_.Content.Script.SharePoint.ADUserAndGroupSPAdministrators} | where{$_} | %{
    
        Get-ADObject -Filter {Name -eq $_} | %{
        
            if($_.ObjectClass -eq "user"){
                
                Get-ADUser $_.DistinguishedName -Properties Mail, DisplayName
                
            }elseif($_.ObjectClass -eq "group"){
            
                Get-ADGroupMember $_.DistinguishedName -Recursive | Get-ADUser -Properties Mail, DisplayName
            }
        }
    } | %{
        
        $Subject = "$($UncheckedFilesByEmail.Count) nicht eingecheckte Dateien auf dem SharePoint"
    
        $BodyTable = $($UncheckedFilesByEmail | select @{L="Name";E={$_.LeafName}}, @{L="FileUrl";E={"<a href='$($_.FileUrl)'>$($_.FileUrl)</a>"}}, @{L="Owner";E={$_.CheckedOutByEmail}}, @{L="SiteUrl";E={"<a href='$($_.SiteUrl)'>$($_.SiteUrl)</a>"}}, TimeLastModified, @{L="Size";E={Format-FileSize $_.Length}})
    
        if($BodyTable){
        
                $Body = @"    
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <style>
    body{
        font-size: 11pt; 
        font-family: Calibri
    }
    table { 
        margin: 1em;
        border-collapse: collapse; 
    }
    td, th {
        margin: 0.3em; 
        border: 1px #ccc solid; 
    }
</style></head><body>

    <p>Guten Tag $($_.DisplayName)</p>

    <p>Sie erhalten diese E-Mail zur Übersicht von nicht eingecheckten Dateien.</p>

    <p>Diese Dateien sind für andere weder bearbeitbar noch sichtbar, da diese noch nie eingecheckt worden sind.</br>
	Das Ein- und Aus-Checken gehört zum SharePoint Dokument Bearbeitungsprozess</p>
	
    <h2>Übersicht Ihrer nicht eingecheckten Dateien</h2>

    $($BodyTable | ConvertTo-Html -Fragment)

    <p>ACHTUNG! Dieses E-Mail wurde von einem unbeaufsichtigtem Konto verschickt, Antworten an den Sender dieser E-Mail werden nicht bearbeitet.</p>

</body></html>
"@
        
            Write-PPEventLog -Message "Send Mail to: $($_.Mail) with subject: $Subject" -Source "Send Mail Report Of Unchecked SharePoint Files" -WriteMessage
            Send-MailMessage -To $_.Mail -From $MailConfig.From -Subject $Subject -Body ([System.Web.HttpUtility]::HtmlDecode($Body)) -SmtpServer $MailConfig.SmtpServer -BodyAsHtml -Priority High -Encoding ([System.Text.Encoding]::UTF8) 
        }
    }
}catch{
    
    Write-PPErrorEventLog -Source "Send Mail Report Of Unchecked SharePoint Files" -ClearErrorVariable
}