Skip to content

Commit

Permalink
Improved and fixed delete all assignments from multiple policies
Browse files Browse the repository at this point in the history
  • Loading branch information
Kvikku committed Jul 12, 2024
1 parent ad16333 commit ff988ca
Showing 1 changed file with 81 additions and 42 deletions.
123 changes: 81 additions & 42 deletions Presentation/Policy/PolicyAssignments/ManagePolicyAssignments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -527,81 +527,116 @@ public async Task DeleteAssignmentsFromSelectedPolicy()

if (policyType == "Compliance")
{
WriteToLog("Deleting assignments for compliance policies are currently not implemented. Skipping...");
rtbSummary.ForeColor = Color.Yellow;
rtbSummary.AppendText("Deleting assignments for compliance policies are currently not implemented. Skipping..." + Environment.NewLine);
rtbSummary.ForeColor = Color.Salmon;

// delete all assignments for the selected policy

// Create a new and empty post request body
var requestBody = new Microsoft.Graph.Beta.DeviceManagement.DeviceCompliancePolicies.Item.Assign.AssignPostRequestBody
{

Assignments = new List<DeviceCompliancePolicyAssignment>()
};

try
{
// Delete - POST request with the group ID's you want to keep assigned to the policy

await graphClient.DeviceManagement.DeviceCompliancePolicies[policyID].Assign.PostAsAssignPostResponseAsync(requestBody);

WriteToLog("All assignments for " + assignment.Cells[0].Value.ToString() + " has been deleted.");
rtbSummary.AppendText("All assignments for " + assignment.Cells[0].Value.ToString() + " has been deleted." + Environment.NewLine);
}

await DeletePolicyAssignment(listOfAssignments, policyType);
catch (Microsoft.Graph.Beta.Models.ODataErrors.ODataError me)
{
rtbSummary.AppendText("Error deleting assignment for " + assignment.Cells[0].Value.ToString() + " for group " + me.Message + Environment.NewLine);
}
}

if (policyType == "Settings Catalog")
{
WriteToLog("Deleting assignments for settings catalog policies are currently not implemented. Skipping...");
rtbSummary.ForeColor = Color.Yellow;
rtbSummary.AppendText("Deleting assignments for settings catalog policies are currently not implemented. Skipping..." + Environment.NewLine);
rtbSummary.ForeColor = Color.Salmon;

// delete all assignments for the selected policy

// Create a new and empty post request body
var requestBody = new Microsoft.Graph.Beta.DeviceManagement.ConfigurationPolicies.Item.Assign.AssignPostRequestBody
{

Assignments = new List<DeviceManagementConfigurationPolicyAssignment>()
};

try
{
// Delete - POST request with the group ID's you want to keep assigned to the policy

await DeletePolicyAssignment(listOfAssignments, policyType);
await graphClient.DeviceManagement.ConfigurationPolicies[policyID].Assign.PostAsAssignPostResponseAsync(requestBody);

WriteToLog("All assignments for " + assignment.Cells[0].Value.ToString() + " has been deleted.");
rtbSummary.AppendText("All assignments for " + assignment.Cells[0].Value.ToString() + " has been deleted." + Environment.NewLine);
}

catch (Microsoft.Graph.Beta.Models.ODataErrors.ODataError me)
{
rtbSummary.AppendText("Error deleting assignment for " + assignment.Cells[0].Value.ToString() + " for group " + me.Message + Environment.NewLine);
}
}

if (policyType == "Device Configuration")
{
// Get all assigned groups for the selected policy

var result = await graphClient.DeviceManagement.DeviceConfigurations[policyID].Assignments.GetAsync();

// delete all assignments for the selected policy

// Check if there are any assignments
// Create a new and empty post request body

if (result.Value.Count == 0)
var requestBody = new Microsoft.Graph.Beta.DeviceManagement.DeviceConfigurations.Item.Assign.AssignPostRequestBody
{
WriteToLog("No assignments found for " + assignment.Cells[0].Value.ToString());
rtbSummary.AppendText("No assignments found for " + assignment.Cells[0].Value.ToString() + Environment.NewLine);
return;
}

// Add the result to a list of assignments
Assignments = new List<DeviceConfigurationAssignment>()
};

foreach (var ID in result.Value)
try
{
// Add the assignment ID to the list
listOfAssignments.Add(ID.Id);
// Delete - POST request with the group ID's you want to keep assigned to the policy

await graphClient.DeviceManagement.DeviceConfigurations[policyID].Assign.PostAsAssignPostResponseAsync(requestBody);

WriteToLog("All assignments for " + assignment.Cells[0].Value.ToString() + " has been deleted.");
rtbSummary.AppendText("All assignments for " + assignment.Cells[0].Value.ToString() + " has been deleted." + Environment.NewLine);
}

await DeletePolicyAssignment(listOfAssignments, policyType);
catch (Microsoft.Graph.Beta.Models.ODataErrors.ODataError me)
{
rtbSummary.AppendText("Error deleting assignment for " + assignment.Cells[0].Value.ToString() + " for group " + me.Message + Environment.NewLine);
}
}

if (policyType == "Administrative Templates")
{
// Get all assigned groups for the selected policy
// delete all assignments for the selected policy

var result = await graphClient.DeviceManagement.GroupPolicyConfigurations[policyID].Assignments.GetAsync();
// Create a new and empty post request body

// Check if there are any assignments

if (result.Value.Count == 0)
var requestBody = new Microsoft.Graph.Beta.DeviceManagement.GroupPolicyConfigurations.Item.Assign.AssignPostRequestBody
{
WriteToLog("No assignments found for " + assignment.Cells[0].Value.ToString());
rtbSummary.AppendText("No assignments found for " + assignment.Cells[0].Value.ToString() + Environment.NewLine);
return;
}

// Add the result to a list of assignments
Assignments = new List<GroupPolicyConfigurationAssignment>()
};

foreach (var ID in result.Value)
try
{
// Add the assignment ID to the list
listOfAssignments.Add(ID.Id);
}

await DeletePolicyAssignment(listOfAssignments, policyType);

}
// Delete - POST request with the group ID's you want to keep assigned to the policy

await graphClient.DeviceManagement.GroupPolicyConfigurations[policyID].Assign.PostAsAssignPostResponseAsync(requestBody);

WriteToLog("All assignments for " + assignment.Cells[0].Value.ToString() + " has been deleted.");
rtbSummary.AppendText("All assignments for " + assignment.Cells[0].Value.ToString() + " has been deleted." + Environment.NewLine);
}

catch (Microsoft.Graph.Beta.Models.ODataErrors.ODataError me)
{
rtbSummary.AppendText("Error deleting assignment for " + assignment.Cells[0].Value.ToString() + " for group " + me.Message + Environment.NewLine);
}
}
}

// Clear the datagridview for older results
Expand Down Expand Up @@ -985,6 +1020,10 @@ public async Task DeletePolicyAssignment(List<string> assignmentList, string pol

private void btnListAllPolices_Click(object sender, EventArgs e)
{
// clear the datagridview for older results
ClearDataGridView(dtgDisplayPolicy);

// Refresh the datagridview
listAllCompliancePolicies();
listAllSettingsCatalogPolicies();
listAllDeviceConfigurationPolicies();
Expand Down

0 comments on commit ff988ca

Please sign in to comment.