Invoke-Sqlcmd -InputFile variable

Blair Mo 11 Reputation points
2022-03-15T19:26:41.487+00:00

How can I pass the input and out file variable in Invoke-Sqlcmd

$SQLServer = "MyServer"
$db = "MyDB"
$inputfile = "e:\mypath\myinputfile.sql"
$outputfile = "e:\mypath\myoutputfile.txt"

Invoke-Sqlcmd -ServerInstance $SQLServer -Database $db -InputFile $inputfile | Out-File -FilePath $outputfile

Error message:
Invoke-Sqlcmd : Cannot validate argument on parameter 'InputFile'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} vote

1 answer

Sort by: Most helpful
  1. Blair Mo 11 Reputation points
    2022-03-15T19:44:56.203+00:00

    It works when adding "Import-Module SQLPS"

    Import-Module SQLPS

    $SQLServer = "MyServer"
    $db = "MyDB"
    $inputfile = "e:\mypath\myinputfile.sql"
    $outputfile = "e:\mypath\myoutputfile.txt"

    Invoke-Sqlcmd -ServerInstance $SQLServer -Database $db -InputFile $inputfile | Out-File -FilePath $outputfile

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.