0

I have seen some similar questions on this here on stack overflow, but I cannot get any of the answers to far to work.

I have this .ps1 file that mounts a drive and echos the drive letter (expected $driverLetter = "G" || "H" || "I"):

$mountDisk = Mount-DiskImage -ImagePath $args[0] -Passthru
$driveLetter = ($mountDisk | Get-Volume).DriveLetter
echo $driveLetter

I'm running it from this batch file:

FOR /F "usebackq delims=" %%i IN (`powershell -File ./mountDisk.ps1 "%1"`) DO  SET "d=%%i"
Echo %d%

Each time I get an empty variable. I've tried setting environment variables, but yield same result.

6
  • 2
    I'm running it from this batch file Why? Can't you just do everything you want in the PowerShell script and avoid issues of passing variables, etc? Commented May 11, 2021 at 8:12
  • Usually this would all be done in powershell, yes. However this requirement uses a few long batch files that are integrated into other systems. The system is running perfectly, and re-writing all of our batch files to powershell would be a considerable waste of money. Commented May 11, 2021 at 8:35
  • 2
    You have not told us what you are passing as %1. Whilst you think it may have been fine to omit it, is is absolutely possible that it would show the immediate reason. Get-Volume only works if your ImagePath was StorageType ISO, not VHD/VHDx etc. So the first thing you need to confirm is that you're passing an existing ISO file as the first argument to that PowerShell script. Also to use Mount-DiskImage to mount a VHD file, as opposed to an ISO, administrator privileges are required. Commented May 11, 2021 at 9:00
  • Hi Compo, understand where you are coming from here, however I didn't think to give unnecessary information about the ps1 file as it is working as intended. I do not have an issue with mounting the ISO file or echoing the letter required from the ps1 script. I can confirm it is an ISO file type passing in the %1 argument. As stated above, the expected result of echo $driverLetter is the letter that corresponds to the mounted volume. Commented May 11, 2021 at 10:08
  • 1
    what results do you get when you change ..DO SET "d=%%i" to ..do echo %%~i Commented May 11, 2021 at 11:11

2 Answers 2

2

Here's how I'd probably do it, assuming that the initial path passed to the batch file is double-quoted as necessary.

@Echo Off & SetLocal EnableExtensions & Set "ISODrv="
If /I Not "%~x1" == ".iso" (Exit /B 1) Else For %%G In ("%~1") Do If "%%~aG" GEq "d" Exit /B 2
For /F %%G In ('%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -Command "(Mount-DiskImage -ImagePath \"%~1\" -PassThru | Get-Volume).Driveletter" 2^>NUL') Do Set "ISODrv=%%G"
If Not Defined ISODrv (Exit /B 3) Else Echo %ISODrv%

Doing it this way eliminates the need for pre-creating a PowerShell script, and then any subsequent modifications to the execution policy. It only proceeds with the image mount if the received input value was an existing ISO file too. If you're running this batch file from a process which retrieves its exit code, 1 means that the input did not end with the case insensitive string .iso, 2 would mean that the input did end with the case insensitive string .iso, but it was a directory, not a file, and 3 would indicate that there was an error returning the mounted ISO image drive letter.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the elegant approach.
0

Try the following to run the cmd from the PowerShell and pathing their variables to it

# The command to pass to cmd.exe /cript
$var = "echo hello world & ping $ip & pause"
$ip = "192.168.1.1"
$var2 = "ping $ip & pause"

# Start the process asynchronously, in a new window,
# as the current user with elevation (administrative rights).
# Note the need to pass the arguments to cmd.exe as an *array*.
Start-Process -Verb RunAs cmd.exe -Args '/c', $var2, $var

1 Comment

No, this won't work. Op needs to run powershell from batch-file, not cmd from powershell.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.