Prelude: I have read all C#-PowerShell-Process-StartInfo links and tried them in my code, nothing worked.
I am coding a small personal app to copy 2012 R2 VMs (turned off) from one location to another location of the same server. I run the app "As Administrator", using "XCOPY" to copy whole VM folder from source to destination, delete old SYMLINK and create NEW SYMLINK pointing to new XML file, giving VM & Children Folders "Everyone" "Full access" - just to be safe. All working well EXCEPT assigning the NEW VHD location in the XML file.
First I tried editing the XML file and putting the NEW path of the VHD, but that doesn't work - there are some other underlying things - don't wann go that route.
What worked manually/from console apps: If this command is RUN As Administrator it changes the VHD location of a VM
A) PowerShell : Set-VMHardDiskDrive -VMName C -Path 'O:\Virtual Machines\C\Virtual Hard Disks\C.vhdx'
B) CommandPromt: PowerShell.exe Set-VMHardDiskDrive -VMName C -Path 'O:\Virtual Machines\C\Virtual Hard Disks\C.vhdx'
C) Create a PS1 file with : Set-VMHardDiskDrive -VMName C -Path 'O:\Virtual Machines\C\Virtual Hard Disks\C.vhdx', call the PS1 file from PowerShell
D) Create a BAT file with : PowerShell.exe Set-VMHardDiskDrive -VMName C -Path 'O:\Virtual Machines\C\Virtual Hard Disks\C.vhdx'
Unfortunately, whenever I tried each of the above A, B, C & D none worked from my OWN C# WinForm app (.net 4.6.2). For each a,b,c,d I tried ShowNoWindow True/False, with/Without Runas, username/password - pretty much all combinations - but nothing work. It doesn't error out, but the VHD location doesn't change. If I just run the command myself from cmd/powershell, it does change the VHD path in the XML and I can see the change in VM Manager app.
If anyone can shed some light, that will be helpful. Thanks.