首页 > 解决方案 > 如何使用 Puppet 因子在 Windows 2008 R2 中导入模块?

问题描述

我正在使用 ruby​​ 脚本创建一个 puppet 自定义事实来输出一些与 AD 相关的详细信息。我了解到 ServerManager 是一个必备模块,因为它还没有自动添加到 Windows 2008 R2 构建中。我已经在我的一台 2008 R2 服务器中手动运行了我的 powershell 命令并且它工作正常。但是,每次我将它作为傀儡因素运行时都会出现以下错误。

感谢您对此的帮助。谢谢!

成功通过 Powershell 在 Windows 2008 R2 服务器中手动运行

Import-Module ServerManager

脚本

if ( $operatingsystemrelease == '2008 R2' )
     Facter::Core::Execution.execute(%q[powershell Import-Module ServerManager])
end

错误

error while resolving custom fact "mycustomfact": execution of command "powershell Import-Module ServerManager" failed: command not found.
Source: Facter

谢谢你。

标签: rubypowershellpuppetwindows-server-2008-r2facter

解决方案


您提供的方法对我来说效果很好(我用 Ruby 2.5.8、2.6.6 和 2.7.1 对其进行了测试),因此必须有更深层次的原因导致它不起作用。不过,我有一个解决方案。根据我个人使用 Ruby 和 PowerShell 的经验,我一直使用 ` 运算符,它也执行命令并返回输出。例如,您可以这样做:

if ( $operatingsystemrelease == '2008 R2' )
    `powershell.exe Import-Module ServerManager`
end

如果这仍然不起作用,我会转向您的 Windows Server 安装,特别是确保您的环境变量没有被弄乱。尽管事情有时看起来不太可能和疯狂,但总有一些时刻只能用“因为窗户”来解释;)


推荐阅读