Link to home
Start Free TrialLog in
Avatar of Anthony K O365
Anthony K O365Flag for United States of America

asked on

Help in Running Powershell command - Export Profile Photos from Office 365

I get the error after running the command below: (I'm trying to export Profile Photos from Office 365)

At C:\Users\Get_Photos.ps1:7 char:55
+ if (Get-UserPhoto $objUser.UserPrincipalName -eq $null)
+                                                       ~
Missing statement block after if ( condition ).
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : MissingStatementBlock




$objUsers = ipcsv c:\PS\users.csv | select UserPrincipalName

Foreach ($objUser in $objUsers)
{
if (Get-UserPhoto $objUser.UserPrincipalName -eq $null)

write-host $objUser.UserPrincipalName

}

else

{$user = Get-UserPhoto $objUser.UserPrincipalName

$user.PictureData |Set-Content "C:\PS\Photo$($User.Identity).jpg" -Encoding byte

}
SOLUTION
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You're missing braces after your if else statements:
Foreach ($objUser in (Import-Csv c:\ps\users.csv)) {
  if (Get-UserPhoto $objUser.UserPrincipalName -eq $null) {
    write-host $objUser.UserPrincipalName
  }
}
else {
  $user = Get-UserPhoto $objUser.UserPrincipalName
  $user.PictureData |Set-Content "C:\PS\Photo$($User.Identity).jpg" -Encoding byte
}

Open in new window

Avatar of Anthony K O365

ASKER

here is what I get;

At C:\Users\katadmin\Desktop\O365items\Get_Photos.ps1:7 char:57
+ if ((Get-UserPhoto $objUser.UserPrincipalName) -eq $null)
+                                                         ~
Missing statement block after if ( condition ).
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : MissingStatementBlock
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Here is what I get:


A parameter cannot be found that matches parameter name 'eq'.
    + CategoryInfo          : InvalidArgument: (:) [Get-UserPhoto], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Get-UserPhoto
    + PSComputerName        : ps.outlook.com

else : The term 'else' is not recognized as the name of a cmdlet, function, script file, or operable program. Check
the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\Users\katadmin\Desktop\O365items\Get_Photos.ps1:6 char:1
+ else {
+ ~~~~
    + CategoryInfo          : ObjectNotFound: (else:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
Try this:

Foreach ($objUser in (Import-Csv c:\ps\users.csv)) {
  $photo = Get-UserPhoto $objUser.userprincipalname
  if ($photo -notlike '*') {
    write-host $objUser.UserPrincipalName
  }
}
else {
  $user = Get-UserPhoto $objUser.UserPrincipalName
  $user.PictureData |Set-Content "C:\PS\Photo$($User.Identity).jpg" -Encoding byte
}

Open in new window

Excellent! that worked, however, I now get this error: Any thoughts?


Cannot process argument transformation on parameter 'Identity'. Cannot convert value
"@{UserPrincipalName=JDoe@Domain.com}" to type "Microsoft.Exchange.Configuration.Tasks.MailboxIdParameter".
Error: "Cannot convert hashtable to an object of the following type:
Microsoft.Exchange.Configuration.Tasks.MailboxIdParameter. Hashtable-to-Object conversion is not supported in
restricted language mode or a Data section."
    + CategoryInfo          : InvalidData: (:) [Get-UserPhoto], ParameterBindin...mationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,Get-UserPhoto
    + PSComputerName        : ps.outlook.com
Ok maybe this then:

Foreach ($objUser in (Import-Csv c:\ps\users.csv)) {
  $photo = Get-UserPhoto $($objUser.userprincipalname)
  if ($photo -notlike '*') {
    write-host $($objUser.UserPrincipalName)
  }
}
else {
  $user = Get-UserPhoto $($objUser.UserPrincipalName)
  $user.PictureData | Set-Content "C:\PS\Photo$($User.Identity).jpg" -Encoding byte
}

Open in new window

The command runs, however it doesn't output any data probably b/c of the error statement below:



else : The term 'else' is not recognized as the name of a cmdlet, function, script file, or operable program. Check
the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\Users\get_photos5.ps1:7 char:1
+ else {
+ ~~~~
    + CategoryInfo          : ObjectNotFound: (else:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
I think I need to do further research on the Get-UserPhoto from Office 365. Here is the error I now get. Some of my users have Profile photos stored in Office 365, however I need a way to bypass errors if there is no photo found. You guys have been helpful! I will do further research on this command.

Thank you!


Error on proxy command 'Get-UserPhoto -Identity:'CN=Doe\, Jane,OU=Tenant.onmicrosoft.com,OU=Microsoft
Exchange Hosted Organizations,DC=NAMPR07A001,DC=prod,DC=outlook,DC=com'' to server
SN2PR07MB2655.namprd07.prod.outlook.com: Server version 15.01.1084.0000, Proxy method PSWS:
Cmdlet error with following error message:
Microsoft.Exchange.Data.Storage.UserPhotoNotFoundException:  There is no photo stored here.
   at Microsoft.Exchange.Configuration.Tasks.Task.ThrowError(Exception exception, ErrorCategory errorCategory, Object
target, String helpUrl)
   at Microsoft.Exchange.Configuration.Tasks.Task.WriteError(Exception exception, ErrorCategory category, Object
target, Boolean reThrow)
   at Microsoft.Exchange.Management.RecipientTasks.GetUserPhoto.ConvertDataObjectToPresentationObject(IConfigurable
dataObject)
   at Microsoft.Exchange.Configuration.Tasks.GetRecipientObjectTask`2.WriteResult(IConfigurable dataObject)
   at Microsoft.Exchange.Configuration.Tasks.GetTaskBase`1.WriteResult[T](IEnumerable`1 dataObjects)
   at Microsoft.Exchange.Configuration.Tasks.GetObjectWithIdentityTaskBase`2.InternalProcessRecord()
Excellent PS guidelines for a novice like me.

Thank you!