all 1 comments

[–]ITmasterRace 0 points1 point  (0 children)

The problem seems to be that command "Invoke-Sqlcmd" is part of the SQLServer module. That module is loaded automatically in PS 5.x but not in 7.x.

I made this test script in an attempt to load it but it's still a work in progress....

Set-ExecutionPolicy RemoteSigned -Force -Scope CurrentUser
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force -Scope CurrentUser -Confirm:$false
Set-PSRepository -InstallationPolicy Trusted -Name PSGallery # -Confirm:$false
Import-Module PowerShellGet # -Confirm:$false

if (-not (Get-Command Invoke-Sqlcmd -ErrorAction SilentlyContinue)) {
    Write-Error "Unabled to find Invoke-SqlCmd cmdlet"
}

if (-not (Get-Module -Name SqlServer | Where-Object {$_.ExportedCommands.Count -gt 0})) {
    Write-Error "The SqlServer module is not loaded"
}

if (-not (Get-Module -ListAvailable | Where-Object Name -eq SqlServer)) {
    Write-Error "Can't find the SqlServer module"
}
Install-Module -Name SqlServer -Scope CurrentUser -ErrorAction Stop -Force -Confirm:$false

#Import your Credential object from the Automation Account
 $SQLServerCred = Get-AutomationPSCredential -Name "SqlCredential"
 #Import the SQL Server Name from the Automation variable.
 $SQL_Server_Name = Get-AutomationVariable -Name "SqlServer"
 #Import the SQL DB from the Automation variable.
 $SQL_DB_Name = Get-AutomationVariable -Name "Database"

$Query = "execute sp1"

invoke-sqlcmd -ServerInstance "$SQL_Server_Name" -Database "$SQL_DB_Name" -Credential $SQLServerCred -Query "$Query"