I have a PowerShell script which is running a SQL query from a Windows Server 2008R2 Server; when logged on to the Server with a domain Administrator/ Service Account the script runs and completes from both basic PS and ISE. If I schedule the same script using the same Administrator/ Service Account from Task Scheduler the query does not return any value. The scheduled task is running as ‘Run with Highest Privileges.’ PowerShell is V4.
$Result = Invoke-Sqlcmd -ServerInstance Server -Database Database -Query $Query
Where run manually $Result always returns a value and yet from the Scheduled Task $Result is always null. In both instances the remainder of the script runs successfully and completes.
$ErrorActionPreferencehas been set in this script which would be useful to know. Perhaps just not returning anything? Maybe in how$queryis being generated.... I don't know for sure. - MattAdd-Snapinpart of your script, or is it loaded automagically when using the console/ISE (e.g. via Profile)? Is this 32/64bit PowerShell? Invoke-SqlCmd how-to - G42