简体   繁体   中英

How to run powershell script from batch file while the ps script path has spaces and path is derived from %~dp0%

I have a batch file Install.bat which calls the GetVersion.ps1 powershell script. These two scripts are in the same folder (C:\\Install_Media) and i am calling the powershell script by getting the directory where the batch file is(using %~dp0%).

The following code works good if there is no space in the path where these files are located. If there is a space in the path then shellscript is not getting executed (eg C:\\Install Media). The script stops saying that The term 'C:\\Install' is not recognized as the name of a cmdlet, function, script..

@ECHO OFF
set SRC_DIR=%~dp0%
Powershell set-executionPolicy remotesigned
Powershell %SRC_DIR%\GetSLMClientVersion.ps1
Powershell set-executionPolicy restricted

Error ScreenShot

Parameter expansion uses just a single % character at the beginning of the expression. Also, you don't need to change the execution policy back and forth. powershell.exe has a parameter for temporarily overriding the execution policy. And spaces in the path are best handled by putting the path in quotes, as TheIncorrigible1 pointed out in the comments.

Change your code to this:

@echo off
powershell.exe -ExecutionPolicy Bypass -File "%~dp0\GetSLMClientVersion.ps1"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM