Posted by: Colin Smith
Background Jobs, Powershell
Today I finished writing a couple scripts that are going to be gathering perfmon counters and pushing them into a DB for me. This is great but I am going to need to run two separate scripts that do this at the same time. So I found Start-job and End-job. these are great because now I can just write my scripts to run in an infinite loop and I will control the stop time by running a stop-job when I am ready for it to end.
I did find something that I was not expecting though. In my scripts I use invoke-sqlcmd and I have the snapin loading in my profile so I did not think I would have an issue. However, after I started one of my scripts and let it run for a while, I checked my database and no data had been inserted. this is strange since when I execute the script without start-job the data flows in as expected. I did a receive-job -id 9, 9 was the id of the running job, and it showed me the output of the script and I saw errors about invoke-sqlcmd not being a cmdlet. WHAT… So I modified my scripts with a add_snapin function and I add the snapin in that function that I call at the beggining of the scripts. Now they are working great.
Love that I can run both jobs in the background and check the status of them as I need to and stop them when I am ready to.