Forum Discussion
LeonPavesic
Mar 18, 2024Silver Contributor
PowerShell - GraphAPI - OneDrive Shared files - Error
Hello everybody, for changing the UPN for some number of users I need to export all the shared files in OneDrive for all users in the tenant. I have found an excellent script by the MVP Vasil M...
- Mar 20, 2024
As an afterthought, the Renew-Token function is flawed and can lead to the unexpected outcome of re-trying a query despite the token renewal having failed.
Given how the function has been used on line 237 and impacts lines 239 to 241, it's quite possible that the token renewal has failed, yet because the $authHeader variable has at no stage been removed, that the wrong conclusion is reached on line 239 - where the pre-existing header featuring the since-expired token remains in existence.
The "easiest" solution to this is to remove the $authHeader variable on the first line of the Renew-Token function using the Remove-Variable commandlet. That way, only a successful renewal will result in its recreation later in the function.
You'd then have to check for any knock-on impacts, but at least in the specific case of lines 239 to 241, the outcome would be fine, and indeed, expected.
As the script is now though, line 239 will always evaluate to $false, meaning line 241 will always execute, even in the token renewal failure scenario.
Cheers,
Lain
LainRobertson
Mar 20, 2024Silver Contributor
Hi, Leon.
The error relates to the return statement on line 248 of the script (per your post), meaning either $Result, or $Result.Content is $null.
$Result is only set either on lines 220 or 241. So, the underlying issue is how is $Result (or $Result.Content) ending up as $null.
You can choose to dig into that, or you can simply alter line 248 to test for $null rather than blindly trying to pipe $null into ConvertFrom-Json, which is what's triggered that error. Certainly, this would at least be the easiest starting point.
I'd use a simple if block, since it would then be trivial to use a matching else to diagnose what's going on with $Result/$Result.Content.
if ($Result -and $Result.Content)
{
return($Result.Content | ConvertFrom-Json);
}
else
{
[PSCustomObject] @{
ResultIsNull = $null -eq $Result;
ContentIsNull = $true;
Uri = $Uri;
Result = if ($Result) { $Result; };
} | Write-Warning; # Sending this to warning stream to avoid unintended consequences.
}
(Change the else block as you see fit - I took the lazy approach of dumping it out to the warning stream but to file might provide better fidelity in the scenario where only $Result.Content is $null.)
Cheers,
Lain