简体   繁体   English

运行带参数的 PowerShell 脚本

[英]Run PowerShell script with parameters

I have a PowerShell script that needs a path passed to it as parameter and surrounded with double quotation because path may be contains space.我有一个 PowerShell 脚本,需要将路径作为参数传递给它并用双引号括起来,因为路径可能包含空格。 But my problem is when the path is root of drive for example "E:\" the passed parameter in the script converted to E:" (must be E:\ ) how to solve this problem? When I use single quotation the passed strings is 'E:\' and contains the quotations!但我的问题是当路径是驱动器的根目录时,例如"E:\"脚本中的传递参数转换为 E:”(必须是E:\ )如何解决这个问题?当我使用单引号时传递的字符串是'E:\'并包含引号!

Please help请帮忙

my code sample:我的代码示例:

in test.ps1
Write-Host ($args[0])


Result:
.\test.ps1 "E:\"
E:"

.\test.ps1 'E:\'
'E:\'

.\test.ps1 "'E:\'"
'E:\'

.\test.ps1 "E:\Test"
E:\test

I call the script from registry like this: powershell -File "test.ps1" "%V"我像这样从注册表调用脚本: powershell -File "test.ps1" "%V"

I solved problem by changing "%V" to '%V' and in the script I use trim function to removes single quotations我通过将"%V"更改为'%V' %V”解决了问题,在脚本中我使用 trim function 删除单引号

$Arg0 = ($args[0])
$Path = $Arg0.trim("'")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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