Posted On June 15, 2020

Loading the SQL Server Management Objects (SMO)

kimconnect 0 comments
blog.KimConnect.com >> Codes , Database >> Loading the SQL Server Management Objects (SMO)
function loadSMO{

    $ErrorActionPreference = "Stop"     
    $sqlpsRegistry="HKLM:\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.SqlServer.Management.PowerShell.sqlps"  
    try{
        if (Get-ChildItem $sqlpsRegistry -ErrorAction "SilentlyContinue")  {  
            throw "SQL Server Provider for Windows PowerShell is not installed."  
            } 
        else{  
            $registryItem = Get-ItemProperty $sqlpsRegistry  
            $sqlpsPath = [System.IO.Path]::GetDirectoryName($registryItem.Path)  
            }  
  
        $assemblylist =  
            "Microsoft.SqlServer.Management.Common",  
            "Microsoft.SqlServer.Smo", 
            "Microsoft.SqlServer.Dmf", 
            "Microsoft.SqlServer.Instapi", 
            "Microsoft.SqlServer.SqlWmiManagement",  
            "Microsoft.SqlServer.ConnectionInfo",  
            "Microsoft.SqlServer.SmoExtended",  
            "Microsoft.SqlServer.SqlTDiagM",  
            "Microsoft.SqlServer.SString",  
            "Microsoft.SqlServer.Management.RegisteredServers",  
            "Microsoft.SqlServer.Management.Sdk.Sfc",  
            "Microsoft.SqlServer.SqlEnum",  
            "Microsoft.SqlServer.RegSvrEnum",  
            "Microsoft.SqlServer.WmiEnum",  
            "Microsoft.SqlServer.ServiceBrokerEnum",  
            "Microsoft.SqlServer.ConnectionInfoExtended",  
            "Microsoft.SqlServer.Management.Collector",  
            "Microsoft.SqlServer.Management.CollectorEnum",  
            "Microsoft.SqlServer.Management.Dac",  
            "Microsoft.SqlServer.Management.DacEnum",  
            "Microsoft.SqlServer.Management.Utility"  
  
        foreach ($assembly in $assemblylist)  {  
            $assembly = [Reflection.Assembly]::LoadWithPartialName($assembly)  
            }  
  
        Push-Location  
        cd $sqlpsPath  
        update-FormatData -prependpath SQLProvider.Format.ps1xml   
        Pop-Location
        write-host "SMO successfully loaded."
        return $true
    }
    catch{
        write-warning "$error"
        return $false
        } 
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

PowerShell: Scan for Available or Unavailable IPs

This function is a demonstration of multi-tasking using PowerShell. The program will ping multiple targets…

PowerShell: Measure File Copying Time

The scripty: $sourcesAndDestinations=@("C:\Users\kimconnect\Desktop\test\test1 C:\Users\kimconnect\Desktop\test\test2","C:\Users\kimconnect\Desktop\test\test2 C:\Users\kimconnect\Desktop\test\test3")$testPath="C:\Users\kimconnect\Desktop\test\test1"$switchesMirrorDirectories="/MIR /SEC /W:3 /FFT "$dateStamp = Get-Date -Format "yyyy-MM-dd-hh-mm"$currentPath=(Get-Item -Path ".\").FullName$log="/LOG+:$currentPath\robocopy-log-$dateStamp.txt"function…

Restore User Script

echo offSET RAR="C:\Program Files\WinRAR\RAR.EXE"IF NOT EXIST P:\ (NET USE P: \\WDFS1\BACKUP)cd %USERPROFILE%%RAR% x -y p:\%username%\desktopstuff.rarC:cd…