How can I integrate the powershell to the ARM template?
Follow the steps below to add the machine to a group using a PowerShell script in an ARM Template
I have followed this MS Doc as a reference to create a custom script extension using ARM Template.
1.Upload the following PowerShell script to your storage account
2. Assign the required role to the VM for accessing the script from the storage account.
$MachineName = "<VMName>"
$GroupName = "<ADGroupName>"
$machine = Get-AzureADDevice -Filter "DisplayName eq '$MachineName'"
if ($machine -eq $null) {
Write-Host "Machine '$MachineName' not found."
exit
}
$group = Get-AzureADGroup -Filter "DisplayName eq '$GroupName'"
if ($group -eq $null) {
Write-Host "Group '$GroupName' not found."
exit
}
Add-AzureADGroupMember -ObjectId $group.ObjectId -RefObjectId $machine.ObjectId
Write-Host "Machine '$MachineName' added to group '$GroupName' successfully."
- You can find the
fileUris
by navigating to the following path.
Azure Portal > Storage Account > Your Storage Account > Select your Container
- Deploy the below ARM Template

ARM Template.
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {},
"variables": {},
"resources": [
{
"type": "Microsoft.Compute/virtualMachines",
"apiVersion": "2022-03-01",
"name": "<VMName>",
"location": "<ResourceGroup Location>",
"properties": {},
"resources": [
{
"type": "extensions",
"name": "customScript",
"location": "<ResourceGroup Location>",
"dependsOn": [
"[concat('Microsoft.Compute/virtualMachines/', 'sampleVM')]"
],
"apiVersion": "2022-03-01",
"properties": {
"publisher": "Microsoft.Compute",
"type": "CustomScriptExtension",
"typeHandlerVersion": "1.10",
"settings": {
"fileUris": [
"powershell_script_url"
],
"commandToExecute": "powershell -ExecutionPolicy Unrestricted -File script.ps1 -MachineName 'sampleVM'"
}
}
}
]
}
],
"outputs": {}
}
A custom extension is created in the Azure VM
once the ARM
deployment is complete, and the device is also added to an Azure AD
group.
