从ruby执行powershell命令
发布时间:2020-12-16 21:08:03 所属栏目:百科 来源:网络整理
导读:我试图从 ruby代码执行powershell命令. Get-WmiObject -Class Win32_Product -ComputerName. -Filter“Name =’Qlik Sense Client’”| Select-Object -Property版本 它完美地给了我产品的版本.但是当我尝试从ruby执行时(在反引号中的整个命令)同样的事情:
我试图从
ruby代码执行powershell命令.
Get-WmiObject -Class Win32_Product -ComputerName. -Filter“Name =’Qlik Sense Client’”| Select-Object -Property版本 它完美地给了我产品的版本.但是当我尝试从ruby执行时(在反引号中的整个命令)同样的事情: 命令中断,它无法解释ruby中的引号,管道等.我试图逃避这些引用,但它仍然打破了.我也不知道如何逃脱这条管道. 解决方法
我现在测试了这个:
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}` Powershell期望UTF-16LE编码的字符串,因此您必须在base64转换之前转换Ruby的编码(UTF-8). 或者,您可以尝试使用shellwords中的shellescape来转义命令,以便shell将其解释为单个字符串. 另一种方法是使用powershell.exe -Command – 使用popen3.这将允许您编写命令并使用文件流读取其结果. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |