0

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
    Is it possible to rewrite your batch script to powershell? – Sergio Tanaka Apr 11 '19 at 20:14
  • Did you try single or double quoting the arguments? `'%RootFolder1%' '%listname1%' '%Folder1Width%'` – Squashman Apr 11 '19 at 20:23
  • I guess that could be done but would take some time. – Håvard Flatøy Apr 11 '19 at 20:24
  • ```cmd /S /C Powershell.exe -executionpolicy remotesigned -File "%listfolder%Script\folder1list.ps1" "%RootFolder1%" "%listname1%" "%Folder1Width%"``` This did work, apparently the path to powershell.exe itself made some problems. – Håvard Flatøy Apr 11 '19 at 20:26
  • 1
    Any environment variable value you want to pass as a single argument must be enclosed in `"..."`. You don't need `cmd /c` to invoke `powershell.exe`. It's unclear if you still have a problem: if you found a solution, please share it _as an answer_, if you think it'll be of interest to future readers; if not, please consider deleting your question. – mklement0 Apr 12 '19 at 01:23
  • ```"..."``` Didn't work for me before I removed my own path to powershell.exe in my code. Maybe I am blind but I can't find out how to deletet or set is as an answer... Found the answer button :) – Håvard Flatøy Apr 12 '19 at 06:00
  • I'm a bit late to the party, but the problem is the `'\'` at the end of `%RootFolder1%`. To handover an argument with spaces you have to put in quotes, but the `'\'` at the end will mask the `'"'` which results in the first argument beiing `\\SERVER\Storage\TV Series\" TVSeries 70`. If you leave out the `'\'` you can abandon `cmd /S`, but have to make up for the missing `'\'` in your script. – Thomas Aug 01 '19 at 15:25

1 Answers1

0

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%"