Powershell:无法索引到空数组

时间:2014-02-13 18:09:58

标签: sql-server powershell switch-statement

所以,我有一个有趣的问题......我已经设置了两个Server 2012盒子,一个带有SQL Server 2012 Enterprise Edition的默认实例,另一个带有一个命名实例SQL Server 2012 Express Edition。我正在编写一个Powershell脚本,该脚本将针对SQL Server的CIS基准测试SQL Server 2012。

我有以下查询,其目的是检索登录模式:

$sapwam = Invoke-Sqlcmd -Query "exec xp_loginconfig 'login mode'" -ServerInstance $InstanceName

然后,我使用switch语句验证设置是否正确配置。它看起来像这样:

switch ($sapwam.config_value -eq "Windows NT Authentication") {
True {"4.1 Set The Server Authentication Property To Windows Authentication mode:                                    Pass"}
False {"4.1 Set The Server Authentication Property To Windows Authentication mode:                                    Deficient"}
}

让我难过的部分就是:当我针对Express Edition运行脚本时,一切都很好 - 没有错误。每当我针对Enterprise Edition实例运行脚本时,我都会得到“无法索引到空值数组的数据”。错误。因此,为了查看正在进行的操作,我添加了Write-Host $sapwam.config_value语句,以查看变量中包含哪些数据,并且我还添加了{{1}声明,只是为了证实我并不疯狂。 Write-Host语句将正确显示config_value属性中包含的值,布尔评估将正确计算(即False)。我也是

我有什么遗失的吗?

编辑:

在听取了KevinD的建议后,我将此行$sapwam.config_value -eq $null添加到我的脚本末尾...这会生成以下错误详情:

$Error.Exception | Where-Object -Property Message -Like "Cannot index into a null array." | Format-List -Force

0 个答案:

没有答案