locked
Hyper-V file location RRS feed

  • Question

  • Hi,

    I put together using google a little script for creating a VM. The script works fine! My issue is that i want it to run on multiple servers and not all of them have the same drive letter. Some of them use the D: while others the E:

    The good thing is that the script is running from the same drive letter. How could i change it in order to represent the current letter?

    I was thinking that if i know the exact subfolder of the script (which i do) then i could change the "D:\VM\$vm_name" to "..\..\VM\$vm_name"

    any ideas would be appreciated!

    Thanks.

    CLS
    $vm_name= Read-Host "Enter new VM name"
    #get path to xml file
    $vmconfig = get-item "D:\Templates\template\Virtual Machines\*.xml" | select -ExpandProperty Fullname
    $vm_path= "D:\VM\$vm_name"
    New-Item -ItemType directory -Path $vm_path
    
    Import-VM -Path $vmconfig -copy -GenerateNewId -SmartPagingFilePath $vm_path -SnapshotFilePath $vm_path -VhdDestinationPath $vm_path
    Get-VM "Template" | Rename-VM -NewName $vm_name -PassThru
    $vhd = Get-VMHardDiskDrive -VMName $vm_name
        $oldName = $vhd.Path
        $newName = ($vm_path + "\" + $vm_name + "_C.vhdx")
        Rename-Item $oldName $newName
    Set-VMHardDiskDrive –VMName $vm_name –Path $newName 
    

    Tuesday, May 23, 2017 7:30 AM

Answers

  • $pwd is the current folder

    $pwd.Drive.Name is the current drive


    \_(ツ)_/

    • Marked as answer by VasileiosG Wednesday, May 24, 2017 7:26 AM
    Tuesday, May 23, 2017 4:30 PM

All replies

  • $pwd is the current folder

    $pwd.Drive.Name is the current drive


    \_(ツ)_/

    • Marked as answer by VasileiosG Wednesday, May 24, 2017 7:26 AM
    Tuesday, May 23, 2017 4:30 PM
  • Hello,

    i spoke a bit too fast. When i am trying to run it, it takes "C" by default and of course if i try to just execute it, requires admin rights. Is there a way to go around these two issues?

    If i open powershell as admin and cd to the folder and execute it, then it works fine but that is not very automated.

    Thank you!

    Wednesday, May 24, 2017 10:26 AM