UTF-8 Ruby facter中的字节序列无效

时间:2017-02-14 18:53:43

标签: ruby utf-8 facter

我正在为Puppet写一个事实。

我有一个包含用户名array = ["user1", "user2", "user3"]的数组,然后是代码

array.each do |item|
    temp_data << Facter::Core::Execution.exec("net user #{item} /DOMAIN")
end
search_text = %r{comment|Local}
users_data = []
temp_data.each do |line|
    line.each_line do |num|
    users_data << (num) unless num =~ search_text
    end
end

temp_data生成正常,但是我收到错误错误puppetlabs.facter - 解析自定义事实时出错“winusers_domain”:UTF-8中的无效字节序列但它没有显示错误行是。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

我已经弄清楚了,所以我回答自己。

array.each do |item|
    temp_data << Facter::Core::Execution.exec("net user #{item} /DOMAIN")
end
search_text = %r{comment|Local}
users_data = []
temp_data.each do |line|
    line.encode!('UTF-16', 'UTF-8', :invalid => :replace, :replace => '')
    line.encode!('UTF-8', 'UTF-16')
    line.each_line do |num|
    users_data << (num) unless num =~ search_text
    end
end