简体   繁体   中英

How to Import-Module in Windows 2008 R2 using Puppet facter?

I'm creating a puppet custom fact using ruby script to output some AD related details. I learned the ServerManager is a prerequisite module as this is not automatically added in the Windows 2008 R2 build yet. I have manually run my powershell commands in one of my 2008 R2 servers and it worked. However, I am getting the following error everytime I run it as a puppet facter.

Appreciate your help on this. Thanks!

Successful Manually run in Windows 2008 R2 server via Powershell

Import-Module ServerManager

Script

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

Error

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

Thank you.

The method you have provided worked perfectly fine for me (I tested it with Ruby 2.5.8, 2.6.6, and 2.7.1), so there must be a deeper reasoning for this not working. Nevertheless, I have a solution. In my own personal experience with Ruby and PowerShell I have always used the ` operator which also executes a command and returns the output. For example, you could do:

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

If that still doesn't work, I would turn to your installation of Windows Server, specifically making sure your environment variables haven't been messed up. As unlikely and wild as things may seem sometimes, there are always those moments that can only be explained with "because windows";)

Check that the Powershell module exists in the target server - it wasn't available by default until 2008 R2 I believe.

It should be in a folder by the same name at:

C:\Windows\System32\WindowsPowerShell\v1.0\Modules

You also might need to use 32-bit Powershell instead of 64-bit.

Okay. So I've decided to use external facts instead. And it's working fine.

You may look at Puppet's external facts page.

Update

I managed to use ruby puppet custom facter. I removed only %q[] . Here's the script.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM