0

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.

Mehdi Anis
  • 358
  • 1
  • 9
  • 17
  • This sounds like a simple "how do i run a program from c#" question, or "how do i pass X Y and Z arguments to a program from c#" with a lot of extra detail. If your happy executing a batch file, take a look at this answer: https://stackoverflow.com/questions/5519328/executing-batch-file-in-c-sharp – MisterSmith Aug 28 '18 at 21:29
  • I am already using same code in your link to A) XCOPY, B)Create SYMLINK, C)Give FULL Security Access to a FOLDER - all working. So I am pretty sure I know I am doing it right. But it just when it comes to using PowerShell and that VHD change command, its not working. – Mehdi Anis Aug 29 '18 at 03:12

0 Answers0