Pass batch variables with spaces in them to powershell script?

末鹿安然 提交于 2020-01-06 08:13:27

问题


I have a batch/powershell script that I use to make a list of all my movies and tv series but have run into a small problem, I can't have spaces in my batch variables when passing them to the powershell script.

I have tried all kinds of combos of ' and " around the variables without any luck.

My batch variables

SETLOCAL enabledelayedexpansion
SET "listfolder=%CD%\ContentList\"

SET "listname1=TVSeries"
SET "RootFolder1=\\SERVER\Storage\TV Series\"
SET "Folder1Width=70"
SET ScanFolder1=TRUE

My batch code to run and pass variables to powershell

"%listfolder%Script\Powershell.exe" -executionpolicy remotesigned -File "%listfolder%Script\folder1list.ps1" %RootFolder1% %listname1% %Folder1Width%

My powershell script code to set the variables.

param([String]$RootFolder1, [String]$listname1, [int32]$Folder1Width)

This is the error I get if I have space in %RootFolder1% path, without spaces all three variables get passed to powershell just fine.

C:\Users\hflat\Desktop\Folder Content List Maker v4.5\ContentList\Script\folder1list.ps1 : Cannot process argument
transformation on parameter 'Folder1Width'. Cannot convert value "TV" to type "System.Int32". Error: "Input string was
not in a correct format."

I found the solution here How to pass in a string with spaces into PowerShell?

What did work was to use a cmd wrapper with /S to unwrap outer quotes and removing my own path to powershell.exe

cmd /S Powershell.exe -executionpolicy remotesigned -File "%listfolder%Script\folder1list.ps1" "%RootFolder1%" "%listname1%" "%Folder1Width%"

回答1:


I found the solution here How to pass in a string with spaces into PowerShell?

What did work was to use a cmd wrapper with /S to unwrap outer quotes and removing my own path to powershell.exe

cmd /S Powershell.exe -executionpolicy remotesigned -File "%listfolder%Script\folder1list.ps1" "%RootFolder1%" "%listname1%" "%Folder1Width%"


来源:https://stackoverflow.com/questions/55640198/pass-batch-variables-with-spaces-in-them-to-powershell-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!