%3CLINGO-SUB%20id%3D%22lingo-sub-1594674%22%20slang%3D%22en-US%22%3EAzure%20Policy%20Remediation%20with%20Deployment%20Scripts%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1594674%22%20slang%3D%22en-US%22%3E%3CP%3EHow%20many%20times%20have%20you%20wanted%20to%20remediate%20a%20non-compliant%20object%20using%20Azure%20Policy%20but%20found%20you%20can%E2%80%99t%20because%20the%20policy%20language%20or%20type%20of%20object%20can%E2%80%99t%20be%20manipulated%20in%20that%20way.%20Or%20maybe%20you%E2%80%99ve%20had%20to%20write%20a%20policy%20with%20an%20audit%20effect%20instead%20of%20being%20able%20to%20create%20a%20deployment%20to%20remediate%20the%20issue.%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fazure-resource-manager%2Ftemplates%2Fdeployment-script-template%3Ftabs%3DCLI%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EDeployment%20Scripts%3C%2FA%3E%20are%20currently%20in%20preview%20and%20allow%20you%20to%20execute%20PowerShell%20or%20CLI%20scripts%20using%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fcontainer-instances%2Fcontainer-instances-overview%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EAzure%20Container%20Instances%3C%2FA%3E%20as%20part%20of%20an%20Azure%20Resource%20Manager%20template.%20To%20put%20it%20simply%20%E2%80%93%20now%20you%20can%20run%20a%20script%20as%20part%20of%20a%20template%20deployment.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESo%20my%20thought%20was%20if%20I%20can%20deploy%20a%20template%20using%20an%20Azure%20Policy%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fgovernance%2Fpolicy%2Fconcepts%2Feffects%23deployifnotexists%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EDeployIfNotExists%3C%2FA%3E%20effect%20%E2%80%93%20why%20can%E2%80%99t%20I%20deploy%20a%20Deployment%20Script%20object%20which%20then%20runs%20the%20code%20to%20remediate%20my%20non-compliant%20Azure%20resource%3F%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWell%20as%20it%20turns%20out%20you%20can!%20And%20this%20allows%20several%20interesting%20use%20cases%20which%20are%20not%20possible%20with%20the%20default%20policy%20language%20such%20as%3A%20-%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EDeleting%20orphaned%20objects.%3C%2FLI%3E%0A%3CLI%3EChanging%20the%20license%20type%20to%20Hybrid%20Benefit%20for%20existing%20Azure%20machines.%3C%2FLI%3E%0A%3CLI%3EDetailed%20tag%20application%20%E2%80%93%20running%20a%20script%20to%20build%20a%20tag%20value%20based%20on%20many%20other%20resources%20or%20conditions.%3C%2FLI%3E%0A%3CLI%3EPerforming%20data%20plane%20operations%20on%20objects%20like%20Azure%20Key%20Vault%20and%20Azure%20Storage.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3EThe%20rest%20of%20this%20post%20takes%20you%20through%20how%20I%20set%20this%20functionality%20up%20to%20ensure%20that%20all%20Windows%20virtual%20machines%20are%20running%20with%20%3CA%20href%3D%22https%3A%2F%2Fazure.microsoft.com%2Fen-us%2Fpricing%2Fhybrid-benefit%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EAzure%20Hybrid%20Benefit%3C%2FA%3E%20enabled.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EAzure%20Policy%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3EI%20won%E2%80%99t%20go%20into%20the%20details%20of%20creating%20the%20basic%20Azure%20Policy%20rules%20however%20you%20want%20to%20ensure%20that%20the%20effect%20for%20your%20policy%20is%20DeployIfNotExists.%20In%20my%20case%20I%20started%20of%20with%20a%20very%20simple%20rule%20which%20will%20help%20filter%20out%20resources%20I%E2%80%99m%20not%20interested%20in%20%E2%80%93%20so%20as%20part%20of%20the%20rule%20I%E2%80%99m%20looking%20for%20resource%20types%20which%20are%20virtual%20machines%2C%20and%20that%20have%20Microsoft%20Windows%20Server%20as%20the%20publisher.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-json%22%3E%3CCODE%3E%7B%0A%20%20%20%20%22if%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%22allOf%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22field%22%3A%20%22type%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22equals%22%3A%20%22Microsoft.Compute%2FvirtualMachines%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22field%22%3A%20%22Microsoft.Compute%2FvirtualMachines%2FstorageProfile.imageReference.publisher%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22equals%22%3A%20%22MicrosoftWindowsServer%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%7D%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFor%20the%20policy%20effect%20I%20specify%20%3CSTRONG%3EDeployIfNotExists%3C%2FSTRONG%3E%20and%20then%20retrieve%20the%20same%20object%20and%20apply%20some%20more%20checks%20to%20it.%20This%20time%20as%20part%20of%20the%20existence%20condition%20I%E2%80%99m%20going%20to%20check%20the%20license%20type%20field%20to%20check%20if%20it%20is%20correct.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-json%22%3E%3CCODE%3E%22existenceCondition%22%3A%20%7B%0A%20%20%20%20%22allOf%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22field%22%3A%20%22Microsoft.Compute%2FvirtualMachines%2FlicenseType%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22exists%22%3A%20true%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22field%22%3A%20%22Microsoft.Compute%2FvirtualMachines%2FlicenseType%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22equals%22%3A%20%22Windows_Server%22%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22field%22%3A%20%22Microsoft.Compute%2FvirtualMachines%2FlicenseType%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22notEquals%22%3A%20%22%5Bparameters('StorageAccountId')%5D%22%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%5D%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20the%20JSON%20above%20there%20is%20a%20check%20for%20the%20%3CEM%3EStorageAccountId%3C%2FEM%3E%20parameter%20which%20has%20nothing%20to%20do%20with%20the%20object%20we%E2%80%99re%20running%20the%20policy%20against%20%E2%80%93%20but%20I%20need%20to%20include%20it%20as%20I%E2%80%99ve%20used%20it%20as%20a%20parameter%20for%20my%20policy.%20It%20will%20always%20return%20true%2C%20so%20it%E2%80%99s%20not%20really%20included%20as%20part%20of%20the%20evaluation%20and%20by%20itself%20won%E2%80%99t%20trigger%20the%20deployment.%20(If%20you%20try%20to%20add%20a%20policy%20without%20consuming%20all%20the%20parameters%20in%20the%20policy%20rules%20you%20will%20get%20an%20error).%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20rest%20of%20a%20%3CSTRONG%3EDeployIfNotExists%3C%2FSTRONG%3E%20policy%20contains%20the%20object%20I%20want%20to%20deploy%2C%20and%20it%20does%20get%20a%20bit%20complicated.%20If%20I%20was%20to%20just%20deploy%20the%20deployment%20script%20object%20it%20would%20deploy%20in%20the%20same%20resource%20group%20as%20my%20resource%20to%20be%20remediated%20which%20isn%E2%80%99t%20a%20desirable%20outcome%20as%20it%20would%20leave%20a%20mess%20of%20orphaned%20objects.%20The%20deployment%20script%20also%20requires%20a%20storage%20account%20to%20work%20and%20I%20don%E2%80%99t%20want%20my%20subscription%20littered%20with%20random%20storage%20accounts.%20To%20get%20around%20this%20I%20create%20a%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fazure-resource-manager%2Ftemplates%2Fdeploy-to-subscription%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Esubscription%20level%20deployment%3C%2FA%3E%20%E2%80%93%20which%20deploys%20a%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Ftemplates%2Fmicrosoft.resources%2Fdeployments%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Edeployment%20resource%3C%2FA%3E%2C%20which%20contains%20a%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fazure-resource-manager%2Ftemplates%2Flinked-templates%23nested-template%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Enested%20deployment%3C%2FA%3E%20to%20deploy%20the%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fazure-resource-manager%2Ftemplates%2Fdeployment-script-template%3Ftabs%3DCLI%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Edeployment%20script%3C%2FA%3E.%20Confused%3F%20Here%20it%20is%20in%20a%20diagram%20and%20you%20can%20follow%20the%20previous%20links%20or%20look%20at%20the%20policy%20itself.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22policy01.png%22%20style%3D%22width%3A%20318px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F212932i6D75102F34FA6978%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22policy01.png%22%20alt%3D%22policy01.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EThe%20best%20part%20is%20we%20don%E2%80%99t%20have%20to%20manage%20the%20Azure%20Container%20Instance%20as%20the%20deployment%20script%20object%20does%20that%20for%20you.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWhat%20I%20do%20have%20to%20worry%20about%20is%20the%20script%20that%20runs%20%E2%80%93%20it%20uses%20a%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Factive-directory%2Fmanaged-identities-azure-resources%2Foverview%23managed-identity-types%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Euser%20assigned%20managed%20identity%3C%2FA%3E%20which%20must%20have%20permission%20manage%20the%20resources%2C%20in%20this%20case%20I%20need%20to%20give%20it%20Reader%20and%20Virtual%20Machine%20Contributor%20rights%20on%20the%20subscription%20so%20it%20can%20change%20that%20license%20type%20and%20update%20the%20virtual%20machine.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20PowerShell%20script%20which%20runs%20is%20so%20simple%3A%20-%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3EParam(%24ResourceGroupName%2C%20%24VMName)%0A%24vm%20%3D%20Get-AzVM%20-ResourceGroupName%20%24ResourceGroupName%20-Name%20%24VMName%0A%24vm.LicenseType%20%3D%20%22Windows_Server%22%0AUpdate-AzVM%20-VM%20%24vm%20-ResourceGroupName%20%24ResourceGroupName%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20container%20instance%20comes%20with%20the%20Az%20modules%20already%20or%20if%20you%20prefer%20to%20use%20the%20Azure%20CLI%20you%20can%20specify%20that%20in%20the%20deployment%20script%20object%20in%20the%20template.%20The%20script%20can%20be%20either%20be%20provided%20inline%20or%20link%20to%20an%20external%20URL.%20If%20you%20are%20linking%20externally%20and%20don%E2%80%99t%20want%20it%20to%20be%20in%20a%20public%20location%2C%20you%20might%20have%20to%20provide%20a%20SAS%20URL.%20I%20also%20specify%20arguments%20to%20provide%20to%20the%20script%20in%20a%20concatenated%20string%20format%2C%20the%20documentation%20on%20the%20deployment%20script%20provides%20some%20more%20information%20on%20these%20arguments%20but%20you%20can%20incorporate%20parameters%20from%20the%20policy%20which%20means%20the%20inputs%20can%20come%20from%20the%20non-compliant%20objects.%20As%20well%20you%20can%20choose%20to%20use%20an%20existing%20storage%20account%2C%20or%20you%20can%20let%20the%20deployment%20script%20create%20one%20for%20you.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-json%22%3E%3CCODE%3E%22forceUpdateTag%22%3A%20%22%5ButcNow()%5D%22%2C%0A%22azPowerShellVersion%22%3A%20%224.1%22%2C%0A%22storageAccountSettings%22%3A%20%7B%0A%20%20%20%20%22storageAccountName%22%3A%20%22%5Bparameters('StorageAccountName')%5D%22%2C%0A%20%20%20%20%22storageAccountKey%22%3A%20%22%5BlistKeys(resourceId('Microsoft.Storage%2FstorageAccounts'%2C%20parameters('StorageAccountName'))%2C%20'2019-06-01').keys%5B0%5D.value%5D%22%0A%7D%2C%0A%22arguments%22%3A%20%22%5Bconcat('-ResourceGroupName%20'%2Cparameters('VMResourceGroup')%2C'%20-VMName%20'%2Cparameters('VMName'))%5D%22%2C%0A%22retentionInterval%22%3A%20%22P1D%22%2C%0A%22cleanupPreference%22%3A%20%22OnSuccess%22%2C%0A%22primaryScriptUri%22%3A%20%22https%3A%2F%2Fraw.githubusercontent.com%2Fanwather%2FMy-Scripts%2Fmaster%2Flicense.ps1%22%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%E2%80%99ve%20linked%20the%20policy%20rule%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fanwather%2FAzurePolicyRemediation%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehere%3C%2FA%3E%20for%20you%20to%20review%2C%20be%20careful%20to%20observe%20the%20flow%20of%20the%20parameters%20as%26nbsp%3B%20they%20are%20provided%20in%20the%20policy%20assignment%20and%20then%20are%20passed%20down%20through%20each%20deployment%20object%20as%20a%20value.%20%26nbsp%3BThe%20%E2%80%98StorageAccountName%E2%80%99%20parameter%20is%20a%20good%20example%20of%20this.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EDeploying%20the%20Solution%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3EScripts%20and%20policies%20are%20located%20in%20my%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fanwather%2FAzurePolicyRemediation%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EGitHub%20repository%3C%2FA%3E%20%E2%80%93%20you%20can%20clone%20or%20download%20them.%3C%2FP%3E%0A%3CP%3EThe%20steps%20to%20deploy%20the%20required%20resources%20and%20policy%20are%20as%20below%3A%20-%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3EEnsure%20that%20you%20have%20the%20latest%20version%20of%20the%20Az%20PowerShell%20modules%20available.%3C%2FLI%3E%0A%3CLI%3EConnect%20to%20Azure%20using%20Connect-AzAccount%3C%2FLI%3E%0A%3CLI%3EModify%20the%20deploy.ps1%20script%20and%20change%20the%20values%20where%20indicated.%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24resourceGroupName%20%3D%20%22ACI%22%20%23%20%26lt%3B-%20Replace%20with%20your%20value%0A%24location%20%3D%20%22australiaeast%22%20%23%20%26lt%3B-%20This%20must%20be%20a%20location%20that%20can%20host%20Azure%20Container%20Instances%0A%24storageAccountName%20%3D%20%22deploymentscript474694%22%20%23%20%26lt%3B-%20Unique%20storage%20account%20name%0A%24userManagedIdentity%20%3D%20%22scriptRunner%22%20%23%20%26lt%3B-%20Change%20this%20if%20you%20don%E2%80%99t%20like%20the%20name%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B4.%20Run%20the%20deploy.ps1%20script.%20The%20output%20should%20be%20like%20below.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22policy02.png%22%20style%3D%22width%3A%20639px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F212938i277014EDD8E37D10%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22policy02.png%22%20alt%3D%22policy02.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20script%20will%20create%20a%20resource%20group%2C%20storage%20account%20and%20deploy%20the%20policy%20definition.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3ECreate%20a%20Policy%20Assignment%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3EIn%20the%20Azure%20portal%20Policy%20section%2C%20we%20can%20now%20create%20the%20assignment%20and%20deploy%20the%20policy.%20Click%20on%20%E2%80%9CAssign%20Policy%E2%80%9D.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22pol1.png%22%20style%3D%22width%3A%20606px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F212951i430D77FB53ECF31A%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22pol1.png%22%20alt%3D%22pol1.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESelect%20the%20scope%20you%20want%20to%20assign%20the%20policy%20to%20and%20ensure%20that%20the%20correct%20policy%20definition%20is%20selected.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22pol2.png%22%20style%3D%22width%3A%20963px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F212952i9A0FA780A432F345%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22pol2.png%22%20alt%3D%22pol2.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EClick%20next%20and%20fill%20in%20the%20parameters%20%E2%80%93%20the%20values%20for%20this%20are%20output%20by%20the%20deployment%20script.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22pol3.png%22%20style%3D%22width%3A%20961px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F212953iD399E9281DFB01D3%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22pol3.png%22%20alt%3D%22pol3.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EClick%20next%20%E2%80%93%20you%20can%20leave%20the%20options%20as%20is%20for%20this%20screen%20and%20simply%20click%20Review%20and%20Create.%20On%20the%20final%20screen%20just%20click%20create.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20policy%20will%20be%20assigned%2C%20and%20a%20new%20managed%20identity%20will%20also%20be%20created%20which%20allows%20us%20to%20remediate%20any%20non-compliant%20resources.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22pol4.png%22%20style%3D%22width%3A%20943px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F212955i3D71E9D66B8F5AAC%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22pol4.png%22%20alt%3D%22pol4.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3ETesting%20It%20Out%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3ETo%20test%20the%20policy%20and%20remediation%20task%20I%20have%20built%20a%20new%20Windows%20Server%20making%20sure%20that%20I%20haven%E2%80%99t%20selected%20to%20use%20Azure%20Hybrid%20Benefit.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22pp1.png%22%20style%3D%22width%3A%20522px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F212957i15D58FE55D098D86%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22pp1.png%22%20alt%3D%22pp1.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EOnce%20the%20policy%20evaluation%20cycle%20is%20complete%20(use%20Start-AzPolicyComplianceScan%20to%20trigger)%20I%20can%20see%20that%20my%20new%20resource%20is%20now%20showing%20as%20non-compliant.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22pp2.png%22%20style%3D%22width%3A%20602px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F212959i59FAA8828A3A2C4A%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22pp2.png%22%20alt%3D%22pp2.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20can%20go%20in%20now%20and%20create%20a%20remediation%20task%20for%20this%20machine%20by%20clicking%20on%20%3CSTRONG%3ECreate%20Remediation%20Task%3C%2FSTRONG%3E.%20The%20task%20will%20launch%20and%20begin%20the%20deployment%20of%20my%20Deployment%20Script%20object.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22pp3.png%22%20style%3D%22width%3A%20602px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F212960i68684E067BE3D572%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22pp3.png%22%20alt%3D%22pp3.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20can%20check%20the%20resource%20group%20I%20specified%20(ACI)%20that%20the%20deployment%20script%20objects%20are%20created%20in%20and%20will%20be%20able%20to%20see%20the%20object%20in%20there.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22pp4.png%22%20style%3D%22width%3A%20484px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F212961i351C0F97FB38CB4F%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22pp4.png%22%20alt%3D%22pp4.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESelecting%20this%20resource%20will%20show%20the%20details%20about%20the%20container%20instance%20that%20was%20launched%20(it%E2%80%99s%20been%20deleted%20already%20since%20the%20container%20has%20run)%20and%20the%20logs.%20You%20can%20also%20see%20that%20during%20the%20script%20deployment%20it%20has%20been%20able%20to%20bring%20the%20parameters%20I%20specified%20in%20the%20template%20into%20the%20script.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22pp5.png%22%20style%3D%22width%3A%20602px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F212962i7F9D5BD6D7D467FA%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22pp5.png%22%20alt%3D%22pp5.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3BAnd%20finally%2C%20we%20can%20check%20the%20virtual%20machine%20itself%2C%20and%20I%20find%20that%20the%20Azure%20Hybrid%20Benefit%20has%20been%20applied%20successfully.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22pp6.png%22%20style%3D%22width%3A%20602px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F212964iB12427C96F7352D7%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22pp6.png%22%20alt%3D%22pp6.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWhen%20I%20look%20at%20the%20resource%20now%20in%20the%20Azure%20Policy%20blade%20it%20is%20now%20showing%20the%20resource%20as%20compliant.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22pp7.png%22%20style%3D%22width%3A%201108px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F212963i888FC36628F998AC%2Fimage-dimensions%2F1108x46%3Fv%3D1.0%22%20width%3D%221108%22%20height%3D%2246%22%20title%3D%22pp7.png%22%20alt%3D%22pp7.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESo%20there%20you%20have%20it%2C%20what%20started%20as%20a%20theory%20for%20remediating%20objects%20has%20been%20proven%20to%20work%20nicely%20and%20now%20I%20have%20the%20task%20of%20looking%20over%20all%20my%20other%20policies%20and%20seeing%20what%20I%20can%20remediate%20using%20this%20method.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EKnown%20Issues%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EIn%20the%20example%20given%20%E2%80%93%20Azure%20Spot%20instances%20can%E2%80%99t%20be%20remediated%20using%20this%20process%3C%2FLI%3E%0A%3CLI%3EMy%20testing%20cases%20are%20small%20and%20in%20no%20way%20should%20reflect%20your%20own%20testing.%3C%2FLI%3E%0A%3CLI%3EThis%20is%20hosted%20on%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fanwather%2FAzurePolicyRemediation%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EGitHub%3C%2FA%3E%26nbsp%3B%E2%80%93%20if%20there%20are%20issues%20or%20you%20make%20changes%20please%20submit%20a%20PR%20for%20review.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%3CFONT%20size%3D%221%202%203%204%205%206%207%22%3EDisclaimer%3A%3C%2FFONT%3E%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%221%202%203%204%205%206%207%22%3EThe%20sample%20scripts%20are%20not%20supported%20under%20any%20Microsoft%20standard%20support%20program%20or%20service.%20The%20sample%20scripts%20are%20provided%20AS%20IS%20without%20warranty%20of%20any%20kind.%20Microsoft%20further%20disclaims%20all%20implied%20warranties%20including%2C%20without%20limitation%2C%20any%20implied%20warranties%20of%20merchantability%20or%20of%20fitness%20for%20a%20particular%20purpose.%20The%20entire%20risk%20arising%20out%20of%20the%20use%20or%20performance%20of%20the%20sample%20scripts%20and%20documentation%20remains%20with%20you.%20In%20no%20event%20shall%20Microsoft%2C%20its%20authors%2C%20or%20anyone%20else%20involved%20in%20the%20creation%2C%20production%2C%20or%20delivery%20of%20the%20scripts%20be%20liable%20for%20any%20damages%20whatsoever%20(including%2C%20without%20limitation%2C%20damages%20for%20loss%20of%20business%20profits%2C%20business%20interruption%2C%20loss%20of%20business%20information%2C%20or%20other%20pecuniary%20loss)%20arising%20out%20of%20the%20use%20of%20or%20inability%20to%20use%20the%20sample%20scripts%20or%20documentation%2C%20even%20if%20Microsoft%20has%20been%20advised%20of%20the%20possibility%20of%20such%20damages.%3C%2FFONT%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-1594674%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%20style%3D%22font-family%3A%20inherit%3B%22%3ERemediate%20non%20compliant%20Azure%20resources%20using%20PowerShell%20or%20CLI%20from%20within%20Azure%20Policy...%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1594674%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAnthonyWatherston%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft

How many times have you wanted to remediate a non-compliant object using Azure Policy but found you can’t because the policy language or type of object can’t be manipulated in that way. Or maybe you’ve had to write a policy with an audit effect instead of being able to create a deployment to remediate the issue. Deployment Scripts are currently in preview and allow you to execute PowerShell or CLI scripts using Azure Container Instances as part of an Azure Resource Manager template. To put it simply – now you can run a script as part of a template deployment.

 

So my thought was if I can deploy a template using an Azure Policy DeployIfNotExists effect – why can’t I deploy a Deployment Script object which then runs the code to remediate my non-compliant Azure resource?

 

Well as it turns out you can! And this allows several interesting use cases which are not possible with the default policy language such as: -

  • Deleting orphaned objects.
  • Changing the license type to Hybrid Benefit for existing Azure machines.
  • Detailed tag application – running a script to build a tag value based on many other resources or conditions.
  • Performing data plane operations on objects like Azure Key Vault and Azure Storage.

The rest of this post takes you through how I set this functionality up to ensure that all Windows virtual machines are running with Azure Hybrid Benefit enabled.

 

Azure Policy

I won’t go into the details of creating the basic Azure Policy rules however you want to ensure that the effect for your policy is DeployIfNotExists. In my case I started of with a very simple rule which will help filter out resources I’m not interested in – so as part of the rule I’m looking for resource types which are virtual machines, and that have Microsoft Windows Server as the publisher.

 

 

{
    "if": {
        "allOf": [
            {
                "field": "type",
                "equals": "Microsoft.Compute/virtualMachines"
            },
            {
                "field": "Microsoft.Compute/virtualMachines/storageProfile.imageReference.publisher",
                "equals": "MicrosoftWindowsServer"
            }
        ]
    }
}

 

 

 

For the policy effect I specify DeployIfNotExists and then retrieve the same object and apply some more checks to it. This time as part of the existence condition I’m going to check the license type field to check if it is correct.

 

 

"existenceCondition": {
    "allOf": [
        {
            "field": "Microsoft.Compute/virtualMachines/licenseType",
            "exists": true
        },
        {
            "field": "Microsoft.Compute/virtualMachines/licenseType",
            "equals": "Windows_Server"
        },
        {
            "field": "Microsoft.Compute/virtualMachines/licenseType",
            "notEquals": "[parameters('StorageAccountId')]"
        }
    ]
}

 

 

 

In the JSON above there is a check for the StorageAccountId parameter which has nothing to do with the object we’re running the policy against – but I need to include it as I’ve used it as a parameter for my policy. It will always return true, so it’s not really included as part of the evaluation and by itself won’t trigger the deployment. (If you try to add a policy without consuming all the parameters in the policy rules you will get an error).

 

The rest of a DeployIfNotExists policy contains the object I want to deploy, and it does get a bit complicated. If I was to just deploy the deployment script object it would deploy in the same resource group as my resource to be remediated which isn’t a desirable outcome as it would leave a mess of orphaned objects. The deployment script also requires a storage account to work and I don’t want my subscription littered with random storage accounts. To get around this I create a subscription level deployment – which deploys a deployment resource, which contains a nested deployment to deploy the deployment script. Confused? Here it is in a diagram and you can follow the previous links or look at the policy itself.

 

policy01.png

The best part is we don’t have to manage the Azure Container Instance as the deployment script object does that for you.

 

What I do have to worry about is the script that runs – it uses a user assigned managed identity which must have permission manage the resources, in this case I need to give it Reader and Virtual Machine Contributor rights on the subscription so it can change that license type and update the virtual machine.

 

The PowerShell script which runs is so simple: -

 

 

Param($ResourceGroupName, $VMName)
$vm = Get-AzVM -ResourceGroupName $ResourceGroupName -Name $VMName
$vm.LicenseType = "Windows_Server"
Update-AzVM -VM $vm -ResourceGroupName $ResourceGroupName

 

 

 

The container instance comes with the Az modules already or if you prefer to use the Azure CLI you can specify that in the deployment script object in the template. The script can be either be provided inline or link to an external URL. If you are linking externally and don’t want it to be in a public location, you might have to provide a SAS URL. I also specify arguments to provide to the script in a concatenated string format, the documentation on the deployment script provides some more information on these arguments but you can incorporate parameters from the policy which means the inputs can come from the non-compliant objects. As well you can choose to use an existing storage account, or you can let the deployment script create one for you.

 

 

"forceUpdateTag": "[utcNow()]",
"azPowerShellVersion": "4.1",
"storageAccountSettings": {
    "storageAccountName": "[parameters('StorageAccountName')]",
    "storageAccountKey": "[listKeys(resourceId('Microsoft.Storage/storageAccounts', parameters('StorageAccountName')), '2019-06-01').keys[0].value]"
},
"arguments": "[concat('-ResourceGroupName ',parameters('VMResourceGroup'),' -VMName ',parameters('VMName'))]",
"retentionInterval": "P1D",
"cleanupPreference": "OnSuccess",
"primaryScriptUri": "https://raw.githubusercontent.com/anwather/My-Scripts/master/license.ps1"

 

 

 

I’ve linked the policy rule here for you to review, be careful to observe the flow of the parameters as  they are provided in the policy assignment and then are passed down through each deployment object as a value.  The ‘StorageAccountName’ parameter is a good example of this.

 

Deploying the Solution

Scripts and policies are located in my GitHub repository – you can clone or download them.

The steps to deploy the required resources and policy are as below: -

  1. Ensure that you have the latest version of the Az PowerShell modules available.
  2. Connect to Azure using Connect-AzAccount
  3. Modify the deploy.ps1 script and change the values where indicated.

 

 

$resourceGroupName = "ACI" # <- Replace with your value
$location = "australiaeast" # <- This must be a location that can host Azure Container Instances
$storageAccountName = "deploymentscript474694" # <- Unique storage account name
$userManagedIdentity = "scriptRunner" # <- Change this if you don’t like the name

 

 

     4. Run the deploy.ps1 script. The output should be like below.

policy02.png

 

The script will create a resource group, storage account and deploy the policy definition.

 

Create a Policy Assignment

In the Azure portal Policy section, we can now create the assignment and deploy the policy. Click on “Assign Policy”.

 

pol1.png

 

Select the scope you want to assign the policy to and ensure that the correct policy definition is selected.

pol2.png

 

Click next and fill in the parameters – the values for this are output by the deployment script. 

pol3.png

Click next – you can leave the options as is for this screen and simply click Review and Create. On the final screen just click create.

 

The policy will be assigned, and a new managed identity will also be created which allows us to remediate any non-compliant resources.

pol4.png

 

Testing It Out

To test the policy and remediation task I have built a new Windows Server making sure that I haven’t selected to use Azure Hybrid Benefit.

pp1.png

Once the policy evaluation cycle is complete (use Start-AzPolicyComplianceScan to trigger) I can see that my new resource is now showing as non-compliant.

pp2.png

 

I can go in now and create a remediation task for this machine by clicking on Create Remediation Task. The task will launch and begin the deployment of my Deployment Script object.

pp3.png

 

I can check the resource group I specified (ACI) that the deployment script objects are created in and will be able to see the object in there.

pp4.png

 

Selecting this resource will show the details about the container instance that was launched (it’s been deleted already since the container has run) and the logs. You can also see that during the script deployment it has been able to bring the parameters I specified in the template into the script.

pp5.png

 

 And finally, we can check the virtual machine itself, and I find that the Azure Hybrid Benefit has been applied successfully.

pp6.png

 

When I look at the resource now in the Azure Policy blade it is now showing the resource as compliant.

pp7.png

 

So there you have it, what started as a theory for remediating objects has been proven to work nicely and now I have the task of looking over all my other policies and seeing what I can remediate using this method.

 

Known Issues:

  • In the example given – Azure Spot instances can’t be remediated using this process
  • My testing cases are small and in no way should reflect your own testing.
  • This is hosted on GitHub – if there are issues or you make changes please submit a PR for review.

 

Disclaimer:

The sample scripts are not supported under any Microsoft standard support program or service. The sample scripts are provided AS IS without warranty of any kind. Microsoft further disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the sample scripts and documentation remains with you. In no event shall Microsoft, its authors, or anyone else involved in the creation, production, or delivery of the scripts be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample scripts or documentation, even if Microsoft has been advised of the possibility of such damages.