So I am using this Powershell script: Set-RunOnce https://www.powershellgallery.com/packages/WindowsImageConverter/1.0/Content/Set-RunOnce.ps1
When I put the driveletter hardcoded in (E:\ServerInstall.ps1) it works like a charm. But I want to be sure this script can run from any drive letter the USB gets plugged into.
How can I get this changing drive letter in the registry?
I first tried it with -ExecutionPolicy Bypass, but that didn't change much either.
I also tried this:
$getusb = Get-WmiObject Win32_Volume -Filter "DriveType='2'" . .\Set-RunOnce.ps1 Set-RunOnce -Command
'%systemroot%\System32\WindowsPowerShell\v1.0\powershell.exe ` -ExecutionPolicy Unrestricted -File $getusb.Name\ServerInstall.ps1'
--> $getusb.Name\ServerInstall.ps1 ended being hardcoded in the registry, but it didn't know what $getusb.name was, so the script didn't launch.
. .\Set-RunOnce.ps1
Set-RunOnce -Command '%systemroot%\System32\WindowsPowerShell\v1.0\powershell.exe `
-ExecutionPolicy Unrestricted -File (wmic logicaldisk where drivetype=2)
ServerInstall.ps1'
ServerInstall.ps1
script does. You should perform the(wmic logicaldisk where drivetype=2)
stuff in there and not put it in as part of the commandline. See the example on powershellgallery.com/packages/WindowsImageConverter/1.0/… – Theo"$($getusb.DriveLetter)\ServerInstall.ps1"
instead of$getusb.Name\ServerInstall.ps1
. Also consider that$getusb
could be an array if more USB disks are plugged in… – JosefZcmd
which is the Windows command processor used for processing Windows batch files.cmd.exe
cannot execute PowerShell commands.PowerShell.exe
is a completely different script interpreter. On using a batch file to runPowerShell.exe
on double clicking on batch file on USB drive it would be possible to pass the drive of storage location of batch file as argument to PowerShell with%~d0
which expands on execution to drive letter and colon. Run in a cmd windowcall /?
for help. Argument 0 is always the batch file itself. – Mofi