You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Azure PowerShell函数应用的requirements.psd1中添加模块解决Invoke-Sqlcmd报错?

问题

在PowerShell函数应用中编写了以下代码:

param($Timer)
$currentUTCtime = (Get-Date).ToUniversalTime()

if ($Timer.IsPastDue) {
    Write-Host "PowerShell timer is running late!"
}
# Write an information log with the current time.
 Write-Host "PowerShell timer trigger function ran! TIME: $currentUTCtime"


$connectionString = "<connectionstring>"
$querystr = "select * from MyTable
Invoke-Sqlcmd -ConnectionString $connectionString -Query $querystr -OutputSqlErrors $true

requirements.psd1内容如下:

@{
    'Az' = '10.*'    
  }

运行时出现如下错误:

ERROR: The term 'Invoke-Sqlcmd' is not recognized as a name of a cmdlet, function, script file, or executable program.     
System.Management.Automation.CommandNotFoundException

请问需要在requirements.psd1中添加什么内容才能解决该问题?

解决方案

Invoke-Sqlcmd cmdlet属于SqlServer PowerShell模块,当前你的requirements.psd1仅引入了Az模块,因此需要添加SqlServer模块的依赖声明。

修改后的requirements.psd1内容如下:

@{
    'Az' = '10.*'
    'SqlServer' = '22.*' # 可根据需求指定兼容版本,或用'*'匹配最新版本
}

另外注意你的PowerShell代码存在语法错误:$querystr的字符串未闭合引号,会导致后续代码执行异常,需修正为:

$querystr = "select * from MyTable"
Invoke-Sqlcmd -ConnectionString $connectionString -Query $querystr -OutputSqlErrors $true

内容的提问来源于stack exchange,提问作者Traci

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 11:28:15