Context
We are using GetAwaiter().GetResult() because PowerShell's Cmdlet.ProcessRecord() does not support async/await.
Code Sample
class Program
{
    static async Task Main(string[] args)
    {
        var topicPath = "some-topic";
        var subscriptionName = "some-subscription";
        var connectionString = "some-connection-string";
        var subscriptionPath = EntityNameHelper.FormatSubscriptionPath(
            topicPath,
            subscriptionName
        );
        var serviceBusClient = new ServiceBusClient(connectionString);
        var receiver = serviceBusClient.CreateReceiver(queueName: subscriptionPath);
        // This one works. :-) 
        await foreach (var item in GetMessages(receiver, maxMessagesPerFetch: 5))
        {
            Console.WriteLine("async/await: " + item);
        }
        // This one explodes.
        var enumerator = GetMessages(receiver, maxMessagesPerFetch: 5).GetAsyncEnumerator();
        while (enumerator.MoveNextAsync().GetAwaiter().GetResult())
        {
            // Unhandled exception. System.InvalidOperationException: Operation is not valid due to the current state of the object.
            //    at NonSync.IAsyncEnumerable.Program.GetMessages(ServiceBusReceiver receiver, Int32 maxMessagesPerFetch)+System.Threading.Tasks.Sources.IValueTaskSource<System.Boolean>.GetResult()
            //    at NonSync.IAsyncEnumerable.Program.Main(String[] args) in C:\dev\mediavalet\MediaValet.Learning\entropy\NonSynchronousDotNet\NonSync.IAsyncEnumerable\Program.cs:line 42
            //    at NonSync.IAsyncEnumerable.Program.<Main>(String[] args)
            Console.WriteLine("GetAwaiter().GetResult(): " + enumerator.Current);
        }
    }
    public static async IAsyncEnumerable<string> GetMessages(
        ServiceBusReceiver receiver,
        int maxMessagesPerFetch
    )
    {
        yield return "Foo";
        var messages = await receiver.PeekMessagesAsync(maxMessagesPerFetch);
        yield return "Bar";
    }
}
Question
What's going on here? How can we fix it without changing GetMessages?
 
                        
According to the documentation of the
ValueTask<TResult>struct:What you can do is to convert the
ValueTask<bool>to aTask<bool>, by using theAsTaskmethod: