13
13
// ----------------------------------------------------------------------------------
14
14
15
15
using Microsoft . Azure . Commands . RemoteApp ;
16
+ using Microsoft . Azure . Management . RemoteApp ;
16
17
using Microsoft . Azure . Management . RemoteApp . Models ;
17
- using Microsoft . PowerShell . Commands ;
18
18
using System ;
19
19
using System . Collections . ObjectModel ;
20
20
using System . Management . Automation ;
21
+ using System . IO ;
22
+ using System . Linq ;
23
+ using System . Management . Automation ;
24
+ using System . Net ;
21
25
22
26
namespace Microsoft . Azure . Management . RemoteApp . Cmdlets
23
27
{
@@ -46,18 +50,15 @@ public class GetAzureRemoteAppCollectionUsageDetails : RdsCmdlet
46
50
public string UsageYear { get ; set ; }
47
51
48
52
[ Parameter ( Mandatory = false ,
49
- HelpMessage = "Allows to run the cmdlet in the background as a PS job." ) ]
53
+ HelpMessage = "Allows running the cmdlet in the background as a PS job." ) ]
50
54
public SwitchParameter AsJob { get ; set ; }
51
55
52
56
private LongRunningTask < GetAzureRemoteAppCollectionUsageDetails > task = null ;
53
57
54
58
private void GetPublishedUsageDetails ( CollectionUsageDetailsResult detailsUsage )
55
59
{
56
60
RemoteAppOperationStatusResult operationResult = null ;
57
- int maxRetryCount = 60 ;
58
- Collection < WebResponseObject > htmlWebResponse = null ;
59
- string scriptBlock = "Invoke-WebRequest -Uri " + "\" " + detailsUsage . UsageDetails . SasUri + "\" " ;
60
-
61
+
61
62
// The request is async and we have to wait for the usage details to be produced here
62
63
do
63
64
{
@@ -67,38 +68,49 @@ private void GetPublishedUsageDetails(CollectionUsageDetailsResult detailsUsage)
67
68
operationResult = CallClient ( ( ) => Client . OperationResults . Get ( detailsUsage . UsageDetails . OperationTrackingId ) , Client . OperationResults ) ;
68
69
}
69
70
while ( operationResult . RemoteAppOperationResult . Status != RemoteAppOperationStatus . Success &&
70
- operationResult . RemoteAppOperationResult . Status != RemoteAppOperationStatus . Failed &&
71
- -- maxRetryCount > 0 ) ;
71
+ operationResult . RemoteAppOperationResult . Status != RemoteAppOperationStatus . Failed ) ;
72
72
73
73
if ( operationResult . RemoteAppOperationResult . Status == RemoteAppOperationStatus . Success )
74
74
{
75
- htmlWebResponse = CallPowershellWithReturnType < WebResponseObject > ( scriptBlock ) ;
76
- string usage = new string ( System . Text . Encoding . UTF8 . GetChars ( htmlWebResponse [ 0 ] . Content ) ) ;
77
- WriteObject ( usage ) ;
75
+ WriteUsageDetails ( detailsUsage ) ;
78
76
}
79
77
else
80
78
{
81
- if ( operationResult . RemoteAppOperationResult . Status == RemoteAppOperationStatus . Failed )
82
- {
83
- ErrorRecord error = RemoteAppCollectionErrorState . CreateErrorRecordFromString (
84
- Commands_RemoteApp . DetailedUsageFailureMessage ,
85
- String . Empty ,
86
- Client . Collections ,
87
- ErrorCategory . ResourceUnavailable ) ;
79
+ ErrorRecord error = RemoteAppCollectionErrorState . CreateErrorRecordFromString (
80
+ Commands_RemoteApp . DetailedUsageFailureMessage ,
81
+ String . Empty ,
82
+ Client . Collections ,
83
+ ErrorCategory . ResourceUnavailable ) ;
88
84
89
- WriteError ( error ) ;
90
- }
91
- else if ( maxRetryCount <= 0 )
85
+ WriteError ( error ) ;
86
+ }
87
+ }
88
+
89
+ private void WriteUsageDetails ( CollectionUsageDetailsResult detailsUsage )
90
+ {
91
+ //
92
+ // Display the content pointed to by the returned URI
93
+ //
94
+ WebResponse response = null ;
95
+
96
+ WebRequest request = WebRequest . Create ( detailsUsage . UsageDetails . SasUri ) ;
97
+
98
+ try
99
+ {
100
+ response = ( HttpWebResponse ) request . GetResponse ( ) ;
101
+ }
102
+ catch ( Exception e )
103
+ {
104
+ ErrorRecord error = RemoteAppCollectionErrorState . CreateErrorRecordFromException ( e , String . Empty , Client . Collections , ErrorCategory . InvalidResult ) ;
105
+ WriteError ( error ) ;
106
+ }
107
+
108
+ using ( Stream dataStream = response . GetResponseStream ( ) )
109
+ {
110
+ using ( StreamReader reader = new StreamReader ( dataStream ) )
92
111
{
93
- ErrorRecord error = RemoteAppCollectionErrorState . CreateErrorRecordFromString (
94
- String . Format ( System . Globalization . CultureInfo . InvariantCulture ,
95
- Commands_RemoteApp . RequestTimedOutFormat ,
96
- detailsUsage . UsageDetails . OperationTrackingId ) ,
97
- String . Empty ,
98
- Client . Collections ,
99
- ErrorCategory . OperationTimeout ) ;
100
-
101
- WriteError ( error ) ;
112
+ String csvContent = reader . ReadToEnd ( ) ;
113
+ WriteObject ( csvContent ) ;
102
114
}
103
115
}
104
116
}
0 commit comments