从ruby执行powershell命令

pad*_*roy 5 ruby powershell

我试图从ruby代码执行powershell命令.Get-WmiObject -Class Win32_Product -ComputerName.-Filter"Name ='Qlik Sense Client'"| Select-Object -Property版本

它完美地给了我产品的版本.但是当我尝试从ruby执行时(在反引号中的整个命令)同样的事情:find =powershell.exe Get-WmiObject -Class Win32_Product -ComputerName . -Filter "Name=''"|Select-Object -Property version

命令中断,它无法解释红宝石中的引号,管道等.我试图逃避这些引用,但它仍然打破了.我也不知道如何逃脱这条管道.请在这里帮助我或推荐我相关的东西.非常感谢.

Max*_*Max 7

我现在已经测试过了:

require 'base64'
cmd = %{Get-WmiObject -Class Win32_Product -ComputerName . -Filter "Name='Qlik Sense Client'"|Select-Object -Property version}
encoded_cmd = Base64.strict_encode64(cmd.encode('utf-16le'))
find = `powershell.exe -encodedCommand #{encoded_cmd}`
Run Code Online (Sandbox Code Playgroud)

Powershell 需要 UTF-16LE 编码的字符串,因此您必须在 base64 转换之前从 Ruby 的编码 (UTF-8) 进行转换。


或者,您可以尝试使用shellescapefrom shellwords来转义命令,以便 shell 将其解释为单个字符串。

另一种选择是powershell.exe -Command -popen3一起使用。这将允许您编写命令并使用文件流读取它们的结果。