如何将模块添加到Azure for powershell Function应用程序中的requirements.psd 1?

vuktfyat  于 12个月前  发布在  Shell
关注(0)|答案(2)|浏览(103)

我在我的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

我需要在文件中包含什么才能使此工作?

bt1cpqcv

bt1cpqcv1#

Invoke-Sqlcmd Cmdlet是SqlServer模块的一部分。因此,最终的requirements.psd1文件应该是这样的:

@{
   'Az' = '10.*' 
   'SqlServer' = '22.*'    
 }
w46czmvw

w46czmvw2#

在本地创建Powershell Azure函数并将其部署到Azure functionapp。
要遵循的步骤:
1.打开命令提示符=>使用命令az login登录到Azure。
1.使用npm i -g azure-functions-core-tools@4 --unsafe-perm true命令安装Azure function core tools
1.使用命令创建Azure函数:

func init --worker-runtime powershell
func new

创建函数的文件:

需要安装SqlServer module才能解决。

  • 使用命令**Install-Module sqlserver**安装Sqlserver module
  • 您可以使用命令**Get-Command -Module SqlServer**检查Invoke-sqlcmd是否在已安装的SqlServer module中可用。

  • 使用命令**Import-Module sqlserver**导入模块:

然后如 *@Abdul Niyas P M * 提到的,在requirements.psd1中添加'SqlServer' = '22.*'
按照上面的步骤,我在我的环境中运行了相同的代码。

param($Timer)

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

if ($Timer.IsPastDue) {
    Write-Host "PowerShell timer is running late!"
}

Write-Host "PowerShell timer trigger function ran! TIME: $currentUTCtime"

$connectionString = "<sql_server_connection_string>"
$querystr = "select * from Persons;"
Import-Module sqlserver
Invoke-Sqlcmd -ConnectionString $connectionString -Query $querystr -OutputSqlErrors $true

回复:

相关问题