skip to Main Content

I wrote the following in my powershell function app:

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

The requirements.psd1 has the following:

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

It gives an error when i run it :

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

what do i need to include in the file to make this work ?

2

Answers


  1. Invoke-Sqlcmd Cmdlet is part of SqlServer module. Hence the final requirements.psd1 file should looks like this:

    @{
       'Az' = '10.*' 
       'SqlServer' = '22.*'    
     }
    
    Login or Signup to reply.
  2. To resolve the error, you need to install SqlServer module.

    • Install the Sqlserver module with the command Install-Module sqlserver:

    • You can check if the Invoke-sqlcmd is available in the installed SqlServer module with the command Get-Command -Module SqlServer.

    enter image description here

    enter image description here

    • Import the Module with the command Import-Module sqlserver:

    Then as @Abdul Niyas P M mentioned, add 'SqlServer' = '22.*' in the requirements.psd1.

    After following above steps, I ran the same code in my environment.

    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
    

    Response:

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search