Exchange Online - Get-UserPhoto - how to catch non terminating error?
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
powershell office365 exchange-server
|
show 2 more comments
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
powershell office365 exchange-server
In yourtryblock, try adding$local:ErrorActionPreference = 'Stop'
– TheIncorrigible1
May 11 '18 at 19:15
According to the documentation the cmdlet should support common parameters. I believe even non-terminating errors get added to the$Errorvariable, though, so you should be able to do:$Error.Clear(); command here; if ($Error) { }
– TheIncorrigible1
May 11 '18 at 19:19
@TheIncorrigible1: A local$ErrorActionPreferencepreference variable instance is unlikely to help, if common parameter-ErrorAction Stopdoesn't work (also, thelocal:prefix wouldn't make a difference). Yes, non-terminating errors are reflected in$Errortoo, as, in fact, are all errors - except if common parameter-ErrorAction Ignoreis passed to a cmdlet invocation (note, however, that'Ignore'cannot be set as the value of$ErrorActionPreferencepreference variable).
– mklement0
May 11 '18 at 20:49
1
@TheIncorrigible1: Re global scope: that's sensible - that's why in my answer I restore the previous value in afinallyblock - 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 thattrydoesn't create a separate scope - a$local:ErrorActionPreference = 'Stop'inside atryblock still stays in effect for the rest of the enclosing function / script.
– mklement0
May 11 '18 at 20:56
1
@mklement0 I had thoughttrycreates its own block scope (due to using a scriptblock syntax). Good to know, thanks!
– TheIncorrigible1
May 11 '18 at 20:57
|
show 2 more comments
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
powershell office365 exchange-server
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
powershell office365 exchange-server
powershell office365 exchange-server
asked May 11 '18 at 19:11
JacksporrowJacksporrow
13214
13214
In yourtryblock, try adding$local:ErrorActionPreference = 'Stop'
– TheIncorrigible1
May 11 '18 at 19:15
According to the documentation the cmdlet should support common parameters. I believe even non-terminating errors get added to the$Errorvariable, though, so you should be able to do:$Error.Clear(); command here; if ($Error) { }
– TheIncorrigible1
May 11 '18 at 19:19
@TheIncorrigible1: A local$ErrorActionPreferencepreference variable instance is unlikely to help, if common parameter-ErrorAction Stopdoesn't work (also, thelocal:prefix wouldn't make a difference). Yes, non-terminating errors are reflected in$Errortoo, as, in fact, are all errors - except if common parameter-ErrorAction Ignoreis passed to a cmdlet invocation (note, however, that'Ignore'cannot be set as the value of$ErrorActionPreferencepreference variable).
– mklement0
May 11 '18 at 20:49
1
@TheIncorrigible1: Re global scope: that's sensible - that's why in my answer I restore the previous value in afinallyblock - 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 thattrydoesn't create a separate scope - a$local:ErrorActionPreference = 'Stop'inside atryblock still stays in effect for the rest of the enclosing function / script.
– mklement0
May 11 '18 at 20:56
1
@mklement0 I had thoughttrycreates its own block scope (due to using a scriptblock syntax). Good to know, thanks!
– TheIncorrigible1
May 11 '18 at 20:57
|
show 2 more comments
In yourtryblock, try adding$local:ErrorActionPreference = 'Stop'
– TheIncorrigible1
May 11 '18 at 19:15
According to the documentation the cmdlet should support common parameters. I believe even non-terminating errors get added to the$Errorvariable, though, so you should be able to do:$Error.Clear(); command here; if ($Error) { }
– TheIncorrigible1
May 11 '18 at 19:19
@TheIncorrigible1: A local$ErrorActionPreferencepreference variable instance is unlikely to help, if common parameter-ErrorAction Stopdoesn't work (also, thelocal:prefix wouldn't make a difference). Yes, non-terminating errors are reflected in$Errortoo, as, in fact, are all errors - except if common parameter-ErrorAction Ignoreis passed to a cmdlet invocation (note, however, that'Ignore'cannot be set as the value of$ErrorActionPreferencepreference variable).
– mklement0
May 11 '18 at 20:49
1
@TheIncorrigible1: Re global scope: that's sensible - that's why in my answer I restore the previous value in afinallyblock - 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 thattrydoesn't create a separate scope - a$local:ErrorActionPreference = 'Stop'inside atryblock still stays in effect for the rest of the enclosing function / script.
– mklement0
May 11 '18 at 20:56
1
@mklement0 I had thoughttrycreates its own block scope (due to using a scriptblock syntax). Good to know, thanks!
– TheIncorrigible1
May 11 '18 at 20:57
In your
try block, try adding $local:ErrorActionPreference = 'Stop'– TheIncorrigible1
May 11 '18 at 19:15
In your
try block, try adding $local:ErrorActionPreference = 'Stop'– TheIncorrigible1
May 11 '18 at 19:15
According to the documentation the cmdlet should support common parameters. I believe even non-terminating errors get added to the
$Error variable, though, so you should be able to do: $Error.Clear(); command here; if ($Error) { }– TheIncorrigible1
May 11 '18 at 19:19
According to the documentation the cmdlet should support common parameters. I believe even non-terminating errors get added to the
$Error variable, though, so you should be able to do: $Error.Clear(); command here; if ($Error) { }– TheIncorrigible1
May 11 '18 at 19:19
@TheIncorrigible1: A local
$ErrorActionPreference preference variable instance is unlikely to help, if common parameter -ErrorAction Stop doesn't work (also, the local: 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).– mklement0
May 11 '18 at 20:49
@TheIncorrigible1: A local
$ErrorActionPreference preference variable instance is unlikely to help, if common parameter -ErrorAction Stop doesn't work (also, the local: 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).– mklement0
May 11 '18 at 20:49
1
1
@TheIncorrigible1: Re global scope: that's sensible - that's why in my answer I restore the previous value in a
finally 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 that try doesn't create a separate scope - a $local:ErrorActionPreference = 'Stop' inside a try block still stays in effect for the rest of the enclosing function / script.– mklement0
May 11 '18 at 20:56
@TheIncorrigible1: Re global scope: that's sensible - that's why in my answer I restore the previous value in a
finally 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 that try doesn't create a separate scope - a $local:ErrorActionPreference = 'Stop' inside a try block still stays in effect for the rest of the enclosing function / script.– mklement0
May 11 '18 at 20:56
1
1
@mklement0 I had thought
try creates its own block scope (due to using a scriptblock syntax). Good to know, thanks!– TheIncorrigible1
May 11 '18 at 20:57
@mklement0 I had thought
try creates its own block scope (due to using a scriptblock syntax). Good to know, thanks!– TheIncorrigible1
May 11 '18 at 20:57
|
show 2 more comments
2 Answers
2
active
oldest
votes
PowerShell's error handling is notoriously tricky, especially so with cmdlets that use implicit remoting via a locally generated proxy script module.
The following idiom provides a workaround based on temporarily setting $ErrorActionPreference to Stop globally (of course, you may move the code for restoring the previous value outside the foreach loop), which ensures that even functions from different modules see it:
try {
# Temporarily set $ErrorActionPreference to 'Stop' *globally*
$prevErrorActionPreference = $global:ErrorActionPreference
$global:ErrorActionPreference = 'Stop'
$user | get-userphoto
} 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
} finally {
# Restore the previous global $ErrorActionPreference value
$global:ErrorActionPreference = $prevErrorActionPreference
}
As for why this is necessary:
Functions defined in modules do not see the caller's preference variables - unlike cmdlets, which do.
The only outside scope that functions in modules see is the global scope.
For more information on this fundamental problem, see this GitHub issue.
@Jacksporrow: My pleasure; glad it was helpful.
– mklement0
May 14 '18 at 18:52
add a comment |
You can throw your own error like so:
try {
$error.Clear()
$user | Get-UserPhoto
if ($error[0].CategoryInfo.Reason -eq "UserPhotoNotFoundException") {throw "UserPhotoNotFoundException" }
} catch {
#code
}
That should work. However, so as not to wipe out the session's$Errorcollection, perhaps it's better to compare the entry count after with the one before to determine if an error occurred. Also, you can just rethrow the (wrapped) exception (with all metadata) withThrow $Error[0].
– mklement0
May 11 '18 at 19:56
I couldn't get this one to work , it wasn't catching . Thanks for your efforts in helping me though
– Jacksporrow
May 14 '18 at 18:07
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f50298817%2fexchange-online-get-userphoto-how-to-catch-non-terminating-error%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
PowerShell's error handling is notoriously tricky, especially so with cmdlets that use implicit remoting via a locally generated proxy script module.
The following idiom provides a workaround based on temporarily setting $ErrorActionPreference to Stop globally (of course, you may move the code for restoring the previous value outside the foreach loop), which ensures that even functions from different modules see it:
try {
# Temporarily set $ErrorActionPreference to 'Stop' *globally*
$prevErrorActionPreference = $global:ErrorActionPreference
$global:ErrorActionPreference = 'Stop'
$user | get-userphoto
} 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
} finally {
# Restore the previous global $ErrorActionPreference value
$global:ErrorActionPreference = $prevErrorActionPreference
}
As for why this is necessary:
Functions defined in modules do not see the caller's preference variables - unlike cmdlets, which do.
The only outside scope that functions in modules see is the global scope.
For more information on this fundamental problem, see this GitHub issue.
@Jacksporrow: My pleasure; glad it was helpful.
– mklement0
May 14 '18 at 18:52
add a comment |
PowerShell's error handling is notoriously tricky, especially so with cmdlets that use implicit remoting via a locally generated proxy script module.
The following idiom provides a workaround based on temporarily setting $ErrorActionPreference to Stop globally (of course, you may move the code for restoring the previous value outside the foreach loop), which ensures that even functions from different modules see it:
try {
# Temporarily set $ErrorActionPreference to 'Stop' *globally*
$prevErrorActionPreference = $global:ErrorActionPreference
$global:ErrorActionPreference = 'Stop'
$user | get-userphoto
} 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
} finally {
# Restore the previous global $ErrorActionPreference value
$global:ErrorActionPreference = $prevErrorActionPreference
}
As for why this is necessary:
Functions defined in modules do not see the caller's preference variables - unlike cmdlets, which do.
The only outside scope that functions in modules see is the global scope.
For more information on this fundamental problem, see this GitHub issue.
@Jacksporrow: My pleasure; glad it was helpful.
– mklement0
May 14 '18 at 18:52
add a comment |
PowerShell's error handling is notoriously tricky, especially so with cmdlets that use implicit remoting via a locally generated proxy script module.
The following idiom provides a workaround based on temporarily setting $ErrorActionPreference to Stop globally (of course, you may move the code for restoring the previous value outside the foreach loop), which ensures that even functions from different modules see it:
try {
# Temporarily set $ErrorActionPreference to 'Stop' *globally*
$prevErrorActionPreference = $global:ErrorActionPreference
$global:ErrorActionPreference = 'Stop'
$user | get-userphoto
} 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
} finally {
# Restore the previous global $ErrorActionPreference value
$global:ErrorActionPreference = $prevErrorActionPreference
}
As for why this is necessary:
Functions defined in modules do not see the caller's preference variables - unlike cmdlets, which do.
The only outside scope that functions in modules see is the global scope.
For more information on this fundamental problem, see this GitHub issue.
PowerShell's error handling is notoriously tricky, especially so with cmdlets that use implicit remoting via a locally generated proxy script module.
The following idiom provides a workaround based on temporarily setting $ErrorActionPreference to Stop globally (of course, you may move the code for restoring the previous value outside the foreach loop), which ensures that even functions from different modules see it:
try {
# Temporarily set $ErrorActionPreference to 'Stop' *globally*
$prevErrorActionPreference = $global:ErrorActionPreference
$global:ErrorActionPreference = 'Stop'
$user | get-userphoto
} 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
} finally {
# Restore the previous global $ErrorActionPreference value
$global:ErrorActionPreference = $prevErrorActionPreference
}
As for why this is necessary:
Functions defined in modules do not see the caller's preference variables - unlike cmdlets, which do.
The only outside scope that functions in modules see is the global scope.
For more information on this fundamental problem, see this GitHub issue.
edited Nov 13 '18 at 19:48
answered May 11 '18 at 19:37
mklement0mklement0
128k20241270
128k20241270
@Jacksporrow: My pleasure; glad it was helpful.
– mklement0
May 14 '18 at 18:52
add a comment |
@Jacksporrow: My pleasure; glad it was helpful.
– mklement0
May 14 '18 at 18:52
@Jacksporrow: My pleasure; glad it was helpful.
– mklement0
May 14 '18 at 18:52
@Jacksporrow: My pleasure; glad it was helpful.
– mklement0
May 14 '18 at 18:52
add a comment |
You can throw your own error like so:
try {
$error.Clear()
$user | Get-UserPhoto
if ($error[0].CategoryInfo.Reason -eq "UserPhotoNotFoundException") {throw "UserPhotoNotFoundException" }
} catch {
#code
}
That should work. However, so as not to wipe out the session's$Errorcollection, perhaps it's better to compare the entry count after with the one before to determine if an error occurred. Also, you can just rethrow the (wrapped) exception (with all metadata) withThrow $Error[0].
– mklement0
May 11 '18 at 19:56
I couldn't get this one to work , it wasn't catching . Thanks for your efforts in helping me though
– Jacksporrow
May 14 '18 at 18:07
add a comment |
You can throw your own error like so:
try {
$error.Clear()
$user | Get-UserPhoto
if ($error[0].CategoryInfo.Reason -eq "UserPhotoNotFoundException") {throw "UserPhotoNotFoundException" }
} catch {
#code
}
That should work. However, so as not to wipe out the session's$Errorcollection, perhaps it's better to compare the entry count after with the one before to determine if an error occurred. Also, you can just rethrow the (wrapped) exception (with all metadata) withThrow $Error[0].
– mklement0
May 11 '18 at 19:56
I couldn't get this one to work , it wasn't catching . Thanks for your efforts in helping me though
– Jacksporrow
May 14 '18 at 18:07
add a comment |
You can throw your own error like so:
try {
$error.Clear()
$user | Get-UserPhoto
if ($error[0].CategoryInfo.Reason -eq "UserPhotoNotFoundException") {throw "UserPhotoNotFoundException" }
} catch {
#code
}
You can throw your own error like so:
try {
$error.Clear()
$user | Get-UserPhoto
if ($error[0].CategoryInfo.Reason -eq "UserPhotoNotFoundException") {throw "UserPhotoNotFoundException" }
} catch {
#code
}
answered May 11 '18 at 19:25
langstromlangstrom
1,341813
1,341813
That should work. However, so as not to wipe out the session's$Errorcollection, perhaps it's better to compare the entry count after with the one before to determine if an error occurred. Also, you can just rethrow the (wrapped) exception (with all metadata) withThrow $Error[0].
– mklement0
May 11 '18 at 19:56
I couldn't get this one to work , it wasn't catching . Thanks for your efforts in helping me though
– Jacksporrow
May 14 '18 at 18:07
add a comment |
That should work. However, so as not to wipe out the session's$Errorcollection, perhaps it's better to compare the entry count after with the one before to determine if an error occurred. Also, you can just rethrow the (wrapped) exception (with all metadata) withThrow $Error[0].
– mklement0
May 11 '18 at 19:56
I couldn't get this one to work , it wasn't catching . Thanks for your efforts in helping me though
– Jacksporrow
May 14 '18 at 18:07
That should work. However, so as not to wipe out the session's
$Error collection, perhaps it's better to compare the entry count after with the one before to determine if an error occurred. Also, you can just rethrow the (wrapped) exception (with all metadata) with Throw $Error[0].– mklement0
May 11 '18 at 19:56
That should work. However, so as not to wipe out the session's
$Error collection, perhaps it's better to compare the entry count after with the one before to determine if an error occurred. Also, you can just rethrow the (wrapped) exception (with all metadata) with Throw $Error[0].– mklement0
May 11 '18 at 19:56
I couldn't get this one to work , it wasn't catching . Thanks for your efforts in helping me though
– Jacksporrow
May 14 '18 at 18:07
I couldn't get this one to work , it wasn't catching . Thanks for your efforts in helping me though
– Jacksporrow
May 14 '18 at 18:07
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f50298817%2fexchange-online-get-userphoto-how-to-catch-non-terminating-error%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
In your
tryblock, try adding$local:ErrorActionPreference = 'Stop'– TheIncorrigible1
May 11 '18 at 19:15
According to the documentation the cmdlet should support common parameters. I believe even non-terminating errors get added to the
$Errorvariable, though, so you should be able to do:$Error.Clear(); command here; if ($Error) { }– TheIncorrigible1
May 11 '18 at 19:19
@TheIncorrigible1: A local
$ErrorActionPreferencepreference variable instance is unlikely to help, if common parameter-ErrorAction Stopdoesn't work (also, thelocal:prefix wouldn't make a difference). Yes, non-terminating errors are reflected in$Errortoo, as, in fact, are all errors - except if common parameter-ErrorAction Ignoreis passed to a cmdlet invocation (note, however, that'Ignore'cannot be set as the value of$ErrorActionPreferencepreference variable).– mklement0
May 11 '18 at 20:49
1
@TheIncorrigible1: Re global scope: that's sensible - that's why in my answer I restore the previous value in a
finallyblock - 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 thattrydoesn't create a separate scope - a$local:ErrorActionPreference = 'Stop'inside atryblock still stays in effect for the rest of the enclosing function / script.– mklement0
May 11 '18 at 20:56
1
@mklement0 I had thought
trycreates its own block scope (due to using a scriptblock syntax). Good to know, thanks!– TheIncorrigible1
May 11 '18 at 20:57