I've never seen a so easy script failing so royally:
$SQLServer = "localhost"
$cred = Get-Credential
invoke-sqlcmd -ServerInstance $SQLServer -Credential $cred -Query "select @@version"

the phrase says -Credentials is not recognized:
Invoke-Sqlcmd : A parameter cannot be found that matches parameter name 'Credential'.
So what is the point to have Get-Credential?
I see a lot of examples on internet and they all use it this way.
EDIT EXAMPLE: why this code is working with -Credential? Because -Credential is inside a function?
function Pause ($Message="Press any key to continue..."){ 
    "" 
    Write-Host $Message 
    $null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") 
} 
function GetCompName{ 
    $SQLServer = Read-Host "Please enter a computer name or IP" 
    CheckHost 
} 
function CheckHost{ 
    $ping = gwmi Win32_PingStatus -filter "Address='$SQLServer'" 
    if($ping.StatusCode -eq 0){$pcip=$ping.ProtocolAddress; GetCollation} 
    else{Pause "Host $SQLServer down...Press any key to continue"; GetCompName} 
} 
function GetCollation {
    #Provide Database Name 
    $DatabaseName ="master"
    #Prompt for user credentials 
    $credential = Get-Credential
    $Query = "SELECT name, collation_name FROM sys.databases;  " 
    invoke-sqlcmd -ServerInstance $SQLServer -Database $DatabaseName -Credential $credential  -Query $Query | Format-Table
}
#---------Start Main-------------- 
$SQLServer = $args[0] 
if($SQLServer){CheckHost} 
else{GetCompName}
                The Invoke-Sqlcmd cmdlet runs a script containing the languages and commands supported by the SQL Server SQLCMD utility. The commands supported are Transact-SQL statements and the subset of the XQuery syntax that is supported by the database engine.
Invoke-Sqlcmd is Now Available for MacOS & Linux in the SqlServer module.
The issue could be resulting from the fact that Microsoft has two versions of Invoke-Sqlcmd:
-Credentials parameter.-Credentials parameter is available. Looked at a couple of your recent SO questions - looks like you have the Database Engine version of the cmdlet. The the SqlServer module is not installed by default, so you have to do it manually. (there's a 'Note' section in previous hyperlink that explains some of the history behind this issue)
In a nutshell, run the following command to get the the SqlServer module:
Install-Module -Name SqlServer -AllowClobber
Make sure to include the -AllowClobber switch. It's a dumb-installer, and if you leave off the switch it will download the ~24MB package and then fail because it's overwriting the database engine version.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With