Skip to content

Commit 05fb0df

Browse files
authored
Merge pull request #2976 from KoenZomers/InvokeGraphReturnStream
Added `-OutStream` to `Invoke-PnPGraphMethod`
2 parents eb6d17b + 62d1c09 commit 05fb0df

File tree

4 files changed

+84
-16
lines changed

4 files changed

+84
-16
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
1111
### Added
1212

1313
- Added `-OutFile` to `Invoke-PnPGraphMethod` which allows for the response to be written to a file [#2971](https://github.com/pnp/powershell/pull/2971)
14+
- Added `-OutStream` to `Invoke-PnPGraphMethod` which allows for the response to be written to a memory stream [#2976](https://github.com/pnp/powershell/pull/2976)
1415

1516
### Contributors
1617

documentation/Invoke-PnPGraphMethod.md

+35
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@ Invoke-PnPGraphMethod -Url <String>
4141
[-Verbose]
4242
```
4343

44+
### Out to stream
45+
```powershell
46+
Invoke-PnPGraphMethod -Url <String>
47+
[-AdditionalHeaders <System.Collections.Generic.IDictionary`2[System.String,System.String]>]
48+
[[-Method] <HttpRequestMethod>]
49+
[-Content <Object>]
50+
[-ContentType <String>]
51+
[-ConsistencyLevelEventual]
52+
[-Connection <PnPConnection>]
53+
[-OutStream]
54+
[-Verbose]
55+
```
56+
4457
## DESCRIPTION
4558
Invokes a REST request towards the Microsoft Graph API. It will take care of potential throttling retries that are needed to retrieve the data.
4659

@@ -89,6 +102,13 @@ Invoke-PnPGraphMethod "https://graph.microsoft.com/v1.0/users/user@contoso.com/p
89102

90103
Downloads the user profile photo of the specified user to the specified file.
91104

105+
### Example 7
106+
```powershell
107+
Invoke-PnPGraphMethod "https://graph.microsoft.com/v1.0/users/user@contoso.com/photo/`$value" -OutStream | Add-PnPFile -FileName user.jpg -Folder "Shared Documents"
108+
```
109+
110+
Takes the user profile photo of the specified user and uploads it to the specified library in SharePoint Online.
111+
92112
## PARAMETERS
93113

94114
### -AdditionalHeaders
@@ -214,6 +234,21 @@ Accept pipeline input: False
214234
Accept wildcard characters: False
215235
```
216236
237+
### -OutStream
238+
Indicates that the result of the request should be returned as a memory stream.
239+
240+
```yaml
241+
Type: String
242+
Parameter Sets: Out to stream
243+
Aliases:
244+
245+
Required: True
246+
Position: Named
247+
Default value: None
248+
Accept pipeline input: False
249+
Accept wildcard characters: False
250+
```
251+
217252
### -Raw
218253
If specified the returned data will not be converted to an object but returned as a JSON string.
219254

src/Commands/Files/AddFile.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class AddFile : PnPWebCmdlet
3232
[ValidateNotNullOrEmpty]
3333
public string NewFileName = string.Empty;
3434

35-
[Parameter(Mandatory = true, ParameterSetName = ParameterSet_ASSTREAM)]
35+
[Parameter(Mandatory = true, ParameterSetName = ParameterSet_ASSTREAM, ValueFromPipeline = true, Position = 0)]
3636
[ValidateNotNullOrEmpty]
3737
public Stream Stream;
3838

src/Commands/Graph/InvokeGraphMethod.cs

+47-15
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88
using System.Net.Http;
99
using System.Net.Http.Headers;
1010
using System.Text.Json.Serialization;
11+
using System.IO;
1112

1213
namespace PnP.PowerShell.Commands.Base
1314
{
1415
[Cmdlet(VerbsLifecycle.Invoke, "PnPGraphMethod", DefaultParameterSetName = ParameterSet_TOCONSOLE)]
1516
public class InvokeGraphMethod : PnPGraphCmdlet
1617
{
18+
private const string ParameterSet_TOSTREAM = "Out to stream";
1719
private const string ParameterSet_TOFILE = "Out to file";
1820
private const string ParameterSet_TOCONSOLE = "Out to console";
1921

@@ -25,6 +27,7 @@ public class InvokeGraphMethod : PnPGraphCmdlet
2527

2628
[Parameter(Mandatory = true, Position = 0, ValueFromPipeline = true, ParameterSetName = ParameterSet_TOFILE)]
2729
[Parameter(Mandatory = true, Position = 0, ValueFromPipeline = true, ParameterSetName = ParameterSet_TOCONSOLE)]
30+
[Parameter(Mandatory = true, Position = 0, ValueFromPipeline = true, ParameterSetName = ParameterSet_TOSTREAM)]
2831
public string Url
2932
{
3033
get { return _url; }
@@ -48,16 +51,19 @@ public string Url
4851

4952
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_TOFILE)]
5053
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_TOCONSOLE)]
54+
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_TOSTREAM)]
5155
public object Content;
5256

5357
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_TOFILE)]
5458
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_TOCONSOLE)]
59+
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_TOSTREAM)]
5560
public string ContentType = "application/json";
5661

5762
IDictionary<string, string> additionalHeaders = null;
5863

5964
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_TOFILE)]
6065
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_TOCONSOLE)]
66+
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_TOSTREAM)]
6167
public IDictionary<string, string> AdditionalHeaders
6268
{
6369
get
@@ -81,6 +87,7 @@ public IDictionary<string, string> AdditionalHeaders
8187

8288
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_TOFILE)]
8389
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_TOCONSOLE)]
90+
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_TOSTREAM)]
8491
public SwitchParameter ConsistencyLevelEventual;
8592

8693
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_TOCONSOLE)]
@@ -92,6 +99,9 @@ public IDictionary<string, string> AdditionalHeaders
9299
[Parameter(Mandatory = true, ParameterSetName = ParameterSet_TOFILE)]
93100
public string OutFile;
94101

102+
[Parameter(Mandatory = true, ParameterSetName = ParameterSet_TOSTREAM)]
103+
public SwitchParameter OutStream;
104+
95105
protected override void ExecuteCmdlet()
96106
{
97107
try
@@ -133,7 +143,7 @@ private void SendRequest()
133143
switch (Method)
134144
{
135145
case HttpRequestMethod.Get:
136-
if(string.IsNullOrWhiteSpace(OutFile))
146+
if(ParameterSetName == ParameterSet_TOCONSOLE)
137147
{
138148
GetRequestWithPaging();
139149
}
@@ -283,24 +293,46 @@ private void DeleteRequest()
283293

284294
private void HandleResponse(HttpResponseMessage response)
285295
{
286-
if(ParameterSpecified(nameof(OutFile)))
296+
switch (ParameterSetName)
287297
{
288-
var responseStream = response.Content.ReadAsStreamAsync().GetAwaiter().GetResult();
298+
case ParameterSet_TOCONSOLE:
299+
var result = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
289300

290-
WriteVerbose($"Writing {responseStream.Length} bytes response to {OutFile}");
301+
WriteVerbose($"Returning {result.Length} characters response");
291302

292-
using var fileStream = new System.IO.FileStream(OutFile, System.IO.FileMode.Create, System.IO.FileAccess.Write);
293-
responseStream.CopyTo(fileStream);
294-
fileStream.Close();
295-
return;
296-
}
297-
else
298-
{
299-
var result = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
303+
WriteGraphResult(result);
304+
break;
305+
306+
case ParameterSet_TOFILE:
307+
using (var responseStreamForFile = response.Content.ReadAsStream())
308+
{
309+
WriteVerbose($"Writing {responseStreamForFile.Length} bytes response to {OutFile}");
310+
311+
using (var fileStream = new System.IO.FileStream(OutFile, System.IO.FileMode.Create, System.IO.FileAccess.Write))
312+
{
313+
responseStreamForFile.CopyTo(fileStream);
314+
fileStream.Close();
315+
}
316+
}
317+
break;
318+
319+
case ParameterSet_TOSTREAM:
320+
var responseStream = response.Content.ReadAsStream();
321+
322+
WriteVerbose($"Writing {responseStream.Length} bytes response to outputstream");
323+
324+
var memoryStream = new System.IO.MemoryStream();
325+
responseStream.CopyTo(memoryStream);
326+
memoryStream.Position = 0;
327+
328+
responseStream.Close();
329+
responseStream.Dispose();
330+
331+
WriteObject(memoryStream);
332+
break;
300333

301-
WriteVerbose($"Returning {result.Length} characters response");
302-
303-
WriteGraphResult(result);
334+
default:
335+
throw new Exception($"Parameter set {ParameterSetName} not supported");
304336
}
305337
}
306338
}

0 commit comments

Comments
 (0)