E. *_*lly 2 powershell 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 $_
}
Run Code Online (Sandbox Code Playgroud)
运行上述代码时(我希望能够发现运行 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
Run Code Online (Sandbox Code Playgroud)
我知道 Invoke-Sqlcmd 和错误处理( source、source )存在已知问题,但是有什么方法可以让我的脚本在这种情况下输出正确的消息吗?现在,我只能手动进入输出文件并将消息从成功更改为失败。这是在工作环境中,所以我希望其他人能够执行我的脚本,而无需他们也进行编辑。我只是犯了一些简单的错误吗?
任何帮助表示赞赏!
小智 6
您想要做的就是告诉 cmdletInvoke-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 $_
}
Run Code Online (Sandbox Code Playgroud)
-ErrorAction Stop然后,如果我从脚本中
删除:
| 归档时间: |
|
| 查看次数: |
5278 次 |
| 最近记录: |