powershell - 未加载 SQLASCmdlets,因为在任何模块目录中均未找到有效的模块文件

标签 powershell powershell-5.0

我的脚本由于缺少 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
}

基于这篇文章 PowerShell to restore database

最佳答案

经过两个小时的研究,我找到了解决方案。

  1. 安装 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/

    相关文章:

    powershell - 如何在Active Directory组中填写Notes属性?

    powershell - 使用 Invoke-Command 修改 ScriptBlock 中的远程变量

    powershell - PowerShell 5中的写入主机与写入信息

    Powershell:不同环境的两个参数定义?

    powershell Foreach 对象

    powershell - Azure:如何使用 Get-AzureStorageAccount 检查 Azure 中是否存在存储帐户

    powershell - 使用 PowerShell 连接到 SFTP 并下载文件

    powershell - 列出与模式匹配的文件夹中的文件名,不包括文件内容

    powershell - 属性具有值,但无法选择它

    powershell - Powershell 5剪贴板-手动粘贴 “complex”剪贴板内容不起作用