Passing Powershell Variables into Puppet manifest

587 views
Skip to first unread message

tom....@itv.com

unread,
Oct 27, 2015, 3:31:29 PM10/27/15
to Puppet Users
Hi all

Im having difficulty passing powershell variables into Puppet, while using the Powershell module.

What I need to achieve is this in powershell. It is basically a script that executes a check to see if a windows module is installed, and if not, copies it from a source directory. 

This is the powershell script

$check = Get-WindowsFeature -name Net-Framework-Core
$source = "\\server\directory\directory2\directory3"
$destination = "C:\source3"
if ($check.Installed -ne "True") {
    Copy-Item $source -Destination $destination -Recurse
    Install-WindowsFeature Net-Framework-Core
    Remove-Item $destination -Recurse
    }

This is my puppet manifest

class profiles::dotnetrun {


$check = "Get-WindowsFeature -name Net-Framework-Core"

$source = "\\\\server\\directory\\directory2\\directory3"

$destination = "C:\source3"


exec { 'dotnet':

        command => 'Copy-Item \$source -Destination \$destination -Recurse',

        onlyif => '\$check.Installed -ne "True"',

        provider => powershell,


}

}


ITV plc (Registration No. 4967001) (ITV) is incorporated in England and Wales with its registered office at The London Television Centre, Upper Ground, London SE1 9LT. Please visit www.itv.com for further information.

This email and any attachments are intended solely for the addressee and may contain confidential, copyrighted and/or privileged information.  If you have received this message in error, please notify us and delete it.  Recipients should scan the email and any attachments for their own protection. 

This email does not conclude a binding agreement.  The views or opinions presented in this email do not necessarily represent those of ITV.

Rob Reynolds

unread,
Jan 7, 2016, 8:41:37 PM1/7/16
to puppet...@googlegroups.com
On Tue, Oct 27, 2015 at 10:27 AM, <tom....@itv.com> wrote:
Hi all

Im having difficulty passing powershell variables into Puppet, while using the Powershell module.

What I need to achieve is this in powershell. It is basically a script that executes a check to see if a windows module is installed, and if not, copies it from a source directory. 

This is the powershell script

$check = Get-WindowsFeature -name Net-Framework-Core
$source = "\\server\directory\directory2\directory3"
$destination = "C:\source3"
if ($check.Installed -ne "True") {
    Copy-Item $source -Destination $destination -Recurse
    Install-WindowsFeature Net-Framework-Core
    Remove-Item $destination -Recurse
    }

This is my puppet manifest

class profiles::dotnetrun {


$check = "Get-WindowsFeature -name Net-Framework-Core"

This is a Puppet variable when it is in the manifest, not a PowerShell variable.
 

$source = "\\\\server\\directory\\directory2\\directory3"

$destination = "C:\source3"


exec { 'dotnet':

        command => 'Copy-Item \$source -Destination \$destination -Recurse',

        onlyif => '\$check.Installed -ne "True"',


Probably due to some confusion on Puppet variables versus PowerShell variables. You cannot define a PowerShell variable in the manifest as you have tried above.

Another issue is that you want these to be used as variables from Puppet, but they are likely being passed as literal strings. You should instead look into removing the variables and passing the entire string or updating these calls to use double-quoted strings so that variable interpolation will occur (the literal string $source is passed when you use '$source', the variable value is passed when you use "$source") and using escape characters where appropriate. 

Another option is to look into calling PowerShell files in either a templates or files directory of your module.

Also calling $check.Installed is not likely to work as it is shown in your manifest. If you instead surround it as "\$($check).Installed", you may have more luck.

HTH.

 

        provider => powershell,


}

}


ITV plc (Registration No. 4967001) (ITV) is incorporated in England and Wales with its registered office at The London Television Centre, Upper Ground, London SE1 9LT. Please visit www.itv.com for further information.

This email and any attachments are intended solely for the addressee and may contain confidential, copyrighted and/or privileged information.  If you have received this message in error, please notify us and delete it.  Recipients should scan the email and any attachments for their own protection. 

This email does not conclude a binding agreement.  The views or opinions presented in this email do not necessarily represent those of ITV.

--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/5672654e-6c18-4fdd-a2c7-d4c62cba1a03%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Rob Reynolds
Developer, Puppet Labs

Reply all
Reply to author
Forward
0 new messages