powershell - 使用 cake-build 执行远程 powershell 脚本的问题

标签 powershell powershell-remoting cakebuild

我正在尝试执行以下 test.ps1脚本

param([string]$name,[string]$password);
write-Output "Hello $($name) my password is  $($password)";
dir c:\
New-Item c:\HRTemp\test.txt -ItemType file

在远程服务器上使用以下命令
StartPowershellScript("Invoke-Command", args =>
{
    args.Append("ScriptBlock", "{{c:\\test.ps1 -name dato -password test}}" );
});

我能够从命令行成功调用这个命令,现在我想要使用 cake 脚本。

我正在使用 Cake.Powershell加入。

当我尝试用一​​个花括号执行它时 {c:\\test.ps1 -name dato -password test} ,我收到错误:
Error: Input string was not in a correct format.
当我用两个花括号尝试它时
{{c:\\test.ps1 -name dato -password test}}
输出如下
Executing: Invoke-Command -ScriptBlock {{c:\test.ps1 -name dato -password test}}
但是,当我检查远程服务器 test.txt 文件时没有创建。

你知道为什么会这样吗?

最佳答案

这是由 ProcessArgumentBuilder 对花括号的不同处理造成的。由 Cake.Powershell 插件内部使用,以及 Cake 的内部记录器内部使用的格式解析器。

我向 Cake.Powershell 提交了一个 PR,它现在已经合并并发布了一个新版本,所以升级到 version 0.2.7将为您解决这个问题。

然后,您应该能够使用以下内容:

StartPowershellScript("Invoke-Command", args =>
{
    args.Append("hostname").Append("-ScriptBlock {c:\\test.ps1 -name dato - password test}");
});

虽然日志将包含双大括号,但实际命令将仅使用单大括号并且应该正确运行。

关于powershell - 使用 cake-build 执行远程 powershell 脚本的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39854014/

相关文章:

powershell - 使用 Powershell 的调用命令运行 Windows 更新时访问被拒绝

powershell - 捕获 RPC 服务器不可用错误 HRESULT : 0x800706BA

javascript - 转换为换行符

powershell - azure VM中未加载指定的模块 'SQLPS'

powershell - Powershell脚本给出错误但仍返回 “Successfully”

c# - 如何在执行 powershell 脚本的 msdeploy post sync 命令中转义引号?

macos - Cake 在 OS X 上构建失败,找不到 msbuild.exe

json - 如何在 powershell 中替换 json 文件中的属性

powershell - Invoke-Command 和直接查询的区别

c# - .NET Framework (4.8) 的 Cake runner 不获取环境变量