Hyper-V VHD file permissions lost when moving folders
I recently found that I broke my home lab environment when I needed to free up/juggle some disk space on one of my SSDs. I carefully used robocopy to preserve the permissions on the backups of the VHDs, but on restoring them, I forgot, and used the GUI to copy and paste them back to their restored location.
When I tried to start the VM up, the Hyper-V manager greeted me with the following error
'YourVirtualMachine' failed to start.
Synthetic SCSI Controller (Instance ID XXXXX-XXXX-XXXX-XXXX-XXXX): Failed to Power on with Error 'General access denied error'.
Hyper-V Virtual Machine Management service Account does not have permission to open attachment 'C:\HYPER-V\VHD\YourVirtualMachine.vhdx'. Error: 'General access denied error'
So the error is fairly self explanatory, I broke the permissions. Each VM on the Hyper-V host, will have a group named after the VMs own unique ID. This group needs NTFS read and write permissions on the VHD or VHDx (note my VM Host is not domain joined).
So now I have 20 or so VHDX files I need to go through and assign a custom group name to each one.
Then I thought if I need to do this again in a large production environment, Id prefer to do this with powershell and automate it.
After an hour or so fiddling, some googling, Ibutchered engineered this script.
And now the permissions are correctly set for the VM to start up.
When I tried to start the VM up, the Hyper-V manager greeted me with the following error
'YourVirtualMachine' failed to start.
Synthetic SCSI Controller (Instance ID XXXXX-XXXX-XXXX-XXXX-XXXX): Failed to Power on with Error 'General access denied error'.
Hyper-V Virtual Machine Management service Account does not have permission to open attachment 'C:\HYPER-V\VHD\YourVirtualMachine.vhdx'. Error: 'General access denied error'
So the error is fairly self explanatory, I broke the permissions. Each VM on the Hyper-V host, will have a group named after the VMs own unique ID. This group needs NTFS read and write permissions on the VHD or VHDx (note my VM Host is not domain joined).
So now I have 20 or so VHDX files I need to go through and assign a custom group name to each one.
Then I thought if I need to do this again in a large production environment, Id prefer to do this with powershell and automate it.
After an hour or so fiddling, some googling, I
# Author: Andrew James Robinson
# Date: 22/10/2017
# Purpose: This script repairs permissions on VHDs that have been moved around and lost their machine ID ACL
# Revision: 1.00
# Changes: 1.00 Initial Release
cls
$VHDRoot = "c:\hyper-v\vhd\"
$GetVM = Get-VM -ComputerName localhost
Foreach ($vm in $GetVM)
{
$AJRname = $vm.Name
$AJRid = $vm.VMId
$vhdPath = (Join-path $VHDRoot ($AJRname + '.vhdx'))
$acl = (Get-Item $vhdPath).GetAccessControl("Access")
# AJR is full control required? Hopefully not, just grant read/write.
#$permission = "NT VIRTUAL MACHINE\$AJRid","FullControl","Allow"
$permission = "NT VIRTUAL MACHINE\$AJRid","Read, Write","Allow"
$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule $permission
$acl.SetAccessRule($accessRule)
$acl | Set-Acl $vhdPath
}
And now the permissions are correctly set for the VM to start up.
Comments
Post a Comment