# Hyper-V

## Convert Dynamic VHDX to Fixed VHDX

```powershell
function Convert-VMDisksToFixed {
    param (
        [Parameter(Mandatory)]
        [string]$VMName,

        [Parameter(Mandatory)]
        [array]$Disks  # Her disk için @{Old="..." ; New="..."}
    )

    # VM SID bul
    try {
        $vmId = (Get-VM $VMName).Id.Guid
        $vmSid = "NT VIRTUAL MACHINE\$vmId"
    } catch {
        Write-Error "❌ VM '$VMName' bulunamadı."
        return
    }

    foreach ($disk in $Disks) {
        $oldPath = $disk.Old
        $newPath = $disk.New

        if (-not (Test-Path $oldPath)) {
            Write-Warning "⚠️ Disk bulunamadı: $oldPath"
            continue
        }

        $vhdInfo = Get-VHD -Path $oldPath

        if ($vhdInfo.VhdType -ne 'Fixed') {
            Write-Host "`n🔄 Converting to fixed: $oldPath -> $newPath"
            Convert-VHD -Path $oldPath -DestinationPath $newPath -VHDType Fixed -ErrorAction Stop
            Remove-Item -Path $oldPath -Force
        } else {
            Write-Host "`n🔁 Already fixed: Renaming $oldPath -> $newPath"
            Rename-Item -Path $oldPath -NewName (Split-Path $newPath -Leaf)
        }

        # NTFS izinlerini ayarla
        try {
            $acl = Get-Acl $newPath
            $accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($vmSid, "FullControl", "Allow")
            $acl.AddAccessRule($accessRule)
            Set-Acl -Path $newPath -AclObject $acl
            Write-Host "✅ NTFS izinleri eklendi: $vmSid → $newPath"
        } catch {
            Write-Warning "❌ İzin ayarlanamadı: $newPath - $_"
        }
    }

    Write-Host "`n🎯 İşlem tamamlandı."
}
```

### Usage

```powershell
$vmName = "VM01"
$disks = @(
    @{Old="E:\HYPERV\VM01\SomeDynamicDisk1.vhdx"; New="E:\HYPERV\VM01\VM01-C-DRIVE.vhdx"},
    @{Old="E:\HYPERV\VM01\SomeFixedDisk1.vhdx"; New="E:\HYPERV\VM01\VM01-D-DRIVE.vhdx"},
    @{Old="E:\HYPERV\VM01\SomeDynamicDisk2.vhdx"; New="E:\HYPERV\VM01\VM01-E-DRIVE.vhdx"}
)

Convert-VMDisksToFixed -VMName $vmName -Disks $disks
```

## Find\&Move Orphand Virtual Hard Disk Files

```powershell
function Find-AndMoveUnusedVHDs {
    param(
        [string]$SearchPath = "E:\HYPERV",
        [string]$MoveFolder = "E:\HYPERV\Orphans",
        [bool]$MoveStaledVMs = $false,
        [string]$LogFolder = "C:\Logs"
    )

    if (-not (Test-Path $SearchPath)) {
        Write-Error "Search path does not exist: $SearchPath"
        return
    }

    if (-not (Test-Path $LogFolder)) {
        New-Item -Path $LogFolder -ItemType Directory | Out-Null
    }

    $LogPath = Join-Path -Path $LogFolder -ChildPath "MovedVHDs_$(Get-Date -Format 'yyyyMMdd_HHmmss').log"
    New-Item -Path $LogPath -ItemType File -Force | Out-Null

    $usedDisks = Get-VM | Get-VMHardDiskDrive | Select-Object -ExpandProperty Path
    $vhdFiles = Get-ChildItem -Path $SearchPath -Recurse -Include *.vhd, *.vhdx -ErrorAction SilentlyContinue

    foreach ($file in $vhdFiles) {
        if ($usedDisks -notcontains $file.FullName) {
            Write-Host "Unused disk found: $($file.FullName)"

            if ($MoveStaledVMs) {
                if (-not (Test-Path $MoveFolder)) {
                    New-Item -Path $MoveFolder -ItemType Directory | Out-Null
                }

                $destinationBase = Join-Path -Path $MoveFolder -ChildPath $file.Name
                $destination = $destinationBase
                $i = 1

                while (Test-Path $destination) {
                    $destination = Join-Path -Path $MoveFolder -ChildPath ("$($file.BaseName)_$i$($file.Extension)")
                    $i++
                }

                Move-Item -Path $file.FullName -Destination $destination -Force

                $logLine = "[$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')] MOVED: '$($file.FullName)' => '$destination' | Last Modified: $($file.LastWriteTime)"
                Add-Content -Path $LogPath -Value $logLine
                Write-Host $logLine -ForegroundColor Green
            }
        }
    }

    Write-Host "İşlem tamamlandı. Log dosyası: $LogPath" -ForegroundColor Cyan
}
```

### Usage

{% code overflow="wrap" %}

```powershell
Find-AndMoveUnusedVHDs -SearchPath "E:\HYPERV" -MoveStaledVMs $true -MoveFolder "E:\HYPERV\Orphans" -LogFolder "C:\Logs"
```

{% endcode %}

## Grant-VMVhdPermission

```powershell
function Grant-VMVhdPermission {
    param (
        [Parameter(Mandatory)]
        [string]$VMName,

        [Parameter(Mandatory)]
        [string]$DiskPath
    )

    # Get VM SID
    try {
        $vmId = (Get-VM $VMName).Id.Guid
        $vmSid = "NT VIRTUAL MACHINE\$vmId"
    } catch {
        Write-Error "❌ Failed to find the VM '$VMName'."
        return
    }

    if (-not (Test-Path $DiskPath)) {
        Write-Error "❌ Disk path does not exist: $DiskPath"
        return
    }

    try {
        $acl = Get-Acl $DiskPath
        $accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($vmSid, "FullControl", "Allow")
        $acl.AddAccessRule($accessRule)
        Set-Acl -Path $DiskPath -AclObject $acl

        Write-Host "✅ Permission successfully granted: $vmSid → $DiskPath"
    } catch {
        Write-Warning "❌ Error while setting permissions: $_"
    }
}
```

### Usage

```powershell
Grant-VMVhdPermission -VMName "VM01" -DiskPath "E:\HYPERV\VM01\VM01-C-DRIVE.vhdx"
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://blog.arksoft.com.tr/scripts/hyper-v.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
