question

DBANovice avatar image
DBANovice asked

Save PowerShell variables Within xp_cmdshell

Does anyone know if it is possible to save POWERSHELL variables when using xp_cmdshell when calling powershell code? The code below returns NULL when run from within SSMS but it should return HELLO as that is the variable I am storing in $test declare @sql as varchar(255) set @sql = 'powershell.exe -c "$test = ''hello'' $test"' EXEC xp_cmdshell @sql
tsqlssmspowershellxp_cmdshell
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
DBANovice avatar image
DBANovice answered
I figured it out. You need to put it on the same line and separate with a ";" declare @sql as varchar(255) set @sql = 'powershell.exe -c "$test = ''hello''; $test"' EXEC xp_cmdshell @sql
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.