0
votes

I'm trying to run Invoke-Sqlcmd on a computer and got the following error.

PS C:\> Invoke-Sqlcmd -server server1 'select 1 a'
Invoke-Sqlcmd : The 'Invoke-Sqlcmd' command was found in the module 'SqlServer', but the module could not be loaded.
For more information, run 'Import-Module SqlServer'.
At line:1 char:1
+ Invoke-Sqlcmd -server server1 'select 1 a'
+ ~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Invoke-Sqlcmd:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CouldNotAutoloadMatchingModule

However, Import-Module failed?

PS C:\> Import-Module SqlServer

Import-Module : The following error occurred while loading the extended type data file: Error in TypeData
"Microsoft.SqlServer.Management.Smo.Agent.Job": The member LastRunDuration is already present.
Error in TypeData "Microsoft.SqlServer.Management.Smo.Agent.JobStep": The member LastRunDurationAsTimeSpan is already present.
Error in TypeData "Microsoft.SqlServer.Management.Smo.NamedSmoObject": The member DefaultKeyPropertySet is already present.
Error in TypeData "Microsoft.SqlServer.Management.Smo.NamedSmoObject": The member DefaultDisplayPropertySet is already present.
Error in TypeData "Microsoft.SqlServer.Management.Smo.NamedSmoObject": The member DefaultDisplayProperty is already present.
At line:1 char:1
+ Import-Module SqlServer
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Import-Module], RuntimeException
+ FullyQualifiedErrorId : FormatXmlUpdateException,Microsoft.PowerShell.Commands.ImportModuleCommand

1
I installed sql server management studio 2017 on the computer - ca9163d9
It looks like this is a common issue if you have the previous SQLPS module installed. SQLPS came with pre-2016 (but post-2008) versions of sql server. - Maximilian Burszley
@TheIncorrigible1 I don't know. I guess it comes with SSMS? - ca9163d9
Try import-module sqlps and report back - Maximilian Burszley
@TheIncorrigible1 Found out Set-ExecutionPolicy -ExecutionPolicy RemoteSigned resolves the problem. - ca9163d9

1 Answers

3
votes

Found out that Set-ExecutionPolicy -ExecutionPolicy RemoteSigned resolved the problem.