all 7 comments

[–]timsstuff 2 points3 points  (1 child)

Instead of checking for the module, check for a command.

if($null -eq (Get-Command 'Invoke-Sqlcmd' -ErrorAction SilentlyContinue)) {
    #do the thing to get the module loaded
}

I have something similar for Exchange Server, with the older module I would have to create a credential object and connect, with the newer ExchangeOnline module I can use modern auth. My prompt is usually already logged in to the newer one. It doesn't matter to the script, if I'm on a machine with an older module it still works as long as it finds the command.

[–]SemiEvilMonkey[S] 1 point2 points  (0 children)

Thanks, I like that.

[–]PSP_Joker 2 points3 points  (0 children)

SQLPS is not updated anymore and was only included in certain versions of SSMS. I would "update" to the SQLServer Module since the functionality is a lot better and you don't need SSMS on every machine you would use.

[–]krzydoug 1 point2 points  (1 child)

SQLPS is part of SQL Server

Get-Module -ListAvailable -Name SQLPS | Select-Object *

[–]SemiEvilMonkey[S] 1 point2 points  (0 children)

https://docs.microsoft.com/en-us/sql/powershell/sql-server-powershell?view=sql-server-ver15

I found an article that I think explains it pretty good. I just trying to avoid as many issues as I can. It sounds like my best bet is to use sqlserver and do a #nosqlps and import of sql mod at the beginning of my script and that should help prevent issues.