将其中带有空格的批处理变量传递给Powershell脚本? [英] Pass batch variables with spaces in them to powershell script?

查看:105
本文介绍了将其中带有空格的批处理变量传递给Powershell脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个批处理/powershell脚本,可以用来列出所有电影和电视剧的列表,但是遇到一个小问题,将批处理变量传递给powershell脚本时,我的批处理变量中不能有空格.

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.

我的批处理变量

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

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

我要运行的批处理代码并将变量传递给powershell

My batch code to run and pass variables to powershell

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

我的Powershell脚本代码来设置变量.

My powershell script code to set the variables.

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

如果我在%RootFolder1%路径中有空格,而没有空格,则将这三个变量传递给powershell都很好,这是我得到的错误.

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

我在这里找到了解决方案如何传入在PowerShell中有空格的字符串?

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

起作用的是使用带有/S的cmd包装器来解开外部引号,并删除我自己的powershell.exe路径.

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

推荐答案

我在这里起作用的是使用带有/S的cmd包装器来解开外部引号,并删除我自己的powershell.exe路径.

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

这篇关于将其中带有空格的批处理变量传递给Powershell脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆