/// <summary> /// Saves a stream to a file on disk. /// </summary> /// <param name="cmdlet">The calling <see cref="PSCmdlet"/>.</param> /// <param name="response">The HTTP response from the service.</param> /// <param name="inputStream">The stream to write to file.</param> /// <param name="filePath">The path to write the file to. This should include the file name and extension.</param> /// <param name="cancellationToken">A cancellation token that will be used to cancel the operation by the user.</param> internal static void WriteToFile(this PSCmdlet cmdlet, HttpResponseMessage response, Stream inputStream, string filePath, CancellationToken cancellationToken) { using (var fileProvider = ProtectedFileProvider.CreateFileProvider(filePath, FileProtection.ExclusiveWrite, new DiskDataStore())) { string downloadUrl = response?.RequestMessage?.RequestUri.ToString(); cmdlet.WriteToStream(inputStream, fileProvider.Stream, downloadUrl, cancellationToken); } }