Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I am using following powershell (that I got from here http://spsatheesh.wordpress.com/2011/06/03/download-all-the-profile-pictures-through-powershell-using-web-service/)so I can download all the profile pictures. The script runs and displays index number and the profile id (with domain) on the screen. however, profile pictures are not saved.

= = = script = = =

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


$SUrl       = 'https://yourcompany.com/_vti_bin/userprofileservice.asmx?wsdl'
$filename   = 'report.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 = Read-Host

        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
            $PictureURL = $UPD.UserProfile[20].Values[0].Value
            if($PictureURL -ne $null)
            {
              $FileName = $FilePath +$AccountName.SubString($AccountName.LastIndexOf("\")+1) + $PictureUrl.SubString($PictureUrl.LastIndexOf("."))
              $WebRequest.DownloadFile($PictureUrl,$FileName)
            }

            $NextIndex = $UPD.NextValue
            Write-Host "$ProfileLoop ----- $AccountName"
        }

}
share|improve this question
    
This is really a powershell question not a SharePoint question, but are you certain that $PictureURL is even being populated? –  Dave Wise Feb 15 '12 at 21:55
    
Is there a reason you are using the User Profile Web Service rather than the SharePoint API? When I run your code I don't get values in PictureUrl, I suspect it is because you are hardcoding the index at 20. –  Matthew McDermott Feb 15 '12 at 22:24

1 Answer 1

up vote 1 down vote accepted

OK, I got your code working for me. You should change the property getter to look like the following, I left in the original lines for reference:

#$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

I am sure there is a better way in PowerShell, but this should work for you.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.