2
votes

I have a script that fails due to missing SQLASCmdlets module.

The specified module 'SQLASCmdlets' was not loaded because no valid module file was found in any module directory.

In order to run this I know I needed SQLPS which I can found using

Get-Module -ListAvaiable

ModuleType Version    Name
---------- -------    ---- 
Manifest   1.0        SQLPS 

I have installed - SQL Sever 2016 - SQL Management Studio 2017

Error:

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.

When I rerun my script again it works. How do I make it work on first time?

The code that I used is:

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
}

Based on this article PowerShell to restore database

3

3 Answers

1
votes

After 2 hours of research I have found the solution.

  1. Install SqlServer package

https://www.powershellgallery.com/packages/SqlServer/21.0.17199

2.I have updated my script to choose specific version in case it is installed

# 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
}
  1. Ensure I have new session for your PowerShell.
0
votes

Yes SQLPS module is not installed by default - you have 2 options:

1) install the module 2) install SSMS - the standard tool for MSSQL administration.

Also if you have 32-bit SSMS installation, need explicitly to add the module if run the default 64-bit PowerShell or run 32-bit.

0
votes

It appears the module SQLASCmdlets is missing from SQL 2016 and SQL 2017 installation. It's on SQL 2014 so I looked to see what commands are in this module and here's the list:

Add-RoleMember Backup-ASDatabase Invoke-ASCmd Invoke-ProcessCube Invoke-ProcessDimension Invoke-ProcessPartition Merge-Partition New-RestoreFolder New-RestoreLocation Remove-RoleMember Restore-ASDatabase

So if you don't need any of these cmdlets from this module, we can stop the error from showing by commenting out the line that tries to load it. I'm using SQL 2017 so the location of the file for me is C:\Program Files (x86)\Microsoft SQL Server\140\Tools\PowerShell\Modules\SQLPS\SqlPsPostScript.PS1

Once you open it you will see where it tries to load the module SQLASCmdlets so just comment this line out and you should be good. Someone needs to tell Microsoft they forgot to ship this module with their SQL installation package