0

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
15
  • 4
    Installing a module doesn't import it. Commented Sep 2, 2024 at 11:51
  • 3
    You need to use Import-Module not Install-Module Commented Sep 2, 2024 at 11:57
  • Guys, I changed Install-Module with Import-Module but behaviour is the same. What means Invoke-Sqlcmd is not known. Commented Sep 2, 2024 at 12:10
  • 1
    Why, out of interest, are you using pswh -f? Why not just .\InvokeSqlCmd.ps1? (I assume you're already in a command line.) Commented Sep 2, 2024 at 12:15
  • 1
    What account are you running the task under from Task Scheduler? As mentioned by @AlwaysLearning , installs can be scoped to the user, so if you installed module scoped to your user, but you're running the task under a different user, that module won't exist. Commented Sep 2, 2024 at 12:28

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.