如何在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
相关产品推荐
相关产品推荐

