@@ -30,6 +30,8 @@ namespace Microsoft.Azure.Commands.GuestConfiguration.Common
30
30
using Microsoft . Azure . Commands . GuestConfiguration . Models ;
31
31
using Microsoft . Azure . Management . GuestConfiguration ;
32
32
using Microsoft . Azure . Management . GuestConfiguration . Models ;
33
+ using Microsoft . Azure . Management . PolicyInsights ;
34
+ using Microsoft . Azure . Management . PolicyInsights . Models ;
33
35
using GuestConfigurationErrorResponseException = Management . GuestConfiguration . Models . ErrorResponseException ;
34
36
using StringResources = Microsoft . Azure . Commands . GuestConfiguration . Properties . Resources ;
35
37
using ResourceManagerErrorResponseException = Microsoft . Azure . Management . Internal . ResourceManager . Version2018_05_01 . Models . ErrorResponseException ;
@@ -49,6 +51,11 @@ public abstract class GuestConfigurationCmdletBase : AzureRMCmdlet
49
51
/// </summary>
50
52
private IPolicyClient _policyClient ;
51
53
54
+ /// <summary>
55
+ /// Policy insights client
56
+ /// </summary>
57
+ private IPolicyInsightsClient _policyInsightsClient ;
58
+
52
59
private string _initiativeId = null ;
53
60
54
61
/// <summary>
@@ -75,11 +82,26 @@ public IPolicyClient PolicyClient
75
82
}
76
83
}
77
84
85
+ /// <summary>
86
+ /// Gets or sets the policy insights client
87
+ /// </summary>
88
+ public IPolicyInsightsClient PolicyInsightsClient
89
+ {
90
+ get
91
+ {
92
+ return _policyInsightsClient ??
93
+ ( _policyInsightsClient = AzureSession . Instance . ClientFactory . CreateArmClient < PolicyInsightsClient > ( DefaultContext , AzureEnvironment . Endpoint . ResourceManager ) ) ;
94
+ }
95
+ set
96
+ {
97
+ _policyInsightsClient = value ;
98
+ }
99
+ }
100
+
78
101
// Get all guest configuration policy assignment reports for a VM
79
102
protected IEnumerable < PolicyStatusDetailed > GetPolicyStatusesDetailed ( string resourceGroupName ,
80
103
string vmName ,
81
- IEnumerable < GuestConfigurationAssignment > gcrpAssignments ,
82
- bool isStatusHistoryCmdlet )
104
+ IEnumerable < GuestConfigurationAssignment > gcrpAssignments )
83
105
{
84
106
var gcPolicyAssignmentReportList = new List < PolicyStatusDetailed > ( ) ;
85
107
var gcPolicySetDefinitions = GetAllGuestConfigPolicySetDefinitions ( ) ;
@@ -91,7 +113,7 @@ protected IEnumerable<PolicyStatusDetailed> GetPolicyStatusesDetailed(string res
91
113
92
114
foreach ( var gcPolicySetDefinition in gcPolicySetDefinitions )
93
115
{
94
- var gcAssignmentReports = GetPolicyStatusesDetailedByInitiativeId ( resourceGroupName , vmName , gcPolicySetDefinition . Id , isStatusHistoryCmdlet , gcrpAssignments ) ;
116
+ var gcAssignmentReports = GetPolicyStatusesDetailedByInitiativeId ( resourceGroupName , vmName , gcPolicySetDefinition . Id , gcrpAssignments ) ;
95
117
if ( gcAssignmentReports != null || gcAssignmentReports . Count ( ) > 0 )
96
118
{
97
119
gcPolicyAssignmentReportList . AddRange ( gcAssignmentReports ) ;
@@ -148,7 +170,7 @@ protected IEnumerable<PolicyData> GetPolicyStatuses(string resourceGroupName,
148
170
var policySetDefinitions = GetAllGuestConfigPolicySetDefinitions ( ) ;
149
171
policySetDefinitionsArray = policySetDefinitions != null ? policySetDefinitions . ToArray ( ) : null ;
150
172
}
151
- var policyStatuses = GetPolicyStatusesHelper ( policySetDefinitionsArray , gcrpAssignments ) ;
173
+ var policyStatuses = GetPolicyStatusesHelper ( policySetDefinitionsArray , gcrpAssignments , resourceGroupName ) ;
152
174
return policyStatuses ;
153
175
}
154
176
@@ -177,7 +199,18 @@ protected IEnumerable<PolicyStatusDetailed> GetPolicyStatusesDetailedByInitiativ
177
199
{
178
200
gcrpReport = GuestConfigurationClient . GuestConfigurationAssignmentReports . Get ( resourceGroupName , gcPolicyAssignment . Configuration . Name , reportGuid , vmName ) ;
179
201
}
180
- gcPolicyAssignmentReportList . Add ( new PolicyStatusDetailed ( gcrpReport , gcPolicyAssignment ) ) ;
202
+
203
+ PolicyStatusDetailed policyDetailed = new PolicyStatusDetailed ( gcrpReport , gcPolicyAssignment ) ;
204
+ QueryOptions queryOptions = new QueryOptions ( ) ;
205
+ queryOptions . Filter = string . Format ( "resourceGroup eq '{0}' and policyDefinitionAction eq 'deployifnotexists' and contains(ResourceId,'{1}')" , resourceGroupName , vmName ) ;
206
+ queryOptions . OrderBy = "Timestamp desc" ;
207
+ queryOptions . Top = 1 ;
208
+ PolicyStatesQueryResults policyDbResults = PolicyInsightsClient . PolicyStates . ListQueryResultsForPolicySetDefinition ( "latest" , DefaultContext . Subscription . Id , gcPolicyAssignment . PolicySetDefinitionName , queryOptions ) ;
209
+ if ( policyDbResults . Odatacount > 0 && policyDbResults . Value [ 0 ] . ComplianceState == "NonCompliant" )
210
+ {
211
+ policyDetailed . ComplianceStatus = policyDbResults . Value [ 0 ] . ComplianceState ;
212
+ }
213
+ gcPolicyAssignmentReportList . Add ( policyDetailed ) ;
181
214
}
182
215
183
216
return gcPolicyAssignmentReportList ;
@@ -186,8 +219,7 @@ protected IEnumerable<PolicyStatusDetailed> GetPolicyStatusesDetailedByInitiativ
186
219
// Get guest configuration policy status history by initiative definition name
187
220
protected IEnumerable < PolicyStatusDetailed > GetPolicyStatusesDetailedByInitiativeId ( string resourceGroupName ,
188
221
string vmName ,
189
- string initiativeId ,
190
- bool isStatusHistoryCmdlet ,
222
+ string initiativeId ,
191
223
IEnumerable < GuestConfigurationAssignment > gcrpAssignments )
192
224
{
193
225
var initiativeName = GetInitiativeNameFromId ( initiativeId ) ;
@@ -227,7 +259,7 @@ protected PolicyStatusDetailed GetPolicyStatusDetailedByReportId(string reportId
227
259
228
260
if ( urlParameters == null || reportGuid == null )
229
261
{
230
- throw new ErrorResponseException ( string . Format ( StringResources . InvalidReportId , reportId ) ) ;
262
+ throw new GuestConfigurationErrorResponseException ( string . Format ( StringResources . InvalidReportId , reportId ) ) ;
231
263
}
232
264
233
265
PolicyStatusDetailed policyReport = null ;
@@ -268,7 +300,7 @@ private string GetInitiativeNameFromId(string initiativeId)
268
300
var indexOfInitiativeName = initiativeId . LastIndexOf ( "/" ) ;
269
301
if ( indexOfInitiativeName < 0 || indexOfInitiativeName == initiativeId . Length - 1 )
270
302
{
271
- throw new ErrorResponseException ( string . Format ( StringResources . NoInitiativeNameFound , initiativeId ) ) ;
303
+ throw new GuestConfigurationErrorResponseException ( string . Format ( StringResources . NoInitiativeNameFound , initiativeId ) ) ;
272
304
}
273
305
var initiativeName = initiativeId . Substring ( indexOfInitiativeName + 1 ) ;
274
306
return initiativeName ;
@@ -305,7 +337,7 @@ private IEnumerable<PolicySetDefinition> GetAllGuestConfigPolicySetDefinitions()
305
337
return gcPolicySetDefinitions ;
306
338
}
307
339
308
- private IEnumerable < PolicyData > GetPolicyStatusesHelper ( PolicySetDefinition [ ] policySetDefinitions , IEnumerable < GuestConfigurationAssignment > gcrpAssignments )
340
+ private IEnumerable < PolicyData > GetPolicyStatusesHelper ( PolicySetDefinition [ ] policySetDefinitions , IEnumerable < GuestConfigurationAssignment > gcrpAssignments , string resourceGroupName )
309
341
{
310
342
var gcPolicyAssignmentsList = new List < PolicyData > ( ) ;
311
343
foreach ( var policySetDefinition in policySetDefinitions )
@@ -395,10 +427,11 @@ private IEnumerable<PolicyData> GetPolicyStatusesHelper(PolicySetDefinition[] po
395
427
396
428
if ( ! string . IsNullOrEmpty ( guestConfigurationAssignmentNameInPolicy ) && gcrp_AssignmentName_Assignment_Map . ContainsKey ( guestConfigurationAssignmentNameInPolicy ) )
397
429
{
430
+ var assignments = PolicyClient . PolicyAssignments . ListForResourceGroup ( resourceGroupName , string . Format ( "policyDefinitionId eq '{0}'" , policySetDefinition . Id ) ) ;
398
431
var gcrpAsgnment = gcrp_AssignmentName_Assignment_Map [ guestConfigurationAssignmentNameInPolicy ] ;
399
- if ( gcrpAsgnment != null )
432
+ if ( gcrpAsgnment != null && assignments . Count ( ) > 0 )
400
433
{
401
- gcPolicyAssignmentsList . Add ( new PolicyData ( gcrpAsgnment , policyDef . DisplayName ) ) ;
434
+ gcPolicyAssignmentsList . Add ( new PolicyData ( gcrpAsgnment , policyDef . DisplayName , policySetDefinition . Name ) ) ;
402
435
}
403
436
}
404
437
}
0 commit comments