Skip to content

Commit 80d68fe

Browse files
authored
Clarifying how to run PowerShell in a CmdExec step
This was in response to https://github.com/MicrosoftDocs/sql-docs/issues/6652. I'm elaborating a little on the `parameters` that can be specified + I'm providing 3 little examples that will hopefully make it easier to understand.
1 parent 1e65e00 commit 80d68fe

1 file changed

Lines changed: 15 additions & 2 deletions

File tree

docs/powershell/run-windows-powershell-steps-in-sql-server-agent.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,24 @@ Each SQL Server Agent job step that runs PowerShell with the **sqlps** module la
6767

6868
6. In the **Process exit code of a successful command** box, enter a value from 0 to 999999.
6969

70-
7. In the **Command** box, enter powershell.exe with parameters specifying the PowerShell script to be run.
70+
7. In the **Command** box, enter PowerShell.exe with parameters specifying the PowerShell script to be run. This is pretty much what you would write at a cmd command prompt, so take a look at `PowerShell.exe -?` for all the possible options.
71+
72+
- Example 1: Runs a simple cmdlet.
73+
```cmd
74+
PowerShell.exe -Command "& { Get-Date }"
75+
```
76+
- Example 2: Runs a query via SQLCmd.exe against the current server (the example uses SQL Agent token replacement).
77+
```cmd
78+
PowerShell.exe -Command "& {&SQLCmd.exe -S $(ESCAPE_NONE(SRVR)) -Q 'select @@version'}"
79+
```
80+
- Example 3: Runs a PowerShell script (using PS7, which would have to be installed on the server). Note that the path to the script is local to the server where SQL Agent is running.
81+
```cmd
82+
PWSH.exe -ExecutionPolicy RemoteSigned -File X:\MyScripts\script001.ps1
83+
```
7184
7285
8. select the **Advanced** page to set job step options, such as: what action to take if the job step succeeds or fails, how many times SQL Server Agent should try to execute the job step, and the file where SQL Server Agent can write the job step output. Only members of the **sysadmin** fixed server role can write job step output to an operating system file.
7386
7487
## See Also
7588
7689
- [SQL Server PowerShell](sql-server-powershell.md)
77-
- [SQL Server Agent with PowerShell](sql-server-powershell.md#sql-server-agent)
90+
- [SQL Server Agent with PowerShell](sql-server-powershell.md#sql-server-agent)

0 commit comments

Comments
 (0)