I am calling PnP-GetWeb
like this:
[Microsoft.SharePoint.Client.SecurableObject]$Web = Get-PnPWeb -Connection $pnpConnection -ErrorAction SilentlyContinue
if($null -eq $web) {
throw "Could not get information for: $($pnpConnection.Url)"
}
but when there is a 403 "Forbidden", it only outputs this to stdout. There is no exception thrown and I don't see a way to capture that error information unless I redirect stdout. It seems I need to capture its error output to a variable instead of to stdout so that I can format the error before logging it.
I this I can capture error output to a variable like this:
$output = Get-PnpWeb -Connection $pnpConnection 2>&1
but how do I both assign $web and $output?
UPDATE:
To clarify, this code is already inside a try/catch block. Sorry for not mentioning that earlier. However, no exception is ever thrown, even without SilentlyContinue
.
Also, Get-PnPWeb
does return $null
when the stdout shows a 403.
Use -ErrorVariable
to assign the error in a variable, here is an example:
try {
[Microsoft.SharePoint.Client.SecurableObject]$Web =
Get-PnPWeb -Connection $pnpConnection -ErrorAction SilentlyContinue -ErrorVariable err
if($null -eq $web) {
throw "Could not get information for: $($pnpConnection.Url)"
}
} catch {
throw $_
}
if ($err) {
throw $_
}