0

Powershell を使用して、SQL Server Management Studio の [メッセージ] タブで、生成されたスクリプトを返す SQL スクリプトを実行します。しかし、PowerShell では取得できません。

私がこれを行うとき:

$ps = [PowerShell]::Create()
[ref]$e = New-Object System.Management.Automation.Runspaces.PSSnapInException
$ps.Runspace.RunspaceConfiguration.AddPSSnapIn( "SqlServerCmdletSnapin100", $e ) | Out-Null
$ps.AddCommand( "Invoke-Sqlcmd" ).AddParameter( "ServerInstance", "localhost").AddParameter( "Database", "MyDatabase").AddParameter("InputFile", "D:\MyScript.sql").AddParameter("Verbose")
$ps.Invoke()
$ps.Streams
$ps.Streams.Verbose | % { $_.Message | Out-File -Append D:\SqlLog.txt }

このエラーが発生します:

PS D:\NEMO\Scripts\Database> $ps.Invoke()
Exception calling "Invoke" with "0" argument(s): "Object reference not set to an instance of an object."
At line:1 char:11
+ $ps.Invoke <<<< ()
    + CategoryInfo          : NotSpecified: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : DotNetMethodException

ご協力ありがとうございました。

4

1 に答える 1

1

したがって、Powershell にメッセージを出力させるのはそれほど単純ではなく、コマンドラインでしか機能しないと思います。これを試して

write-host $line
    Start-Transcript $outputfilepath
    $ScriptParams = "DatabaseName=$line" "
    Invoke-Sqlcmd -ServerInstance $SQLInstance -Username $SQLAdminUser -Password $SQLAdminPwd -InputFile "D:\File.sql" -verbose -Variable $ScriptParams 
    Stop-Transcript
于 2013-04-09T15:28:04.960 に答える