【问题标题】:Invoke-Sqlcmd error in Loop, No error once?循环中调用-Sqlcmd错误,一次没有错误?
【发布时间】:2020-07-21 11:27:04
【问题描述】:

如果我运行这个 .ps1 脚本-

$secID = Invoke-Sqlcmd -ServerInstance "MyDBServer" -Database "MyDataBase"-Query "SELECT SysID FROM dbo.SecurityLevels WHERE LEVELNAME LIKE '%User%';" 
Write-Host "MyDataBase"
Write-Host $secID.SysID

我在控制台上得到以下信息,没有错误 -

MyDataBase
18

但是,如果我在更大的脚本中的 for 循环中尝试相同的查询 -

$dbservers = @('DataBaseServer1', 'DataBaseServer2')

foreach ($dbserver in $dbservers)
{
    $databases = Get-SqlDatabase -ServerInstance $dbserver | Where-Object { $_.Name -Match '\d{3,4}' -and $_.Name -notlike '*test*'}
    foreach ($database in $databases)
    {
            $secID = Invoke-Sqlcmd -ServerInstance $dbserver.Name -Database $database.Name -Query "SELECT SysID FROM dbo.SecurityLevels WHERE LEVELNAME LIKE '%User%';" 
            Write-Host $database.Name
            Write-Host $secID.SysID
    }
}

我得到了正确的查询结果,但控制台上出现了错误 -

Invoke-Sqlcmd : A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that    
the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) 
At \SQL.ps1:28 char:13                                                                                                      
+ ...    $secID = Invoke-Sqlcmd -ConnectionString $ConnectionString -Query  ...                                                                                                     
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~                                                                                                         
    + CategoryInfo          : InvalidOperation: (:) [Invoke-Sqlcmd], SqlException                                                                                                   
    + FullyQualifiedErrorId : SqlExceptionError,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand                                                                          

Invoke-Sqlcmd :                                                                                                                                                                     
At \SQL.ps1:28 char:13                                                                                                      
+ ...    $secID = Invoke-Sqlcmd -ConnectionString $ConnectionString -Query  ...                                                                                                     
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~                                                                                                         
    + CategoryInfo          : ParserError: (:) [Invoke-Sqlcmd], ParserException                                                                                                     
    + FullyQualifiedErrorId : ExecutionFailureException,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand*                                                                  
MyDataBase                                                                                                                                                                                                                                                                                                                                                            
18                                                                                                                                                                                  

为什么在循环中运行它而不是运行一次时会出错?另外,即使我收到错误,为什么查询仍然有效?我想我错过了任何帮助将不胜感激!

【问题讨论】:

    标签: sql-server powershell invoke-sqlcmd


    【解决方案1】:

    我认为这是因为您在字符串上调用属性Name,实际上什么也没返回。试试这个(我刚刚删除了属性$dbserver.Name$dbserver的调用):

    $dbservers = @('DataBaseServer1', 'DataBaseServer2')
    
    foreach ($dbserver in $dbservers)
    {
        $databases = Get-SqlDatabase -ServerInstance $dbserver | Where-Object { $_.Name -Match '\d{3,4}' -and $_.Name -notlike '*test*'}
        foreach ($database in $databases)
        {
                $secID = Invoke-Sqlcmd -ServerInstance $dbserver -Database $database.Name -Query "SELECT SysID FROM dbo.SecurityLevels WHERE LEVELNAME LIKE '%User%';" 
                Write-Host $database.Name
                Write-Host $secID.SysID
        }
    }
    

    错误一定是因为它正在寻找一个数据库实例“”但它没有找到它,并且查询可能仍然进行,因为第一台服务器可能没有实例,或者因为这些值已经加载到之前的属性。但我只是推测,并没有真正使用过这个 cmdlet。

    PS:如果您想避免调用空属性,请将Set-StrictMode -Version 2 添加到您的脚本中,例如:

    PS > $text = "This is just a string"
    PS > $text.AnyProperty
    PS > Set-StrictMode -Version 2
    PS > $text.AnyProperty
    The property 'AnyProperty' cannot be found on this object. Verify that the property exists.
    At line:1 char:1
    + $text.AnyProperty
    + ~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], PropertyNotFoundException
        + FullyQualifiedErrorId : PropertyNotFoundStrict
    

    希望对你有帮助。

    【讨论】:

    • 很高兴它有帮助。
    猜你喜欢
    • 2018-10-22
    • 2014-09-12
    • 2015-07-28
    • 1970-01-01
    • 1970-01-01
    • 2018-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多