gpt4 book ai didi

ruby - ruby函数 'powershell'在哪里定义的?

转载 作者:数据小太阳 更新时间:2023-10-29 07:59:29 25 4
gpt4 key购买 nike

我正在为 puppet 使用 msutter DSC 模块。在阅读源代码时,我遇到了这样的代码(在 dsc_configuration_provider.rb 中):

  def create
Puppet.debug "\n" + ps_script_content('set')
output = powershell(ps_script_content('set'))
Puppet.debug output
end

什么文件定义了 powershell 函数或方法?它是内置的 ruby 吗?内置 puppet ?从类继承?我知道它被用来将文本作为命令发送到 powershell 并收集结果,但我需要查看源代码以了解如何为我的目的改进其错误日志记录,因为某些 powershell 错误被吞没并且没有警告被打印到 Puppet 日志。

文件 dsc_provider_helpers.rb 中的这些行可能是相关的:

    provider.commands :powershell =>
if File.exists?("#{ENV['SYSTEMROOT']}\\sysnative\\WindowsPowershell\\v1.0\\powershell.exe")
"#{ENV['SYSTEMROOT']}\\sysnative\\WindowsPowershell\\v1.0\\powershell.exe"
elsif File.exists?("#{ENV['SYSTEMROOT']}\\system32\\WindowsPowershell\\v1.0\\powershell.exe")
"#{ENV['SYSTEMROOT']}\\system32\\WindowsPowershell\\v1.0\\powershell.exe"
else
'powershell.exe'
end

当然这定义了 Powershell 可执行文件的位置,但没有说明它是如何调用的以及它的返回值是如何派生的。 stdout 和 stderr 合并了吗?我给出的是文本输出还是错误代码?等

最佳答案

这是 Puppet 的核心逻辑。当提供者有一个命令时,比如

命令 :powershell => 一些二进制文件

它被连接为函数 powershell(*args)

您可以通过其他提供商查看它,例如 Chocolatey :

  commands :chocolatey => chocolatey_command

def self.chocolatey_command
if Puppet::Util::Platform.windows?
# must determine how to get to params in ruby
#default_location = $chocolatey::params::install_location || ENV['ALLUSERSPROFILE'] + '\chocolatey'
chocopath = ENV['ChocolateyInstall'] ||
('C:\Chocolatey' if File.directory?('C:\Chocolatey')) ||
('C:\ProgramData\chocolatey' if File.directory?('C:\ProgramData\chocolatey')) ||
"#{ENV['ALLUSERSPROFILE']}\chocolatey"

chocopath += '\bin\choco.exe'
else
chocopath = 'choco.exe'
end

chocopath
end

然后其他位置可以像使用args 的函数一样调用chocolatey :

 chocolatey(*args)

关于ruby - ruby函数 'powershell'在哪里定义的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29544858/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com