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

调用Invoke-Sqlcmd遇SqlServer模块加载失败,寻求解决方案

问题:SqlServer模块加载失败导致Invoke-Sqlcmd无法执行

错误日志

PS>TerminatingError(Import-Module): "无法加载文件或程序集'file:///C:\Program Files\WindowsPowerShell\Modules\SqlServer\21.1.18209\Microsoft.SqlServer.Management.PSSnapins.dll'或其依赖项之一。灾难性故障 (异常来自 HRESULT: 0x8000FFFF (E_UNEXPECTED))"

TerminatingError(Import-Module): "无法加载文件或程序集'file:///C:\Program Files\WindowsPowerShell\Modules\SqlServer\21.1.18209\Microsoft.SqlServer.Management.PSSnapins.dll'或其依赖项之一。灾难性故障 (异常来自 HRESULT: 0x8000FFFF (E_UNEXPECTED))"
PS>TerminatingError(): "在模块'SqlServer'中找到了命令'Invoke-Sqlcmd',但无法加载该模块。有关详细信息,请运行'Import-Module SqlServer'。"
在模块'SqlServer'中找到了命令'Invoke-Sqlcmd',但无法加载该模块。有关详细信息,请运行'Import-Module SqlServer'。
Invoke-Sqlcmd : 在模块'SqlServer'中找到了命令'Invoke-Sqlcmd',但无法加载该模块。有关详细信息,请运行'Import-Module SqlServer'。
位于 C:\ECC\DataImporter.ps1:17 字符:1

  • Invoke-Sqlcmd -Query "EXEC dbo.Toshiba_ECC_Export" -ServerInstance "S ...
+ CategoryInfo          : ObjectNotFound: (Invoke-Sqlcmd:String) [], ParentContainsErrorRecordException
  + FullyQualifiedErrorId : CouldNotAutoloadMatchingModule

关联PowerShell脚本

$ErrorActionPreference = "Stop"
$dbcredential = Import-CliXml -Path "db.cred"
Start-Transcript -Path "C:\ECC\Logs\Log.txt" -Append

Write-Host "---------------ECC DATA IMPORTER------------------"
Write-Host "---------------------START---------------------"
Write-Host "---------------------Stopping old processes---------------------"

$CommandLines =  Get-CimInstance -Query "SELECT * from Win32_Process WHERE CommandLine LIKE '%eccimport%'"

foreach ($command in $CommandLines)
{
    Stop-Process -id $Command.processId -Force
}

Invoke-Sqlcmd -Query "EXEC dbo.Toshiba_ECC_Export" -ServerInstance "ServerXXXX" -Database "DatabaseXXX" -querytimeout 0 -Credential $dbcredential | 
Export-Csv -NoTypeInformation -Path "Import_ECC.csv" -Encoding UTF8

$MyFile = Get-Content .\Import_ECC.csv
$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $False

[System.IO.File]::WriteAllLines(".\Import_ECC_ENCODED.csv", $MyFile, $Utf8NoBomEncoding)

Start-Sleep -s 2

$destination = ".\Archive\Import_ECC_{0:d}.csv" -f (Get-Date)
Copy-Item ".\Import_ECC_ENCODED.csv" -Destination $destination

Start-Sleep -s 2
Write-Host "Import_ECC.csv was created."    
Write-Host $destination "was created."
Write-Host "Uploading to ECC". 
$test = java -jar ECCImport35.jar -o

Write-Host $test
Write-Host "----------------------END---------------------"
Write-Host "   "
Write-Host
解决方案

1. 修复或重新安装SqlServer模块

  • 卸载现有模块:
    Uninstall-Module -Name SqlServer -Force -AllVersions
    
  • 手动删除残留目录:C:\Program Files\WindowsPowerShell\Modules\SqlServer
  • 安装稳定兼容版本(如21.1.18178):
    Install-Module -Name SqlServer -RequiredVersion 21.1.18178 -Force -AllowClobber
    

2. 检查并更新.NET Framework依赖

SqlServer模块需要.NET Framework 4.7.2及以上版本,执行以下命令检查当前版本:

Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP' -Recurse | Get-ItemProperty -Name Version,Release -ErrorAction SilentlyContinue | Where-Object { $_.PSChildName -match '^(?!S)\p{L}' } | Select-Object PSChildName, Version, Release

若版本不足,下载安装对应版本的.NET Framework。

3. 验证模块加载权限

  • 以管理员身份启动PowerShell,执行Import-Module SqlServer -Verbose查看详细加载日志,排查权限问题。
  • 若遇到执行策略限制,运行:
    Set-ExecutionPolicy RemoteSigned -Scope CurrentUser -Force
    

4. 替代方案:使用SqlServerCmdlets模块

如果SqlServer模块持续报错,可尝试旧版兼容模块:

Install-Module -Name SqlServerCmdlets -Force

该模块的Invoke-Sqlcmd参数与原模块基本兼容,可直接替换使用。

5. 修复系统文件完整性

运行系统文件检查器,修复可能损坏的系统组件:

sfc /scannow

内容的提问来源于stack exchange,提问作者Luffy One World

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.15 15:58:23