#Import-Module ActiveDirectory

#add-pssnapin Microsoft.Exchange.Management.PowerShell.E2010

. 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'

Connect-ExchangeServer -auto

Function Set-ImageSize

{

    <#

.SYNOPSIS

   Resize image file.

 

.DESCRIPTION

   The Set-ImageSize cmdlet to set new size of image file.

 

.PARAMETER Image

   Specifies an image file. 

 

.PARAMETER Destination

   Specifies a destination of resized file. Default is current location (Get-Location).

 

.PARAMETER WidthPx

   Specifies a width of image in px. 

 

.PARAMETER HeightPx

   Specifies a height of image in px.

 

.PARAMETER Overwrite

   Specifies a destination exist then overwrite it without prompt. 

 

.PARAMETER FixedSize

   Set fixed size and do not try to scale the aspect ratio. 

 

.PARAMETER RemoveSource

   Remove source file after conversion. 

x

.EXAMPLE

PS C:\> Get-ChildItem 'P:\test\*.jpg' | Set-ImageSize -Destination "p:\test2" -WidthPx 300 -HeightPx 375 -Verbose

VERBOSE: Image 'P:\test\00001.jpg' was resize from 236x295 to 300x375 and save in 'p:\test2\00001.jpg'

VERBOSE: Image 'P:\test\00002.jpg' was resize from 236x295 to 300x375 and save in 'p:\test2\00002.jpg'

VERBOSE: Image 'P:\test\00003.jpg' was resize from 236x295 to 300x375 and save in 'p:\test2\00003.jpg'

 

.NOTES

Author: Michal Gajda

Blog  : http://commandlinegeeks.com/

#>

[CmdletBinding(

    SupportsShouldProcess=$True,

        ConfirmImpact="Low"

    )]

Param

(

[parameter(Mandatory=$true,

ValueFromPipeline=$true,

ValueFromPipelineByPropertyName=$true)]

[Alias("Image")]

[String[]]$FullName,

[String]$Destination = $(Get-Location),

[Switch]$Overwrite,

[parameter(Mandatory=$true)]

[Int]$WidthPx,

[parameter(Mandatory=$true)]

[Int]$HeightPx,

[Switch]$FixedSize,

[Switch]$RemoveSource

)

 

Begin

{

[void][reflection.assembly]::LoadWithPartialName("System.Windows.Forms")

}

 

Process

{

 

Foreach($ImageFile in $FullName)

{

If(Test-Path $ImageFile)

{

$OldImage = new-object System.Drawing.Bitmap $ImageFile

$OldWidth = $OldImage.Width

$OldHeight = $OldImage.Height

 

if($FixedSize)

{

$NewWidth = $WidthPx

$NewHeight = $HeightPx

}

else

{

if($OldWidth -lt $OldHeight)

{

$NewWidth = $WidthPx

[int]$NewHeight = [Math]::Round(($NewWidth*$OldHeight)/$OldWidth)

 

if($NewHeight -gt $HeightPx)

{

$NewHeight = $HeightPx

[int]$NewWidth = [Math]::Round(($NewHeight*$OldWidth)/$OldHeight)

}

}

else

{

$NewHeight = $HeightPx

[int]$NewWidth = [Math]::Round(($NewHeight*$OldWidth)/$OldHeight)

 

if($NewWidth -gt $WidthPx)

{

$NewWidth = $WidthPx

[int]$NewHeight = [Math]::Round(($NewWidth*$OldHeight)/$OldWidth)

}

}

}

 

$ImageProperty = Get-ItemProperty $ImageFile

$SaveLocation = Join-Path -Path $Destination -ChildPath ($ImageProperty.Name)

 

If(!$Overwrite)

{

If(Test-Path $SaveLocation)

{

#$Title = "A file already exists: $SaveLocation"

 

"A file already exists: $SaveLocation"

 

#$ChoiceOverwrite = New-Object System.Management.Automation.Host.ChoiceDescription "&Overwrite"

#$ChoiceCancel = New-Object System.Management.Automation.Host.ChoiceDescription "&Cancel"

#$Options = [System.Management.Automation.Host.ChoiceDescription[]]($ChoiceCancel, $ChoiceOverwrite)

#If(($host.ui.PromptForChoice($Title, $null, $Options, 1)) -eq 0)

#{

#Write-Verbose "Image '$ImageFile' exist in destination location - skiped"

Continue

#} #End If ($host.ui.PromptForChoice($Title, $null, $Options, 1)) -eq 0

} #End If Test-Path $SaveLocation

} #End If !$Overwrite

 

$NewImage = new-object System.Drawing.Bitmap $NewWidth,$NewHeight

 

$Graphics = [System.Drawing.Graphics]::FromImage($NewImage)

$Graphics.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic

$Graphics.DrawImage($OldImage, 0, 0, $NewWidth, $NewHeight) 

 

$ImageFormat = $OldImage.RawFormat

$OldImage.Dispose()

$NewImage.Save($SaveLocation,$ImageFormat)

$NewImage.Dispose()

Write-Verbose "Image '$ImageFile' was resize from $($OldWidth)x$($OldHeight) to $($NewWidth)x$($NewHeight) and save in '$SaveLocation'"

 

If($RemoveSource)

{

Remove-Item $Image -Force

Write-Verbose "Image source '$ImageFile' was removed"

} #End If $RemoveSource

}

}

 

} #End Process

 

End{}

}

$i = 1

 

Get-ChildItem -Path 'source_dir\*' -Include '*.jpg' | ForEach-Object {

  $basename = $_.basename

  $dir  = $_.directoryname + "/small/"

  $path0 = $_.directoryname + "/small/" + $_.name

  $path = $_.directoryname + "/small/" + $_.basename + "_small.jpg"

  #$path

  if(test-path$path){

    Write-Warning "User '$basename' photo exist - skiped"

#    continue

  }

  else {

    Set-ImageSize $_ -Destination "$dir" -WidthPx 96 -HeightPx 96 -Verbose

    move $path0 $path

    $mb = get-mailbox "$basename"

    $cnt = ($mb | measure-object).count

    if ($cnt -eq 1){

       "$basename" >> log_mb_add.txt

       Import-RecipientDataProperty -Identity "$basename" -Picture -FileData ([Byte[]]$(Get-Content -Path "$path" -Encoding Byte -ReadCount 0))

       $emailbody += "$i. $basename<br>"

       $i++

    }

    else{

      write-host "WARNING : $cnt почтовых ящика(ов) : $basename"  -foregroundcolor "Red" #>> log_mb_double.txt

      $emailbody += "$i. <font color=red>WARNING : $cnt почтовых ящика(ов) : $basename</font><br>"

      $i++

    }

  }

}

 

if (($emailbody).length -gt 10){

  $EmailTo = "my mail"

  $mailmessage = New-Object system.net.mail.mailmessage 

  $mailmessage.from = ("from mail") 

  $mailmessage.To.add($emailto)

  $mailmessage.Subject = "Обновлены фотографии следующих пользвователей " + (Get-Date).GetDateTimeFormats("u")

  $mailmessage.Body = "<html><head></head><body>" +   $emailbody + "</body></html>"

  $mailmessage.IsBodyHTML =  $true

  $SMTPClient = New-Object Net.Mail.SmtpClient("192.168.11.135", 25)  

  $SMTPClient.Send($mailmessage)

}