如何在 powershell 中将变量作为参数传递给带引号的命令

发布于 2024-08-29 23:19:13 字数 734 浏览 3 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野却迷人 2024-09-05 23:19:14

在 PowerShell 中使用外部命令行应用程序的最简单方法(在我看来)是使用别名。例如,以下内容对我来说效果很好。

Set-Alias Szip C:\Utilities\7zip\7za.exe

$Archive = 'C:\Temp\New Folder\archive.7z'
$Filename = 'C:\Temp\New Folder\file.txt'

SZip a $Archive $Filename

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.

Set-Alias Szip C:\Utilities\7zip\7za.exe

$Archive = 'C:\Temp\New Folder\archive.7z'
$Filename = 'C:\Temp\New Folder\file.txt'

SZip a $Archive $Filename

PowerShell takes care of delimiting the parameters correctly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文