My PowerShell script only works when running from ISE

EGr picture EGr · Apr 1, 2013 · Viewed 18.2k times · Source

I can't post all of the script contenet, but the basic idea is that it downloads JSON and converts it to objects using the ConvertFrom-Json cmdlet. Some objects are filtered out, and the rest are written to an XML/XLS document (in the Excel 2003 format). This file is then attached to an email and sent to various people.

The problem I'm having is that it only works when run from the Powershell ISE. Once I try setting up a scheduled task, calling it from cmd, or even calling it from powershell, the attached file is completely empty. It is as if some functions do not run (the one that loops through and creates all rows).

I can continue to run from ISE for the time being, but the idea of this script is to send out an automatic email that will require no intervention. Any ideas as to what could be causing this?

Answer

Stephen Price picture Stephen Price · Oct 30, 2014

You need to run the script "dot sourced" which can be done like this

powershell.exe -noexit -file c:\test.ps1

or

pwershell.exe -noexit ". c:\test.ps1"

See this link under the -File section http://technet.microsoft.com/en-us/library/hh847736.aspx