我正在尝试执行以下 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/