param(

  [System.IO.FileInfo]$file = $null,

  [string]$login = $null

);

 

$CODEPAGE = "iso-8859-1";

 

$url_upload = "http://url/filepost.php";

 

#--------------------------------------------------------------------------------------------

# function Get-EncodedDataFromFile

#--------------------------------------------------------------------------------------------

function Get-EncodedDataFromFile()

{

  param(

    [System.IO.FileInfo]$file = $null,

    [string]$codePageName = $CODEPAGE

  );

  

  $data = $null;

 

  if ( $file -and [System.IO.File]::Exists($file.FullName) )

  {

    $bytes = [System.IO.File]::ReadAllBytes($file.FullName);

    if ( $bytes )

    {

      $enc = [System.Text.Encoding]::GetEncoding($codePageName);

      $data = $enc.GetString($bytes);

    }

  }

  else

  {

    Write-Host "ERROR; File '$file' does not exist";

  }

  $data;

}

 

#--------------------------------------------------------------------------------------------

# function Get-ImageContentType

#--------------------------------------------------------------------------------------------

function Get-ImageContentType()

{

  param([System.IO.FileInfo]$file = $null);

  $contentType = $null;

  $contentTypeMap = @{

    ".jpg"  = "image/jpeg";

    ".jpeg" = "image/jpeg";

    ".gif"  = "image/gif";

    ".png"  = "image/png";

    ".tiff" = "image/tiff";

  }

  

  if ( $file )

  {

    $contentType = $contentTypeMap[$file.Extension.ToLower()];

  }

  $contentType;

}

 

#----------------------------------------------------------------------------

# function Execute-HTTPPostCommand

#----------------------------------------------------------------------------

function Execute-HTTPPostCommand()

{

  param(

    [string] $url = $null,

    [string] $data = $null,

    [System.Net.NetworkCredential]$credentials = $null,

    [string] $contentType = "application/x-www-form-urlencoded",

    [string] $codePageName = "UTF-8",

    [string] $userAgent = $null

  );

 

  if ( $url -and $data )

  {

    [System.Net.WebRequest]$webRequest = [System.Net.WebRequest]::Create($url);

    $webRequest.ServicePoint.Expect100Continue = $false;

    if ( $credentials )

    {

      $webRequest.Credentials = $credentials;

      $webRequest.PreAuthenticate = $true;

    }

    $webRequest.ContentType = $contentType;

    $webRequest.Method = "POST";

    if ( $userAgent )

    {

      $webRequest.UserAgent = $userAgent;

    }

    

    $enc = [System.Text.Encoding]::GetEncoding($codePageName);

    [byte[]]$bytes = $enc.GetBytes($data);

    $webRequest.ContentLength = $bytes.Length;

    [System.IO.Stream]$reqStream = $webRequest.GetRequestStream();

    $reqStream.Write($bytes, 0, $bytes.Length);

    $reqStream.Flush();

    

    $resp = $webRequest.GetResponse();

    $rs = $resp.GetResponseStream();

    [System.IO.StreamReader]$sr = New-Object System.IO.StreamReader -argumentList $rs;

    $sr.ReadToEnd();

  }

}

 

#--------------------------------------------------------------------------------------------

# function Post-TwitPic

#--------------------------------------------------------------------------------------------

function Post-GD()

{

  param

  (

    [System.IO.FileInfo]$file = $null,

    [string]$login = $null

  );

  

  if ( $file -and $login )

  {

    $boundary = [System.Guid]::NewGuid().ToString();

    $header = "--{0}" -f $boundary;

    $footer = "--{0}--" -f $boundary;

  

    [System.Text.StringBuilder]$contents = New-Object System.Text.StringBuilder;

    [void]$contents.AppendLine($header);

 

    $filedata = Get-EncodedDataFromFile -file $file -codePageName $CODEPAGE;

    if ( $filedata )

    {

      write-host $filedata.length

      $fileContentType = Get-ImageContentType -file $file;

      $fileHeader = "Content-Disposition: file; name=""{0}""; filename=""{1}""" -f "photo", $file.Name;

 

      [void]$contents.AppendLine($fileHeader);

      [void]$contents.AppendLine("Content-Type: {0}" -f $fileContentType);

      [void]$contents.AppendLine();

      [void]$contents.AppendLine($fileData);

 

      [void]$contents.AppendLine($header);

      [void]$contents.AppendLine("Content-Disposition: form-data; name=""cmd""");

      [void]$contents.AppendLine();

      [void]$contents.AppendLine("photoupdate");

      

      [void]$contents.AppendLine($header);

      [void]$contents.AppendLine("Content-Disposition: form-data; name=""up""");

      [void]$contents.AppendLine();

      [void]$contents.AppendLine("1");

 

      [void]$contents.AppendLine($header);

      [void]$contents.AppendLine("Content-Disposition: form-data; name=""login""");

      [void]$contents.AppendLine();

      [void]$contents.AppendLine($login);

      

      $url = $url_upload;

      

      [void]$contents.AppendLine($footer);

      

      $contents.ToString() > ".\out.txt";

      

      $postContentType = "multipart/form-data; boundary={0}" -f $boundary;

 

      $resp = Execute-HTTPPostcommand -url $url -data $contents.ToString() -contentType $postContentType -codePageName $CODEPAGE -userAgent "f.u.c.k";

      

      $resp

<#      

      [xml]$resp = Execute-HTTPPostcommand -url $url -data $contents.ToString() -contentType $postContentType -codePageName $CODEPAGE -userAgent "f.u.c.k";

      

      if ( $resp )

      {

        switch ($resp.rsp.stat)

        {

          "ok" {

            $obj = 1 | select mediaid, mediaurl;

            $obj.mediaid = $resp.rsp.mediaid;

            $obj.mediaurl = $resp.rsp.mediaurl;

            $obj;

          }

          "fail" {

            $errcode = $resp.rsp.err.code;

            $errmsg = $resp.rsp.err.msg;

            Write-Host "Post Error: Code = '$errcode'; Message = '$errmsg'";

          }

        }

      }

#>

 

    }

  }

  else

  {

    Write-Host "USAGE: Post-GD.ps1 -file file -login $login";

  }

}

 

Get-ChildItem -Path '\\fileserver\foto_upload$\*' -Include '*.jpg' | ForEach-Object {

  $basename = $_.basename

  $fn = $_.fullname

 

  $mb = get-mailbox "$basename"

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

  if ($cnt -eq 1){

     write-host $mb.samaccountname

     Post-GD -file $fn -login $mb.samaccountname;

     $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 ( $file -and $login)

{

  write-host "$file"

 

  Post-GD -file $file -login $login;

}

else

{

  Write-Host "USAGE: Post-GD.ps1 -file file -login $login";

}

 

#>