1

i am running a powershell command through SQL,please find below code

     declare @sql varchar(200)

set @sql = 'powershell.exe Get-WmiObject -Class MSFT_PhysicalDisk -Namespace root\Microsoft\Windows\Storage| select FriendlyName'


EXEC xp_cmdshell @sql

its gives me output

 output
'select' is not recognized as an internal or external command,
operable program or batch file.
NULL

why it is giving like that if i remove select than powershell command is working

1 Answer 1

1

You need to put the arguments in quotes like this:

declare @sql varchar(200)
set @sql = 'powershell.exe "Get-WmiObject -Class MSFT_PhysicalDisk -Namespace root\Microsoft\Windows\Storage| select FriendlyName"'
EXEC xp_cmdshell @sql

Just now it's basically running two separate commands which are:

powershell.exe Get-WmiObject -Class MSFT_PhysicalDisk -Namespace root\Microsoft\Windows\Storage|

Then

select| FriendlyName

So the second command is invalid and causing the error, the quotes group them together to make it process it as one.

Sign up to request clarification or add additional context in comments.

2 Comments

but what about SQL
@deepti I've updated my answer so its set in the variable for you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.