[英]how do I use Regex in powershell commands with ruby using puppet
I need to create a fact in puppet to get which versions of .netcore are installed in the servers.我需要在 puppet 中创建一个事实,以获取服务器中安装了哪些版本的 .netcore。
Facter.add('common_dotnetcore_version') do
confine :kernel => 'windows'
powershell = 'C:\Windows\system32\WindowsPowershell\v1.0\powershell.exe'
setcode do
dotnetVersion = Hash.new
if Facter::Util::Resolution.which('dotnet')
dotNetList = Facter::Core::Execution.exec(%Q{#{powershell} -command "(((dotnet --list-runtimes ) -replace '(?!\d*\.\d*)[A-Za-z([():\]\\)]') | select -Unique)"})
dotNetList.each_line { |line| dotnetVersion[line] = true }
end
dotnetVersion
end
end
please note that when I run this command in PowerShell请注意,当我在 PowerShell 中运行此命令时
(((dotnet --list-runtimes ) -replace '(?!\d*\.\d*)[A-Za-z([():\]\\)]') | select -Unique)
I got this output:我得到了这个 output:
.. 2.1.26 ..
.. 2.1.28 ..
.. 3.1.13 ..
.. 3.1.15 ..
.. 5.0.4 ..
.. 5.0.6 ..
But when its executed by Puppet I got this output:但是当它被 Puppet 执行时,我得到了这个 output:
Microsoft.AspNetCore.All 2.1.26 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.28 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.26 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.28 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.1.15 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.1.26 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.1.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.1.13 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 5.0.4 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
It's not doing the -replace with the Regex它不是用正则表达式做 -replace
Does anyone know why Puppet does not recognize the Regex and |有谁知道为什么 Puppet 不能识别正则表达式和 | select -Unique? select - 独一无二?
The problem is that backslashes inside a Ruby %Q{...}
quoted string have a meaning.问题是 Ruby %Q{...}
引用的字符串中的反斜杠是有意义的。 For example, %Q{\n}
is a newline.例如, %Q{\n}
是换行符。 So the backslashes in your regex are being interpreted by the %Q{...}
string before they get to PowerShell.因此,正则表达式中的反斜杠在到达 PowerShell 之前被%Q{...}
字符串解释。
If you look at the string:如果您查看字符串:
puts %Q{'(?!\d*\.\d*)[A-Za-z([():\]\\)]'}
# '(?!d*.d*)[A-Za-z([():]\)]'
you'll see that the backslashes are going away.你会看到反斜杠消失了。
The easiest thing to do is to escape all the backslashes by doubling them:最简单的做法是通过将所有反斜杠加倍来逃避所有反斜杠:
dotNetList = Facter::Core::Execution.exec(%Q{#{powershell} -command "(((dotnet --list-runtimes ) -replace '(?!\\d*\\.\\d*)[A-Za-z([():\\]\\\\)]') | select -Unique)"})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.