I am not able from within .ps1 script call function Invoke-Sqlcmd.
This script is:
Install-Module -Name SqlServer
Invoke-Sqlcmd -ServerInstance "SQLSERVER\" -Database "MYDB" -Query "SELECT * FROM MYDB"
After I run script:
pwsh -f .\InvokeSqlCmd.ps1
I get message:
The term 'Invoke-Sqlcmd' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
But when I run command Invoke-Sqlcmd from general Powershell console, I can run this command without problems.
And when I run command Get-Command -Module sqlserver from within script I cannot see Invoke-Sqlcmd but when I run this command from Powershell console window I can see Invoke-Sqlcmd there.
Powershell version is
Name Value
---- -----
PSVersion 7.4.3
PSEdition Core
GitCommitId 7.4.3
OS Microsoft Windows 10.0.20348
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
When I replace Install-Module -Name SqlServer with Import-Module -Name SqlServer I get these errors:
Import-Module: Could not load file or assembly 'Microsoft.SqlServer.ConnectionInfo, Version=17.100.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91'. Could not find or
load a specific file. (0x80131621)
InvalidOperation: The variable '$binaryModule' cannot be retrieved because it has not been set.
Import-Module: Could not load file or assembly 'Microsoft.SqlServer.ConnectionInfo, Version=17.100.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91'. Could not find or
load a specific file. (0x80131621)
Add-Type: Assembly with same name is already loaded
Add-Type: Could not load file or assembly 'Microsoft.SqlServer.ConnectionInfo, Version=17.100.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91'.
Add-Type: Assembly with same name is already loaded
Add-Type: Assembly with same name is already loaded
Import-ModulenotInstall-ModuleInstall-ModulewithImport-Modulebut behaviour is the same. What meansInvoke-Sqlcmdis not known.pswh -f? Why not just.\InvokeSqlCmd.ps1? (I assume you're already in a command line.)