如何在 powershell 中将变量作为参数传递给带引号的命令
我的 powershell 脚本采用以下参数:
Param($BackedUpFilePath)
传递到我的脚本中的值是:
"\123.123.123.123\Backups\Website.7z"
我有另一个变量,它是我想要提取文件的位置:
$WebsiteDeploymentFolder = "C:\example"
我正在尝试使用以下命令提取存档:
`7z x $ BackedUpFilePath -o$WebsiteDeploymentFolder -aoa
我不断收到以下错误:
错误: 找不到存档
以下有效,但我需要 $BackedUpFilePath 是动态的:
`7z x '\123.123.123.123\Backups\Website.7z' -o$WebsiteDeploymentFolder -aoa
我想我需要将 $BackedUpFilePath 带引号传递给 7z,但无论我尝试什么,它们似乎都会被删除。我身处报价地狱。
谢谢。
编辑:事实证明问题是我传入了“'\123.123.123.123\Backups\Website.7z'”。 (额外的单引号)
My powershell script takes the following parameter:
Param($BackedUpFilePath)
The value that is getting passed into my script is:
"\123.123.123.123\Backups\Website.7z"
I have another variable which is the location I want to extract the file:
$WebsiteDeploymentFolder = "C:\example"
I am trying to extract the archive with the following command:
`7z x $BackedUpFilePath -o$WebsiteDeploymentFolder -aoa
I keep getting the following error:
Error:
cannot find archive
The following works but I need $BackedUpFilePath to be dynamic:
`7z x '\123.123.123.123\Backups\Website.7z' -o$WebsiteDeploymentFolder -aoa
I think I need to pass $BackedUpFilePath to 7z with quotes but they seem to get stripped out no matter what I try. I am in quote hell.
Thanks.
EDIT: It turns out the problem was I was passing in "'\123.123.123.123\Backups\Website.7z'". (extra single quotes)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 PowerShell 中使用外部命令行应用程序的最简单方法(在我看来)是使用别名。例如,以下内容对我来说效果很好。
PowerShell 负责正确分隔参数。
The easiest way to work with external command line applications in PowerShell (in my opinion) is to use aliases. For example, the following works fine for me.
PowerShell takes care of delimiting the parameters correctly.