Using PSU 3.3.7
Service running as system
I want to run the script below. this script works fine in PS ISE ( $credential = Get-Credential)
SQLConnection credential is a normal domain\user account
Using it as a job gets around using alternate credentials (ps sesssion running as one user, but wanting to run the invokeSQLcmd as another user)
The error I get when I run the job is:
The running command stopped because the preference variable “ErrorActionPreference” or common parameter is set to Stop: [localhost] An error occurred while starting the background process. Error reported: Access is denied.
Job is running as default credentials and environment of windows powershell 5.1
$Database = “CM_xxx”
$ServerInstance = “server1”
$Credential = (Get-Secret SQLConnection)
$SQLQuery = "
select
CS.Name0 as [SystemName],
CS.Manufacturer0 as [Manufacturer],
CS.Model0 as [Model],
OS.Caption0 as [Caption]
from
v_GS_COMPUTER_SYSTEM CS
Inner Join
v_GS_Operating_System OS
On CS.ResourceID = OS.ResourceID
Where OS.Caption0 NOT LIKE ‘%Server%’
"
#Submit the job with creds
$job = Start-Job { Invoke-Sqlcmd -AbortOnError -Database $using:Database -Query $using:SQLQuery -ServerInstance $using:ServerInstance} -Credential $Credential | Get-Job | Wait-Job
#Receive the job
$jobInfo = Receive-Job -Job $job
$jobInfo