The Wayback Machine - https://web.archive.org/web/20240605180340/https://www.geeksforgeeks.org/microsoft-azure-creating-json-snippet-for-policy-initiative-creation-using-csv-file/
Open In App

Microsoft Azure – Creating JSON Snippet For Policy Initiative Creation using CSV File

Last Updated : 30 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Here in this article, we are going to create a custom JSON script by using the data filled in the CSV file for automation.

What we are going to do is first we’ll convert a list of given Policy IDs from CSV to JSON Code Snippet as a prerequisite for creating a custom policy initiative definition for automation.

Implementation:

Follow the below steps to create a JSON snippet for policy initiative creation using a CSV file:

Step 1: Create a new with .csv extension and policies id’s to it and format should look like this. ‘PolicyID’ as Header and Add required Policy Definition Id(s) as Values one after the other.

Step 2: After creating the csv, Login to Azure Portal

Step 3: From Azure Portal Access the cloud shell and select PowerShell 

Step 4: Now Upload the created CSV File to cloud shell storage 

Open Cloud Shell and Click on Upload/Download files >> Select Upload >> Choose your created CSV file from your local disk and select.

Step 5: Now, create a new PowerShell file with .ps1 extension.

Syntax:

touch <filename>.ps1
code <filename>.ps1

Example:

touch policygen.ps1
Code policygen.ps1

Open the <filename>.ps1 and Copy paste the following code and save the file

$PolicyIDs = Import-CSV .\PolicyID.csv

Write-Output "["
foreach ($PolicyID in $PolicyIDs)
{
    Write-Output "  {"
    Write-Output (" " + "   ""policyDefinitionId""" + " : " + """/providers/Microsoft.Authorization/policyDefinitions/" + $Policy.PolicyID + """,")
    Write-Output "  },"
}
Write-Output "]"

Step 6: Run the <filename>.ps1 file 

./<filename>.ps1

Example:

./policygen.ps1

Output:

Step 7: Save console output to a file with .json extension.

./<filename>.ps1 | Out-File ./<filename>.json

Example:

./policygen.ps1 | Out-File ./sample.json

Output:

Now, this sample.json script can be used in CI/CD Pipelines Automation for Created or Updating the Custom Initiative Definitions and Policy Definitions or it can also be used with Azure PowerShell Cmdlets.


Previous Article
Next Article

Similar Reads

Microsoft Azure - Creating Custom Policy Initiative using PowerShell
In this article we'll see how we can create an Azure Custom Policy Initiative Definition with a JSON file using the Azure PowerShell commands in simple easy steps. What is Custom Policy Initiative Definition? Custom Policy Initiative is a collection or group of policy definitions that are grouped to apply policies together for a specific organizati
2 min read
How to Export Definitions in an Azure Initiative Policy to CSV File?
An Azure Policy Initiative Definition is a collection of Azure Policy definitions that are grouped together. Here in this article, we will show how to export Azure policy definitions that are listed in an Azure policy initiative. We will be using the Azure Resource Graph KQL query to export the Azure Policy definitions that are grouped together in
3 min read
Microsoft Azure - Create a Custom Policy Initiative in Azure
Here in this article, we'll see how we can create an Azure Custom Policy Initiative using Azure Portal. Before getting started let's know about Custom Initiative. What is Azure Policy Initiative Definition? An initiative definition is a collection of policy definitions that are grouped towards achieving a singular overarching goal. Initiative defin
2 min read
Microsoft Azure - Add Assignment to Custom Policy Initiative
In this article, we will add an assignment to an already existing custom policy initiative definition using azure PowerShell commands. Here policy assignment is a kind of instructions to apply or stop the action of policies on Azure resources. We can restrict a few executions, update, creations, modifications by applying the policy assignments to s
2 min read
Microsoft Azure - Get Azure Policies Definitions and Policy Initiatives Definitions
In this article, we will be using some Azure PowerShell Commands either in Azure Cloud Shell or in Azure PowerShell ISE in Admin mode to get the details and properties about the Azure Policies. Let's get started. 1. Use the Get-AzPolicyStateSummary PowerShell command to get the details of the azure policy compliance status of Non-Compliant Resource
1 min read
Microsoft Azure - Get Azure VM Properties using Azure PowerShell
The purpose of using the Azure PowerShell Commands is to quickly analyze the overall properties of VM/VMs at once the filtering the with select and where conditions. To find the properties of an Azure VM, you can perform the following commands in Azure Cloud Shell to get the details. Command: Get-AzVM Output: 1. Get all properties of an Azure Virtu
2 min read
Microsoft Azure - RDP to Azure Virtual Machines using Azure Bastion
In this article, we will learn how to do RDP(Remote Desktop Protocol) / SSH(Secure Shell) Connection to an Azure VM using Azure Bastion. First, let's discuss Azure Bastion. The Azure Bastion service is a fully platform-managed PaaS service that you provision inside your virtual network. It provides secure and seamless RDP/SSH connectivity to your v
3 min read
Microsoft Azure - Check Status of Azure VM using Azure PowerShell
The following Azure PowerShell command helps you to find the Azure VM Config and properties details of Os Name, Os Version, Hyper V Generation, Disks properties, Extensions properties of Type Handler Version, Status and Message, VM Agents properties of Type Handler Version, Status and Message and also VM Power Status Codes, Messages, Plugins, Time
3 min read
Microsoft Azure - Azure VM Disk Space in Percentage using Azure KQL
Here we'll be using the Azure Insights Metric Query to find the free disk space in percentage using KQL. The purpose of this query is to find disk drive free space of all the Azure VMs without logging into every server. You can Run the following KQL log Queries in Azure Monitor or Azure VM Logs or Log Analytics Workspace. Note: VM Insights Agent sh
1 min read
Microsoft Azure - Manage Azure VMs using Azure PowerShell Commands
Here we are using Azure PS Commands to manage azure resources from Azure Cloud Shell or by using Windows PowerShell. By using the simple AZ PS command we can manage Azure VM Quicks without any efforts of navigating in Azure Portal and at Scale. Let's get started by using the major Azure PS commands to manage Azure Virtual Machine. 1. Start a Specif
2 min read