-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGet-VhdSecurityPermission.ps1
83 lines (68 loc) · 2.91 KB
/
Get-VhdSecurityPermission.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
function Get-VhdSecurityPermission {
<#
.SYNOPSIS
Gets NTFS security permissions for a VHD or VHDX file associated with a virtual machine (VM).
.DESCRIPTION
This function retrieves NTFS security permissions for a VHD or VHDX file associated with a VM.
.PARAMETER VMName
The name of the virtual machine.
.PARAMETER Disk
The full path and filename of the VHD or VHDX file.
.EXAMPLE
Get-VhdSecurityPermission -VMName "MyVM"
Retrieves and displays the NTFS security permissions for the VHD associated with the VM named "MyVM".
.NOTES
Author: Ken Teague
Date: September 19, 2023
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[string] $VMName,
[Parameter()]
[string] $Disk
)
# Check if the user supplied the -VMName parameter, if not, prompt for it
if ([string]::IsNullOrWhiteSpace($VMName)) {
$VMName = Read-Host "Enter the name of the VM"
}
# Check if the user supplied the -Disk parameter, if not, obtain the list of VHDs attached to the VM
if ([string]::IsNullOrWhiteSpace($Disk)) {
$AttachedVHDs = (Get-VMHardDiskDrive -VMName $VMName | Select-Object -ExpandProperty 'Path')
if ($AttachedVHDs.Count -eq 0) {
Write-Host "No VHDs attached to the VM."
exit
}
}
# Check each VHD for proper permissions
foreach ($VHDPath in $AttachedVHDs) {
$Acl = Get-Acl -Path $VHDPath
$VMID = (Get-VM -Name $VMName).VMId
# Check if the VMID has read and write permissions
$HasPermissions = $Acl.Access | Where-Object { $_.IdentityReference -eq "NT VIRTUAL MACHINE\$VMID" -and $_.FileSystemRights -eq "Read,Write,Synchronize" }
Write-Host "VMName: $VMName"
Write-Host "VHD: $VHDPath"
if ($HasPermissions -eq $null) {
Write-Host "Result: " -NoNewline
Write-Host "Mismatch (ACL does not contain VMID)" -ForegroundColor Yellow
$AddPermission = Read-Host "Add permissions for VM with ID $VMID to this VHD? (Y/n)"
if ($AddPermission -eq 'Y' -or $AddPermission -eq '') {
# Add NTFS permissions for the VHD
$Rule = New-Object System.Security.AccessControl.FileSystemAccessRule(
"NT VIRTUAL MACHINE\$VMID",
"Read,Write,Synchronize",
"Allow"
)
$Acl.AddAccessRule($Rule)
Set-Acl -Path $VHDPath -AclObject $Acl
Write-Host "Permissions for VM with ID $VMID added to the VHD: $VHDPath." -ForegroundColor Green
} else {
Write-Host "No permissions added for VM with ID $VMID on the VHD: $VHDPath." -ForegroundColor Yellow
}
} else {
Write-Host "Result: " -NoNewline
Write-Host "Match (ACL contains VMID)" -ForegroundColor Green
}
Write-Host
}
}