将带有复杂参数的批处理文件命令转换为 PowerShell [英] Convert a batch-file command with complex arguments to PowerShell

查看:39
本文介绍了将带有复杂参数的批处理文件命令转换为 PowerShell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 .bat 中有以下内容(这有效):

I have the following in .bat that is (this works):

"%winscp%" /ini=nul ^
           /log=C:\TEMP\winscplog.txt ^
           /command "open scp://goofy:changeme@10.61.10.225/ -hostkey=""ssh-rsa 2048 d4:1c:1a:4c:c3:60:d5:05:12:02:d2:d8:d6:ae:6c:5d""" ^
           "put ""%outfile%"" /home/public/somedir/somesubdir/%basename%" ^
           "exit"

我试图将其复制到这样的 powershell 脚本中:

I have tried to duplicate that into a powershell script like this:

& $winscp "/ini=nul" `
           "/log=C:\TEMP\winscplog.txt" `
           "/command" 'open sftp://goofy:changeme@10.61.10.225/ -hostkey="ssh-rsa 2048 d4:1c:1a:4c:c3:60:d5:05:12:02:d2:d8:d6:ae:6c:5d"' `
           "put `"" + $outfile + "`" /home/public/somedir/somesubdir/" + $basename `
           "exit"

当我运行 .bat 脚本时,文件将上传.

When I run the .bat script the file will upload.

当我运行 .ps1 脚本时,我得到 主机密钥与配置的密钥 ssh-rsa 不匹配

When I run the .ps1 script I get Host key does not match configured key ssh-rsa

我怀疑我没有在 powershell 中正确格式化命令,并且在 winscp 看到它时主机密钥已经损坏.

I suspect that I have not formatted the command properly in powershell and the hostkey is getting mangled by the time winscp sees it.

我检查了日志,显示的只是来自主机的主机密钥.它没有显示我正在使用的密钥.我通过更改我的主机并注意到它没有出现在日志中来确认这一点.我比较了 .bat 和 .ps1 之间的日志,不同之处在于 ps1 以上述错误终止.

I checked the log and all that is shown is the hostkey from the host. It does not show the key I am using. I confirmed that by changing my host and noting that it did not show up in the log. I compared the log between .bat and .ps1 and the difference is ps1 terminates with the error noted above.

winscp 是一个 sftp 实用程序.

winscp is a sftp utility.

推荐答案

每当我必须从 PowerShell 调用可执行文件时,我总是将参数作为列表传递,如下所示.我也使用单引号,除非我实际上是替换变量,在这种情况下我必须使用双引号.

Whenever I have to invoke an executable from PowerShell, I always pass the parameters as a list, like below. I also use single quote marks, unless I'm actually substituting variables, in which case I have to use the double quotation marks.

 & SomeUtility.exe @('param1','param2',"with$variable")

当参数中有空格时会有点棘手,因为您可能必须提供引号,以便实用程序可以正确解析命令.

It gets a little tricky when there are spaces in the parameters, since you may have to provide quotation marks so that the utility can properly parse the command.

您的示例更加棘手,因为 WinScp 想要将 /command 参数的参数括在引号中,并将其中的任何字符串括在 引号中.由于 WinScp,所有这些都需要保留.我相信以下方法会奏效.为了便于阅读,我已将参数分成多行.我还假设您已成功填充 $winscp$outfile$basename 变量.

Your example is even more tricky, since WinScp wants the argument of the /command parameter enclosed in quotation marks, and any strings inside enclosed in double quotation marks. All of that needs to be preserved, because of WinScp. I believe the following would work. I've broken up the parameters into multiple lines for readability. I'm also assuming that you've successfully populated your $winscp, $outfile, and $basename variables.

$params = @(
  '/ini=nul',
  '/log=C:\TEMP\winscplog.txt',
  '/command',
  '"open scp://goofy:changeme@10.61.10.225/ -hostkey=""ssh-rsa 2048 d4:1c:1a:4c:c3:60:d5:05:12:02:d2:d8:d6:ae:6c:5d"""',
  ('"put ""' + $outfile + '"" /home/public/somedir/somesubdir/' + $basename + '"'),
  '"exit"'
)

& $winscp $params

注意第五个参数周围的括号;这是由于那里的字符串连接操作.(如果没有括号,每个操作数都将单独添加到列表中——您可以通过查看 $params.Count 来确认这一点.)另外请记住,您将需要在日志周围加上引号文件路径,如果您必须将其更改为带有空格的内容.

Note the parentheses around the fifth parameter; this is due to the string concatenation operations there. (Without the parentheses, each operand would have been added to the list separately -- you can confirm this by looking at $params.Count.) Also keep in mind that you will need quotation marks around your log file path if you ever have to change it to something with spaces.

这篇关于将带有复杂参数的批处理文件命令转换为 PowerShell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆