Functions/Install-VmFromIsoFile.ps1


function Install-VmFromIsoFile {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
        [ArgumentCompleter( {
                param ( $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters )
                Get-VM "$wordToComplete*" | Sort-Object Name | ForEach-Object { "`"$($_.Name)`"" }
            })]
        [string[]]
        $VMName,
        [Parameter(Mandatory)] [string] $IsoFile
    )

    Stop-VM $VMName -Force -TurnOff


    $vmDvdDrive = Get-VMDvdDrive $VMName
    Set-VMFirmware $VMName -FirstBootDevice $vmDvdDrive

    $vmDvdDrive | Set-VMDvdDrive -Path $IsoFile


    # Start-Job -ScriptBlock { Start-Sleep 1200; $vmDvdDrive | Set-VMDvdDrive -Path "" }


    Start-VM $VMName

}