Reputation: 11
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?
Upvotes: 1
Views: 66
Reputation: 11
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
Upvotes: 0