I have encountered a similar situation, and I hope maybe this will help you...
I backed up my old workstation's C drive to a VHD with a drive label of "Old_BG7". The VHDX file currently resides on a server here in my datacenter (\\bgserver\e$\Backup\NetOpsBG7_C$.vhdx). I desired to have this virtual disk mounted as my E: drive every time I logged in. The issue I ran into, however, was that Diskpart did not always assign the same Volume Number to my virtual disk.
Here is what I have done to work around that issue, and so far, it has been working for me.
I created a scheduled task which runs a batch script (C:\Users\BG\AppData\Roaming\BG_Stuff\NetOpsBG7_E$.cmd) at logon with elevated privileges. That batch script looks like this:
@echo off
diskpart /s "%userprofile%\AppData\Roaming\BG_Stuff\NetOpsBG7_C$.cfg"
for /f "tokens=2,3* delims= " %%i in ('echo list volume ^| diskpart ^| find /i "Old_BG7"') do if not %%j==E (echo select volume=%%i>"%userprofile%\AppData\Roaming\BG_Stuff\tmpScript.cfg" && echo assign letter=E>>"%userprofile%\AppData\Roaming\BG_Stuff\tmpScript.cfg" && echo exit>>"%userprofile%\AppData\Roaming\BG_Stuff\tmpScript.cfg" && diskpart /s "%userprofile%\AppData\Roaming\BG_Stuff\tmpScript.cfg" && del /q "%userprofile%\AppData\Roaming\BG_Stuff\tmpScript.cfg")
exit 0
The first command in the batch script calls this standard diskpart script (C:\Users\BG\AppData\Roaming\BG_Stuff\NetOpsBG7_C$.cfg):
select vdisk file="\\bgserver\e$\Backup\NetOpsBG7_C$.vhdx"
attach vdisk
exit
The real magic, however, happens in the second line of the batch script. I'm echoing the command 'list volume' into Diskpart, piping that output through the 'FIND' command to locate my VHD disk label "Old_BG7", then using 'FOR' to return two variables: the Volume Number and the Drive Letter of the mounted VHD. Then the 'IF' command takes over and checks to see whether the VHD's Drive Letter matches "E" or not. If it does, it just quits and exits; but if not, it writes, executes, and then deletes a temporary Diskpart script (C:\Users\BG\AppData\Roaming\BG_Stuff\tmpScript.cfg).
So, for example, if the VHD's assigned Drive Letter was not "E", and its Volume Number turned out to be 5, then the tmpScript would look like:
select volume=5
assign letter=E
exit
Hope this helps...
-=B