Invoke-Sqlcmd unable to run

前端 未结 4 1728
一向
一向 2021-01-04 14:13

I have a PowerShell script that checks the CPU level of the server it is running on, and then if it is above a certain threshold it will run a SQL stored procedure and e-mai

相关标签:
4条回答
  • 2021-01-04 14:36

    Make sure you've installed Powershell module with SQL Server:

    Then try to import module SQLPS:

    Import-Module SQLPS
    

    See also: https://blogs.msdn.microsoft.com/psssql/2008/09/02/sql-server-2008-management-tools-basic-vs-complete-explained/

    0 讨论(0)
  • 2021-01-04 14:55

    This should help your debugging!

    if (-not (Get-Command Invoke-Sqlcmd -ErrorAction SilentlyContinue)) {
        Write-Error "Unabled to find Invoke-SqlCmd cmdlet"
    }
    
    if (-not (Get-Module -Name SqlServer | Where-Object {$_.ExportedCommands.Count -gt 0})) {
        Write-Error "The SqlServer module is not loaded"
    }
    
    if (-not (Get-Module -ListAvailable | Where-Object Name -eq SqlServer)) {
        Write-Error "Can't find the SqlServer module"
    }
    
    Import-Module SqlServer -ErrorAction Stop
    
    0 讨论(0)
  • 2021-01-04 15:00

    If import-module fails then you'll need to run install-module first. Installing SSMS or SSDT doesn't include sqlserver module by default.

    install-module sqlserver
    update-module sqlserver
    import-module sqlserver
    
    0 讨论(0)
  • 2021-01-04 15:03

    PowerShell v2 doesn't auto-load modules, so you need to load the relevant modules or snapins yourself:

    Add-PSSnapin SqlServerCmdletSnapin100
    Add-PSSnapin SqlServerProviderSnapin100
    

    [Source]

    0 讨论(0)
提交回复
热议问题