Tried running a PowerShell script to remediate virtual machines in our Azure tenant. Our VMs need to have Encryption at Host enabled.
I wrote a script to get all VMs, check if they had encryption enabled, stop the VM, update the property, then restart the VM (if it was running). I had about 30 VMs that didn't remediate, but some VMs did even in the same subscription. I modified the code to try and force the VMs that didn't remediate to update their settings as follows:
$subscriptionID = "mysubid"
Set-AzContext $subscriptionID
# List remaining non-compliant VMs
$subVMs = ("list of vm names")
foreach ($name in $subVMs) {
$vm = Get-AZVM -Name $name
$thisVm = Get-AzVM -ResourceGroupName $vm.ResourceGroupName -Name $vm.Name -Status
$powerState = $thisVm.Statuses.DisplayStatus
Stop-AzVM -ResourceGroupName $vm.ResourceGroupName -Name $vm.Name -Force
$vm.Name
$vm.SecurityProfile.EncryptionAtHost = $true
Update-AzVM -ResourceGroupName $vm.ResourceGroupName -vm $vm
if ($powerState -eq "VM running") {
Start-AzVM -ResourceGroupName $vm.ResourceGroupName -Name $vm.Name
}
}
The problem I'm running into is I get this error for each of these VMs that didn't originally remediate.
"The property 'EncryptionAtHost' cannot be found on this object. Verify that the property exists and can be set."
I have double checked that the feature is registered at the subscription levels with:
Get-AzProviderFeature -FeatureName "EncryptionAtHost" -ProviderNamespace "Microsoft.Compute"
And the subscriptions have the feature enabled.
I know VM size can be an issue but we have VMs on the same OS with the same size, one is encrypted at host, the other gives me the error. Looking for a workaround that doesn't involve just manually changing all of the VMs.
Edit: Let me clarify more the issue I'm running into. It's not that I don't know which VMs are non-compliant, it's that when I run the script to change them, the script isn't recognizing that the VM even has the property to change. I ran this code to see if I could figure out why:
foreach ($name in $subVMs) {
$vm = Get-AZVM -Name $name
$vm.Name
if ($vm.SecurityProfile.EncryptionAtHost -eq $true) {
Write-Output "Already Encrypted"
}
if ($vm.SecurityProfile.EncryptionAtHost -eq $false){
Write-Output "Not Encrypted"
}
Write-Output ""
}
This returned an output similar to the following:
VM1 Not Encrypted
VM2 Already Encrypted
VM3
VM4 Already Encrypted
See how VM3 doesn't have either output message? These are the VMs that are returning that the property cannot be found on the object. I'm trying to figure out why so that I can adjust my script to change these VMs as well.
Edit: After running the Get-AZVM command for a specific VM that is not encrypting. Get-AZVM Output
