希望你一切都好,
一年前我有类似的要求,但那是为了将机器添加到本地 AD 组。 在使用 ARM 模板配置 Azure VM 时添加 onprem AD 组 -Azure 虚拟桌面
我使用自定义扩展脚本成功了。现在我们需要将机器添加到 only Azure AD 组(而不是与 AAD 同步的 onprem 组)。 出于安全考虑,我们不得不限制在我们的供应 SPN 上委派 api 权限。
我可以运行一个以机器作为输入的 powershell 脚本来手动将机器添加到 AD 组。
如何将 Powershell 集成到 ARM 模板中?因为委托的 api 权限需要 SPN 和用户来验证 AAD 以执行活动。
想知道是否有人有一些想法?
问候, 纳文。 S
如何将 powershell 集成到 ARM 模板中?
按照以下步骤在 ARM 模板中使用 PowerShell 脚本将机器添加到组
我已经按照这个MS Doc作为使用ARM模板创建自定义脚本扩展的参考。
1.将以下PowerShell脚本上传到您的存储账户 2. 为 VM 分配所需的角色以从存储帐户访问脚本。
$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."
fileUris
。Azure 门户 > 存储帐户 > 您的存储帐户 > 选择您的容器
ARM 模板。
{
"$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": {}
}
一旦
Azure VM
部署完成,就会在 ARM
中创建一个自定义扩展,并且该设备也被添加到一个 Azure AD
组中。