0

I need to invoke an async method of a CLR type like this:

$tokenTask = $tokenProvider.AcquireAccessToken()
$tokenTask.Wait()
Write-Output "Token: $($tokenTask.Result)"

This in not working since it for some reason cannot successfully invoke Wait:

Exception calling "Wait" with "0" argument(s): "One or more errors occurred."
At D:\UserManagement\STP.UserManagement.Identity.Client\STP.UserManagement.Identity.Client.Sample\authenticate.ps1:8 char:1
+ $tokenTask.Wait()
+ ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : AggregateException

How can I wait and get the result of my async method? Is there a keyword like await in PowerShell?

1 Answer 1

1

My problem was that my task was Faulted right from the start because of missing Add-Type for another assembly. I found it like this:

$tokenTask = $tokenProvider.AcquireAccessToken()
Write-Output "authenticating... ($($tokenTask.Status), $($tokenTask.Exception))"
$tokenTask.Wait()
Write-Output "authenticated ($($tokenTask.Status), $($tokenTask.Exception))"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.