I'm trying to export a list of all users with no photo from our Exchange Online account using powershell. I cannot get it to work and have tried various methods.
Get-UserPhoto returns this exception when there is no profile present.
Microsoft.Exchange.Data.Storage.UserPhotoNotFoundException: There is no photo stored here.
First of all I tried use Errorvariable against the command but received:
A variable that cannot be referenced in restricted language mode or a Data section is being referenced. Variables that can be referenced include the following: $PSCulture, $PSUICulture, $true, $false, and $null.
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : VariableReferenceNotSupportedInDataSection
+ PSComputerName : outlook.office365.com
Next I tried try, catch but the non-terminating error never calls the catch despite various methods followed online about setting $ErrorActionPreference first of all.
Any ideas ? Here is the script:
$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session
$timestamp = $timestamp = get-date -Format "dd/M/yy-hh-mm"
$outfile = "c:\temp\" + $timestamp + "UserswithoutPhotos.txt"
$resultslist=@()
$userlist = get-user -ResultSize unlimited -RecipientTypeDetails usermailbox | where {$_.accountdisabled -ne $True}
Foreach($user in $userlist)
{
try
{
$user | get-userphoto -erroraction stop
}
catch
{
Write-Host "ERROR: $_"
$email= $user.userprincipalname
$name = $user.DisplayName
$office = $user.office
write-host "User photo not found...adding to list : $name , $email, $office"
$resultslist += $user
}
}
$resultslist | add-content $outfile
$resultslist
try
block, try adding$local:ErrorActionPreference = 'Stop'
– Perce$Error
variable, though, so you should be able to do:$Error.Clear(); command here; if ($Error) { }
– Perce$ErrorActionPreference
preference variable instance is unlikely to help, if common parameter-ErrorAction Stop
doesn't work (also, thelocal:
prefix wouldn't make a difference). Yes, non-terminating errors are reflected in$Error
too, as, in fact, are all errors - except if common parameter-ErrorAction Ignore
is passed to a cmdlet invocation (note, however, that'Ignore'
cannot be set as the value of$ErrorActionPreference
preference variable). – Unfit$local:
scope). I use that trick when calling methods usually so they throw errors in their try block, but don't impact the rest of the script. – Percefinally
block - however, setting it at least temporarily is needed (so I'm speculating) to get around script-module variable-scoping issues in this case. Do note thattry
doesn't create a separate scope - a$local:ErrorActionPreference = 'Stop'
inside atry
block still stays in effect for the rest of the enclosing function / script. – Unfittry
creates its own block scope (due to using a scriptblock syntax). Good to know, thanks! – Perce