Welcome › Forums › General PowerShell Q&A › out-file doesnt work when running in an SQL Server Agent job
- This topic has 1 reply, 2 voices, and was last updated 1 month, 2 weeks ago by
Participant.
-
AuthorPosts
-
-
November 30, 2020 at 10:51 am #275331
Hi,
Im trying to establish some logging in an sql server agent job running some powershell code but I keep failing, repeatably 🙁
I’m running this code in explorer Ise and it work fine, no problems
<p class=”p1″> Try {</p>
<p class=”p2″> Restore-DbaDatabase -SqlInstance MIPYDB5 -Path ‘\\172.16.64.200\clustsql01-backup\CLUSTSQL01$AG01\AspNetServices\’ -DestinationDataDirectory I:\MSSQL15.MSSQLSERVER\MSSQL\DR-DATA -DestinationLogDirectory J:\MSSQL15.MSSQLSERVER\MSSQL\DR-Log -DirectoryRecurse -RestoredDatabaseNamePrefix DR_ -DestinationFilePrefix DR_ -norecovery -withreplace -ErrorAction Stop -WarningAction stop -verbose | Out-File -FilePath “I:\MSSQL15.MSSQLSERVER\MSSQL\Log\DR_AspNetServices_$((Get-Date).ToString(‘yyyyMMdd_hhmmss’)).txt”</p>
<p class=”p3″> [System.Environment]::Exit(0)</p>
<p class=”p4″>} catch {</p>
<p class=”p3″> [System.Environment]::Exit(1)</p>
<p class=”p1″>}</p>
<p class=”p5″> When I run in in SQL Server agent as a subsystem = powershell job under a proxy account it fails. If I remove the out-file part of the job and run the code in sql server agent it works fine, so the problem is the out-file part…</p>
Anyone, any ideas???Regards
Michael
-
November 30, 2020 at 11:30 am #275360
When running as the proxy, how is it mapping I: and J:? This proxy account is just executing the task, not logging on the server and running any logon scripts. The proxy also would need to access to the remote shares.
-
-
AuthorPosts
- You must be logged in to reply to this topic.