从SQL脚本将变量返回到PowerShell

时间:2018-12-24 13:29:56

标签: sql powershell return

我有一个问题,我找不到...

我希望将SQL脚本的返回值返回到Powershell。

-MyScript.sql

    DECLARE @var VARCHAR(20);
    SET @var = 'databaseName';

-我的PowerShell

    $serverInstance='Server'
    $inputFile='C:\Users\xxx\Desktop\xxx\MyScript.sql'
    $user='user'
    $pass='pass'

    $splat=@{
        ServerInstance=$serverInstance
        InputFile=$InputFile
        U=$user
        P=$pass
    }

    $result= @(Invoke-Sqlcmd @splat -ErrorAction Stop)

    #From here it's an example because don't know how
    $result= @var  #The return from .SQL Script @var = 'databaseName'
    If $result = "databaseName"
       #Invoke-Sqlcmd with other actions

帮助!

0 个答案:

没有答案