从Invoke-SqlCmd

时间:2019-07-31 14:37:27

标签: powershell invoke-sqlcmd

我正在研究一个Powershell脚本,该脚本以CSV文件作为输入,其中包含有关每个服务器上服务器和数据库的信息,以便为文件中的每个服务器和数据库创建特定的登录名和用户。该脚本尝试使用Invoke-SqlCmd使用SQL命令创建登录名和用户,然后将尝试的结果输出到新的CSV文件。如果成功添加了登录名和用户,则输出带有成功消息的数据,并且如果两个命令中的任何一个失败,则应该输出带有失败消息的数据。现在,如果发生Invoke-SqlCmd错误,脚本将不确认存在故障,并输出运行成功。

我想要一些帮助弄清楚如何编写我的Try / Catch块,以便它们实际上捕获到SQL命令失败并输出正确的消息。

我当前对这些部分的“尝试/捕获”块:

Try # Create Login
{
    # Importing sqlserver module to run the commands
    if (-not (Get-Module -Name "sqlserver"))
    {
        Import-Module sqlserver
    }
    Write-Host "Checking server $fullServerName to see if login [Example_Login] exists. Will create login if it does not already exist."
    Invoke-Sqlcmd -ServerInstance $fullServerName -Query "$loginCommand"

    Write-Host "Login command successfully executed.`n"
}
Catch
{
    Write-Host "An error has occurred while attempting to add login [Example_Login] to server $fullServerName."
    Write-Host $_
}

Try # Create database user
{
    Write-Host "Checking server $fullServerName to see if user [Example_User] exists on database $currentDatabase. Will create user if it does not already exist."
    Invoke-Sqlcmd -ServerInstance $fullServerName -Query "$databaseUserCommand"
    Write-Host "Database User command successfully executed.`n"
}
Catch
{
    Write-Host "An error has occurred while attempting to add user [Example_User] to login [Example_Login] on $currentDatabase."    
    Write-Host $_
}

运行上面的代码时,希望运行Invoke-SqlCmd时会出现问题,但出现以下错误,但脚本输出运行成功。

Invoke-Sqlcmd : Database 'MyDatabase' does not exist. Make sure that the name is entered correctly. 
 Msg 911, Level 16, State 1, Procedure , Line 2.
At E:\Create_Login.ps1:175 char:6
+ ...             Invoke-Sqlcmd -ServerInstance $fullServerName -Query "$da ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Invoke-Sqlcmd], SqlPowerShellSqlExecutionException
    + FullyQualifiedErrorId : SqlError,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand

我知道Invoke-Sqlcmd和错误处理(sourcesource)存在已知问题,但是有什么方法可以使我的脚本在这种情况下输出正确的消息?现在,我被困在手动进入输出文件并将消息从成功更改为失败的过程中。这是在工作环境中,因此我希望我的脚本能够由其他人执行,而无需他们也进行编辑。我只是在犯一些简单的错误?

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

要执行的操作是告诉cmdlet Invoke-Sqlcmd在收到错误时停止,然后它将正确击中catch块。您执行-ErrorAction Stop。因此,以这种方式使用命令将输入​​catch块

$fullServerName = 'Doesnotexist'
$loginCommand = "SELECT @@SERVERNAME"
Try # Create Login
{
    # Importing sqlserver module to run the commands
    if (-not (Get-Module -Name "sqlserver"))
    {
        Import-Module sqlserver
    }
    Write-Host "Checking server $fullServerName to see if login [Example_Login] exists. Will create login if it does not already exist."
    Invoke-Sqlcmd -ServerInstance $fullServerName -Query "$loginCommand" -ErrorAction Stop

    Write-Host "Login command successfully executed.`n"
}
Catch
{
    Write-Host "An error has occurred while attempting to add login [Example_Login] to server $fullServerName."
    Write-Host $_
}

通过ps1文件执行的上述示例: enter image description here

然后,如果我从脚本中删除了-ErrorAction Stopenter image description here