1

I have a script which copies an ISO to C:\temp then mounts the ISO and stores the drive letter in a variable to be used to execute a file on the mounted drive. I do it like this:

$mountinfo = Mount-DiskImage -ImagePath $ctempiso
$driveletter = (($mountinfo | Get-volume).driveletter)
$installpath = "${driveletter}:\setup.exe"

$scriptblock = [scriptblock]::create($installpath)
Invoke-Command -ScriptBlock $scriptblock

All of this works fine if I manually set the Execution Policy to Unrestricted and then execute my PowerShell script.

To avoid having to set the execution policy, I use the following in a batch file which I run as Administrator:

PowerShell.exe -ExecutionPolicy Bypass -File \\server.contoso.local\share\test.ps1

When I run the batch file it's not able to store the drive letter and returns an error that it's not able to execute ${driveletter}:\setup.exe

I guess my first question would be why does my script work in the first method and not the second?

Secondly, is there another way to circumvent the execution policy and have my script run like I want it?

  • [Mapped drives are not available from an elevated prompt when UAC is configured to Prompt for credentials](https://learn.microsoft.com/en-us/troubleshoot/windows-client/networking/mapped-drives-not-available-from-elevated-command)? – iRon Jul 27 '21 at 15:40
  • @iRon I don't think this is the answer. UAC is indeed enabled as it is the Windows default. But I am not mapping a drive, I'm mounting an ISO-file. I'm not sure about this but I don't think it's the same thing. If mounting and mapping are the same thing, then my script should still work since I'm 'mapping' the ISO in the same usersession(see workaround 2 of your linked article). – displayed_name Jul 28 '21 at 07:00

1 Answers1

0

I found the problem... The magic word was "-passthru", it was missing in my code.

 $mountinfo = Mount-DiskImage -ImagePath $ctempiso -Passthru 
$driveletter = (($mountinfo | Get-volume).driveletter) 
$installpath = "${driveletter}:\setup.exe"  

$scriptblock = [scriptblock]::create($installpath) 
Invoke-Command -ScriptBlock $scriptblock