Chef- Powershell background job is not working

powershell_script 'NETSTAT' do
code <<-EOH
start-job -scriptblock{
 NETSTAT 2 
}
EOH
end

This code is running successfully but not working as expected.
NETSTAT process should be running but actually not running

If you want a persistently running instance of netstat running in the background that outlasts the chef run, I’d advise using a scheduled task. Running your powershell_script resource is essentially the equivilent of running:

powershell -command { start-job -scriptblock{ NETSTAT 2 } }

If you run this in an interactive powershell console, you will not see NETSTAT in the process list after the command retrurns because its parent powershell process has exited.

If you leverage the windows_task resource and have a task launch netstat 2, you should see that process continue to run even after the chef converge has completed.

1 Like

Dear Matt_Wrock,

Thanks for the support

1. windows_task resource on windows server 2012 r2 is not working.
My opscode is:

windows_task 'chef-client' do
  user 'Administrator'
  password 'XXXXXXXX'
  command 'NETSTAT 2'
  run_level :highest
  frequency :minute
end

After running the recipe following error occurred.
"No resource or method named windows_task' forChef::Recipe “start”’ "

2. I have no idea about schedule task
if you can give a Sample example , I will be thankful to you.

Oh my apologies I neglected to mention that the windows_taskresource is in the windows cookbook and you would therefore want to pull that cookbook in. Check the readme for info specifically related to creating scheduled tasks.

Thank for your continuous support.

Now windows_task resource is running.

opscode to start the Process:

windows_task 'NETSTAT-Process' do
  command 'NETSTAT 2'
  run_level :highest
  frequency :minute
end

opscode to stop the Process

windows_task 'NETSTAT-Process' do
action :delete
end

powershell_script 'Stop NETSTAT' do
  code <<-EOH
  Stop-Process -processname NETSTAT -Force
 EOH
end

But command that is used in start process snippet is not working when logged the NETSTAT result into another file in particular interval
that means this opscode is not working.

windows_task 'NETSTAT-Process' do
  command 'NETSTAT 2> F:\\Testing\\netstat.log'
  run_level :highest
  frequency :minute
 end

In this case, infact NETSTAT process is not running as process.

Is there any way to call the pwershell script or run the script code?