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 Answer 1
Found out that Set-ExecutionPolicy -ExecutionPolicy RemoteSigned resolved the problem.
import-module sqlpsand report backSet-ExecutionPolicy -ExecutionPolicy RemoteSignedresolves the problem.