Hi All, Is there anyone who has written a custom function calling powershell that returns a value? The case we are trying to use the puppet module *scheduled_task *and it works really well! The problem is that we don''t want to declare our password in the declaration so we would like to set username and password with a task performed after the task is registered. Everything works almost fine except for when we set the user and password , the next runtime the scheduled_task resource will change back to system since nothing is declared. So everything runs everytime puppet agent runs. We would like to a custom functions that checks if the scheduled_task is created and runing under proper user. If that is the case we dont ned to run *scheduled_task *resource. So in pseudo code something like this: $result = sheduledtaskexists("MyMagicTask") unless $result scheduled_task { ''MyMagicTask'': ensure => present, enabled => true, command => ''d:\Scheduled_Tasks\MyMagicTask.bat'', trigger => { schedule => daily, every => 1, # Defaults to 1 start_date => ''2011-08-31'', # Defaults to ''today'' start_time => ''03:00'', # Must be specified } } end module Puppet::Parser::Functions newfunction(:sheduledtaskexists, :type => :rvalue) do |args| argument =args[0].to_s() # "MyMagicTask" cmd = ''C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -NonInteractive -NoLogo -ExecutionPolicy Bypass -Command . d:\\tools\\check_task_exist.ps1; Task-Exists argument'' #should be repla value = %x[ #{cmd} ].to_s().chomp result = value end end Powershell snippet... not proper code.. function Task-Exists([string]$taskname) { [Boolean]$isFound = $false [xml]$tasks = schtasks /query /TN $taskname /XML if($tasks) { $isFound = $true } $isFound } Has someone done something like this? -- 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+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users. For more options, visit https://groups.google.com/groups/opt_out.
Alessandro Franceschi
2013-Sep-06 12:36 UTC
[Puppet Users] Re: Custom Function that uses powershell
Functions are executed on the PuppetMaster when compiling the catalog. I don''t think you can. You may try some workaround calling a powershell command from a custom fact and use the fact value to manage your logic. On Thursday, September 5, 2013 6:32:21 PM UTC+2, carllindelof wrote:> > Hi All, > > Is there anyone who has written a custom function calling powershell that > returns a value? > > The case we are trying to use the puppet module *scheduled_task *and it > works really well! > > The problem is that we don''t want to declare our password in the > declaration so we would like to set username and password with a task > performed after the task is registered. > > Everything works almost fine except for when we set the user and password > , the next runtime the scheduled_task resource will change back to system > since nothing is declared. > > So everything runs everytime puppet agent runs. > > We would like to a custom functions that checks if the scheduled_task is > created and runing under proper user. If that is the case we dont ned to > run *scheduled_task *resource. > > > So in pseudo code something like this: > > $result = sheduledtaskexists("MyMagicTask") > > unless $result > > > scheduled_task { ''MyMagicTask'': > ensure => present, > enabled => true, > command => ''d:\Scheduled_Tasks\MyMagicTask.bat'', > trigger => { > schedule => daily, > every => 1, # Defaults to 1 > start_date => ''2011-08-31'', # Defaults to ''today'' > start_time => ''03:00'', # Must be specified > } > } > > > end > > > > > module Puppet::Parser::Functions > newfunction(:sheduledtaskexists, :type => :rvalue) do |args| > > argument =args[0].to_s() # "MyMagicTask" > > cmd = ''C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -NonInteractive -NoLogo -ExecutionPolicy Bypass -Command . d:\\tools\\check_task_exist.ps1; Task-Exists argument'' #should be repla > > value = %x[ #{cmd} ].to_s().chomp > result = value > end > end > > > > Powershell snippet... not proper code.. > > > function Task-Exists([string]$taskname) > { > > [Boolean]$isFound = $false > [xml]$tasks = schtasks /query /TN $taskname /XML > > if($tasks) > > { > $isFound = $true > > } > > $isFound > > } > > > Has someone done something like this? > > > > > >-- 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+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users. For more options, visit https://groups.google.com/groups/opt_out.
Rob Reynolds
2013-Sep-09 15:15 UTC
Re: [Puppet Users] Re: Custom Function that uses powershell
The next version of facter will bring in executable external facts (which can be a powershell script) that would allow you to do the fact workaround. Another thing you can do is set environment variables on the machine that would contain user/pass and use those. And then there is also ENC On Fri, Sep 6, 2013 at 7:36 AM, Alessandro Franceschi <al@lab42.it> wrote:> Functions are executed on the PuppetMaster when compiling the catalog. > I don''t think you can. > You may try some workaround calling a powershell command from a custom > fact and use the fact value to manage your logic. > > > On Thursday, September 5, 2013 6:32:21 PM UTC+2, carllindelof wrote: >> >> Hi All, >> >> Is there anyone who has written a custom function calling powershell that >> returns a value? >> >> The case we are trying to use the puppet module *scheduled_task *and it >> works really well! >> >> The problem is that we don''t want to declare our password in the >> declaration so we would like to set username and password with a task >> performed after the task is registered. >> >> Everything works almost fine except for when we set the user and password >> , the next runtime the scheduled_task resource will change back to system >> since nothing is declared. >> >> So everything runs everytime puppet agent runs. >> >> We would like to a custom functions that checks if the scheduled_task is >> created and runing under proper user. If that is the case we dont ned to >> run *scheduled_task *resource. >> >> >> So in pseudo code something like this: >> >> $result = sheduledtaskexists("**MyMagicTask") >> >> unless $result >> >> >> scheduled_task { ''MyMagicTask'': >> ensure => present, >> enabled => true, >> command => ''d:\Scheduled_Tasks\MyMagicTas**k.bat'', >> trigger => { >> schedule => daily, >> every => 1, # Defaults to 1 >> start_date => ''2011-08-31'', # Defaults to ''today'' >> start_time => ''03:00'', # Must be specified >> } >> } >> >> >> end >> >> >> >> >> module Puppet::Parser::Functions >> newfunction(:**sheduledtaskexists, :type => :rvalue) do |args| >> >> argument =args[0].to_s() # "MyMagicTask" >> >> cmd = ''C:\Windows\System32\**WindowsPowerShell\v1.0\**powershell.exe -NoProfile -NonInteractive -NoLogo -ExecutionPolicy Bypass -Command . d:\\tools\\check_task_exist.**ps1; Task-Exists argument'' #should be repla >> >> value = %x[ #{cmd} ].to_s().chomp >> result = value >> end >> end >> >> >> >> Powershell snippet... not proper code.. >> >> >> function Task-Exists([string]$taskname) >> { >> >> [Boolean]$isFound = $false >> [xml]$tasks = schtasks /query /TN $taskname /XML >> >> if($tasks) >> >> { >> $isFound = $true >> >> } >> >> $isFound >> >> } >> >> >> Has someone done something like this? >> >> >> >> >> >> -- > 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+unsubscribe@googlegroups.com. > To post to this group, send email to puppet-users@googlegroups.com. > Visit this group at http://groups.google.com/group/puppet-users. > For more options, visit https://groups.google.com/groups/opt_out. >-- Rob Reynolds Developer, Puppet Labs Join us at PuppetConf 2014, September 23-24 in San Francisco -- 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+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users. For more options, visit https://groups.google.com/groups/opt_out.