我的脚本由于缺少 SQLASCmdlets 模块而失败。
The specified module 'SQLASCmdlets' was not loaded because no valid module file was found in any module directory.
为了运行这个,我知道我需要 SQLPS,我可以使用
找到它Get-Module -ListAvaiable
ModuleType Version Name
---------- ------- ----
Manifest 1.0 SQLPS
我已经安装了 - SQL Server 2016 - SQL 管理工作室 2017
错误:
VERBOSE: ******************************
VERBOSE: The specified module 'SQLASCmdlets' was not loaded because no valid module file was found in any module directory.
VERBOSE: Errors: 1
VERBOSE: ******************************
VERBOSE: System.IO.FileNotFoundException: The specified module 'SQLASCmdlets' was not loaded because no valid module file was found in any module directory.
VERBOSE: ScriptHalted The specified module 'SQLASCmdlets' was not loaded because no valid module file was found in any module directory.
当我再次重新运行我的脚本时,它就可以工作了。我怎样才能让它第一次工作?
我使用的代码是:
Import-Module "SQLPS" -DisableNameChecking
$sqlsvr = New-Object -TypeName Microsoft.SQLServer.Management.Smo.Server("WIN-C0BP65U3D4G")
$restore = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Restore
$devicetype = [Microsoft.SqlServer.Management.Smo.DeviceType]::File
$files = gci C:\psbackups
$BackupFiles = @()
foreach ($file in $files){
$restoredevice = New-Object -TypeName Microsoft.SQLServer.Management.Smo.BackupDeviceItem($file.FullName,$devicetype)
$restore.Devices.add($restoredevice)
$errcnt=0
try{
$restore.ReadMediaHeader($sqlsvr)
}
catch [System.Exception]{
Write-Output "$file is not a sql backup file `n"
$errcnt =1
}
finally {
if ($errcnt -ne 1){
Write-Output "$file is a sql backup file `n"
$BackupFiles += $file
}
$errcnt = 0
}
$restore.Devices.remove($restoredevice)
Remove-Variable restoredevice
}
最佳答案
经过两个小时的研究,我找到了解决方案。
- 安装 SqlServer 包
https://www.powershellgallery.com/packages/SqlServer/21.0.17199
2.我已更新我的脚本以在安装时选择特定版本
# import modules
if ((Get-Module -ListAvailable | where-object {($_.Name -eq 'SqlServer') -and ($_.Version.Major -gt 20) } |Measure).Count -eq 1){
# implementation of new sql modules migated into new location
Import-Module SqlServer -DisableNameChecking -Verbose
}
else{
# fallback for SQLPS
Import-Module SQLPS -DisableNameChecking -Verbose
}
- 确保我有适用于您的 PowerShell 的新 session 。
关于powershell - 未加载 SQLASCmdlets,因为在任何模块目录中均未找到有效的模块文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47890288/