{"id":130,"date":"2017-03-29T20:57:57","date_gmt":"2017-03-30T00:57:57","guid":{"rendered":"https:\/\/packetlost.com\/blog\/?p=130"},"modified":"2017-03-29T20:58:45","modified_gmt":"2017-03-30T00:58:45","slug":"load-powershell-profiles-for-scheduled-tasks-script-exection","status":"publish","type":"post","link":"https:\/\/packetlost.com\/blog\/2017\/03\/29\/load-powershell-profiles-for-scheduled-tasks-script-exection\/","title":{"rendered":"Load PowerShell profiles for scheduled tasks script execution"},"content":{"rendered":"<p>I stumbled upon an interesting bug today. I tested some PowerShell scripts running from task scheduler on Windows Server 2012 R2 and everything executed fine. However, when I configured the scheduled task for daily execution and checked the log output the next day, the script had failed. None of the service account&#8217;s PowerShell profiles loaded and the functions I needed were unavailable. This was odd because I tested this entire process the day before, the only difference I could think of was that I was logged in interactively with that service account when I tested the scheduled task the day before.<\/p>\n<p>Armed with this hunch of an assumption I went to the search engines and eventually found Microsoft hotfix\u00a0<a href=\"https:\/\/support.microsoft.com\/en-us\/help\/3133689\/ubpm-doesn-t-set-environmental-variables-correctly-when-you-run-scheduled-tasks-in-windows-server-2012-r2\">3133689.<\/a><\/p>\n<p>So if you ever need to load PowerShell profiles for scheduled tasks, you are going to need that hotfix. Obviously there are much cleaner ways to do what I was trying to do. Additionally having profile dependencies in your scripts or allowing interactive sessions for your service accounts are both far from best practice.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I stumbled upon an interesting bug today. I tested some PowerShell scripts running from task scheduler on Windows Server 2012 R2 and everything executed fine. However, when I configured the scheduled task for daily execution and checked the log output &hellip;<\/p>\n<p class=\"read-more\"><a href=\"https:\/\/packetlost.com\/blog\/2017\/03\/29\/load-powershell-profiles-for-scheduled-tasks-script-exection\/\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-130","post","type-post","status-publish","format-standard","hentry","category-powershell"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/packetlost.com\/blog\/wp-json\/wp\/v2\/posts\/130","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/packetlost.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/packetlost.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/packetlost.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/packetlost.com\/blog\/wp-json\/wp\/v2\/comments?post=130"}],"version-history":[{"count":2,"href":"https:\/\/packetlost.com\/blog\/wp-json\/wp\/v2\/posts\/130\/revisions"}],"predecessor-version":[{"id":132,"href":"https:\/\/packetlost.com\/blog\/wp-json\/wp\/v2\/posts\/130\/revisions\/132"}],"wp:attachment":[{"href":"https:\/\/packetlost.com\/blog\/wp-json\/wp\/v2\/media?parent=130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/packetlost.com\/blog\/wp-json\/wp\/v2\/categories?post=130"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/packetlost.com\/blog\/wp-json\/wp\/v2\/tags?post=130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}