【问题标题】:How can I run NuGet commands in a PowerShell script?如何在 PowerShell 脚本中运行 NuGet 命令?
【发布时间】:2019-03-16 07:28:15
【问题描述】:

我正在尝试通过 PowerShell 脚本运行一些 NuGet 命令,但 PowerShell 提示错误,例如:“Unknown command:'-set'”或“Unexpected token '-set' in expression or statement”

我正在尝试运行以下 NuGet 命令:

$nugetExe = "C:\TestAutomation\NuGet\nuget.exe"

Invoke-Expression "$nugetExe config -set http_proxy.user= t123654"
Invoke-Expression "$nugetExe config -set http_proxy.password= njhbjhb"

PS.:PowerShell 版本是 5.1,NuGet 是 4.6。**此代码通过 Windows CMD 可以正常工作。

在 CMD 命令行上,我这样运行它,它可以工作:

nuget.exe config -set http_proxy.user=t123645
nuget.exe config -set http_proxy.password= njhbjhb

【问题讨论】:

  • 但是即使我输入第三个命令它也不起作用。我将从那里删除它,所以它更清楚。
  • 如果您有有效的批处理代码,请将其发布在您的问题中。
  • 对不起,我没有通过批处理文件运行它,但我在那里添加了我如何直接通过 CMD 运行它
  • 我得到 "Unknown command: '-set'" running nuget.exe -set http_proxy.user= t123654 with cmd.exe and NuGet 4.7.1.5393 (latest stable) 和 NuGet 4.6.2.5055。
  • 多么奇怪,对我来说它通过 cmd 运行。我现在运行它以确保一切正常,我的 Nuget.config 已更新。你是从 nuget.exe 目录运行的吗?

标签: powershell nuget


【解决方案1】:

nuget.exe 没有set 参数。

NuGet CLI reference

如果您尝试设置代理,我认为您缺少 config 参数:

nuget.exe config -set http_proxy=http://my.proxy.address:port

config command (NuGet CLI)

NuGet behind a proxy

使用 PowerShell 运行:

$nugetExe = "D:\Scrap\New folder\NuGet.exe"

$Params = @(

    "config",
    "-Set", "HTTP_PROXY.USER=domain\user"
    "-configfile" "D:\scrap\New folder\my.config"
)

& $nugetExe $Params

【讨论】:

  • 实际上我已经有这个工作了,但是使用 cmd 命令行。我现在需要的是在 powershell 脚本中运行此代码。
  • 如果您愿意,您应该可以使用 Invoke-Expression 来做到这一点:stackoverflow.com/a/3593445/1426007
【解决方案2】:

我解决了。这只是关于编写它的方式。应该是这样的:

.\nuget config -Set http_proxy.user=t514144

.\nuget config -Set http_proxy.password=njhbjhb

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-13
    • 1970-01-01
    • 2019-08-15
    • 1970-01-01
    • 1970-01-01
    • 2011-06-11
    • 1970-01-01
    相关资源
    最近更新 更多