Can I use variable for Windows_task start_time?


#1

powershell_script ‘calc start time’ do
code <<-EOH
$starttime = (get-date).AddMinutes(5).ToString(“HH:mm”)
EOH
end

What is correct syntax?
windows_task ‘windowspatches’ do
command ‘Powershell.exe my powershell script’
frequency :once
start_time “$starttime”
end
Thanks!!!


#2

Try powershell_exec

result = powershell_exec("your_powershell_command")

The result ruby variable will contain the output of your powershell command and can be used in any chef resource


#3

Thanks!!


#4

Note that result would give you back an object in that case, would need to do:

result = powershell_exec('(get-date).AddMinutes(5).ToString("HH:mm")').result

You probably don’t even need to drop into PowerShell to achieve this, here’s a pure Ruby example:

windows_task 'windowspatches' do
  command 'Powershell.exe my powershell script'
  frequency :once
  start_time (Time.now + 5*60).strftime("%H:%M")
end

#5

Stuart,

Thanks so much!! I am new to Chef/Ruby/Powershell and very much appreciate the help…

I am using below for day 2 provisioning and only want to run one time. If I understand correctly, this script would get scheduled every time client runs. Any ideas on how to handle?

Thanks


#6

You would probably want to use a not_if or only_if Guard here on your resource. See https://docs.chef.io/resource_common.html#guards for examples.


#7

Thanks!