Powershell invoke-sqlcmd:值不能为空。参数名称:ServerInstance

Powershell invoke-sqlcmd : Value cannot be null. Parameter name: ServerInstance

我在使用下面概述的方法调用本地数据库时遇到问题。

错误信息

invoke-sqlcmd : Value cannot be null. Parameter name: ServerInstance At C:\filelocation\HealthCheckCombined.ps1:86 char:3 1. invoke-sqlcmd -query $agentquery -serverinstance $servername ... 2. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Invoke-Sqlcmd], ArgumentNullException + FullyQualifiedErrorId : CannotGetServerInstance,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand


环境


目标

我正在尝试通过 PowerShell 运行 查询 servers.txt(配置文件)中列出的任何 SQL 实例。


两个分量-

  1. 外部配置文件(servers.txt)
  2. 包含函数的 PowerShell 脚本,循环从 servers.txt 创建数组并执行函数。

所以servers.txt的内容看起来像=

server=test2k16\powershell
server=test2k16\healthcheck

这是我导入文本文件并创建函数的部分=

#===============================================================================
#Configurable variables
#===============================================================================
$configfile = 'C:\filelocation\servers.txt'
Import-Module "sqlps"
#===============================================================================

#===============================================================================
#SQL Agent Jobs
#===============================================================================
function SQLAgent{
$agentquery= @"
    declare @count int
    select @count = count(1) from msdb.dbo.sysjobs as sj
    join msdb.dbo.sysjobhistory as sjh on sj.job_id = sjh.job_id
    where sj.enabled != 0
    and sjh.sql_message_id > 0
    and sjh.run_date > CONVERT(char(8), (select dateadd (day,(-30), getdate())), 112)
    and sjh.Step_id <= 1

    if (@count >= 1)
        begin
            select distinct sj.name as SQLJobName
            from msdb.dbo.sysjobs as sj
            join msdb.dbo.sysjobhistory as sjh on sj.job_id = sjh.job_id
            where sj.enabled != 0
            and sjh.sql_message_id > 0
            and sjh.run_date > CONVERT(char(8), (select dateadd (day,(-30), getdate())), 112)
            and sjh.Step_id <= 1
        order by name
    end

        else
        begin
            Select 'No Job Failed in Last Month' as SQLJobName
        end
"@

        invoke-sqlcmd -query $agentquery -serverinstance $servername -username "user" -password "password" | Format-Table -AutoSize -Wrap
}
#===============================================================================

现在我通过格式化导入的变量并在 运行 调用函数时循环遍历它们来实现奇迹=

#===============================================================================
#Run Health Check for each server
#===============================================================================
$import = $(foreach ($line in get-content $configfile) {$line.tolower().split(" ")}) | sort | get-unique
ForEach ($_ in $import){
    $servername = $import.trimstart("server=")
}
ForEach ($_ in $servername){
    SQLAgent
}
#===============================================================================

到目前为止的调查结果

我显然遗漏了一些东西...我已经在堆栈和 Google 中搜索了一天,但一无所获。希望这是我忽略或不了解 PowerShell 的小事。

在此先感谢您的帮助!

您正在为 -ServerInstance 参数引用 $ServerName。它需要一个字符串,而您向它显示一个字符串数组。此外,您最近两次错误地使用了 ForEach 循环。应该是变量名,而不是$_的自动变量。示例:

ForEach($Server in $ServerName){
    SQLAgent
}

然后将函数中的 -ServerInstance 更改为引用 $Server。更好的是,为您的函数设置参数并在循环中为其提供所需的信息。

Function SQLAgent($Server){
    <Code goes here!>
    invoke-sqlcmd -query $agentquery -serverinstance $server -username "user" -password "password" | Format-Table -AutoSize -Wrap
}
$ServerList = Get-Content $configfile | ForEach{$_.Split('=')[1]}
ForEach($Item in $ServerList){
    SQLAgent -Server $Item
}