Monday, March 2, 2015

Download Picture from UPS SharePoint Server

[system.reflection.assembly]::LoadWithPartialName("System.IO")
[system.reflection.assembly]::LoadWithPartialName("System.Web")


$SUrl = 'http://servername:8888/_vti_bin/userprofileservice.asmx?wsdl'
$filename = 'D:\UPSPictures\report01.csv'

$stream = [System.IO.StreamWriter] "D:\UPSPictures\reportsAll.csv"

function Download()
{
    trap [Exception]
        {
            Write-Output "-------------------------------Error"    
            Write-Output $_.Exception.Message
            Write-Output "-------------------------------Error"
            continue
        }

        $datel = Get-Date
        Write-Output "INFO: `t $datel `t Job Started"

        Write-Host "Set Path to Store Pictures" 
        $FilePath = "D:\UPSPictures"

        if($FilePath.EndsWith("\") -eq $false)
        {
          $FilePath = $FilePath + "\"
        }

        Write-Host "Enter the User Profile Web Service Url"
        #$SUrl = Read-Host

        Write-Host "Enter the User name & Password using which it needs to connect the service"
        #Create Credential Object
        $Credential = Get-Credential

        #Get Service
        #$Service = New-WebServiceProxy -Uri $SUrl -NameSpace UserProfileService -UseDefaultCredential
        $Service = New-WebServiceProxy -Uri $SUrl -NameSpace UserProfileService -Credential $Credential


        #Create WebRequest
        $WebRequest = new-object System.Net.WebClient
        #$WebRequest.Credentials = [System.Net.CredentialCache]::DefaultCredentials
        #$WebRequest.Credentials = new-object System.Net.NetworkCredential("palnisam","password","domain")
        $WebRequest.Credentials =  $Credential.GetNetworkCredential()


        #[IO.Directory]::SetCurrentDirectory((Convert-Path (Get-Location -PSProvider FileSystem)))
        #$Reader = new-object System.IO.StreamReader($filename)

        $ProfileCount = $Service.GetUserProfileCount()
        $ProfileLoop  = 0;
        $NextIndex = 358;
        for($ProfileLoop = 0;$ProfileLoop -le $ProfileCount;$ProfileLoop++)
        {
            trap [Exception]
            {
            }

            $UPD = $Service.GetUserProfileByIndex($NextIndex)
           
            #$AccountName = $UPD.UserProfile[1].Values[0].Value
           
            $Prop = $UPD.UserProfile | Where-Object { $_.Name -eq "AccountName" }
            $AccountName = $Prop.Values[0].Value

            #$PictureURL = $UPD.UserProfile[20].Values[0].Value
           
            $Prop = $UPD.UserProfile | Where-Object { $_.Name -eq "PictureURL" }
            $PictureURL = $Prop.Values[0].Value

            #if($PictureURL -ne $null)
            #{
              #$FileName = $FilePath +$AccountName.SubString($AccountName.LastIndexOf("\")+1) + $PictureUrl.SubString($PictureUrl.LastIndexOf("."))
              #$WebRequest.DownloadFile($PictureUrl,$FileName)
              $stream.WriteLine( "$AccountName ; $PictureURL" )
              #Write-Host "$ProfileLoop ; $AccountName ; $FILENAME; $PictureURL"
            #}

            $NextIndex = $UPD.NextValue

        }
    $stream.close()
}

Download

No comments:

Post a Comment