Powershell 从调用SqlCmd捕获错误

Powershell 从调用SqlCmd捕获错误,powershell,invoke-sqlcmd,Powershell,Invoke Sqlcmd,我正在编写一个Powershell脚本,该脚本以CSV文件作为输入,其中包含关于服务器和每个服务器上的数据库的信息,以便为文件中的每个服务器和数据库创建特定的登录名和用户。该脚本尝试使用Invoke SqlCmd创建带有SQL命令的登录名和用户,然后将尝试结果输出到新的CSV文件。如果成功添加了登录名和用户,则输出数据时会显示一条成功消息,如果其中一条命令失败,则应输出数据时会显示一条失败消息。现在,如果存在Invoke-SqlCmd错误,则脚本不会确认存在故障,并输出运行成功 我想了解如何编写

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

我想了解如何编写Try/Catch块,以便它们能够实际捕获SQL命令失败并输出正确的消息

我当前为这些部分设置的Try/Catch块:

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和错误处理(,)存在已知的问题,但是有什么方法可以让我的脚本在这种情况下输出正确的消息?现在,我无法手动进入输出文件并将消息从成功更改为失败。这是在一个工作环境中,所以我希望我的脚本能够被其他人执行,而不需要他们进行编辑。我只是犯了一些简单的错误吗


感谢您的帮助

当接收到错误时,您希望如何告诉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文件执行上述操作的示例:

然后,如果我从脚本中删除
-ErrorAction Stop