0

I have tried without success but I suspect I have many errors at the moment. However, can anyone tell me if it is possible to separate PowerShell scripts across a series of SQL Server Agent job steps, i.e.

Job 1 Step 1 --> declare & assign a variable

Job 1 Step 2 --> use variable in a query/command

For example I am trying build a query here

powershell.exe -ExecutionPolicy Bypass -Command ' $query = ''select BackupFile from MyDB.dbo.tbl'''

and use it in this command here:

powershell.exe -ExecutionPolicy Bypass -Command ' $res = Invoke-Sqlcmd2 -ServerInstance myserver -Query $query | select-object -expand BackupFile'

I'm using basic steps to get the idea for something complicated.

Thank you for your help!

beehive
  • 93
  • 3
  • 11
  • you could `$global:` scoped variables: https://www.sapien.com/blog/2013/03/06/first-rule-of-powershell-scoping-rules/ – trebleCode Mar 15 '18 at 20:39
  • Global variables are only global to the session that they are created in. – EBGreen Mar 15 '18 at 20:46
  • 2
    @beehive, I think we will need more information about what your goal is and why you feel you need to do it this way. – EBGreen Mar 15 '18 at 20:48

0 Answers0