【发布时间】:2022-01-04 19:50:06
【问题描述】:
我正在尝试作为某些 xp_cmdshell 代码执行:
declare @url varchar(250) = 'https://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=439d4b804bc8187953eb36d2a8c26a02'
declare @c varchar(1000) = N'powershell.exe -noprofile -executionpolicy bypass '
+ N'-command (Invoke-WebRequest -Uri '''+@url+'''-UseBasicParsing).content'
print @c
exec xp_cmdshell @c
但这是我得到的错误:
The string is missing the terminator: '.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : TerminatorExpectedAtEndOfString
'appid' is not recognized as an internal or external command,
operable program or batch file
当我尝试在 powershell 中执行相同的操作时,我得到了预期的响应(见图)
(Invoke-WebRequest -Uri 'https://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=439d4b804bc8187953eb36d2a8c26a02'-UseBasicParsing).content
xp_cmdshell 的预期输出应该是 json
【问题讨论】:
-
你为什么想要首先在SSMS中执行它?
-
我正在尝试将 API 响应放入表中。我不是 C# 开发人员,我想在 ssms 中使用 sql server 2019 和 json 功能。
-
然后将数据推送到 SQL Server。调用 Web API 并不是 SQL 的设计目的。使用不同的语言将数据获取到 SQL Server,然后您可以在 SQL Server 中使用它。
-
如果您想在 SQL Server 中“玩转” JSON,那么只需自己获取 JSON 并将值放入一个变量中以 玩转。不要把
xp_cmdshell塞进去。 -
旁注,如果您将命令粘贴到命令提示符中(
xp_cmdshell正在使用),you get the same error。
标签: sql json powershell api ssms