我有一个powershell脚本,它接受我打算用来执行存储过程的用户输入。 sproc接受多个参数来自定义返回的数据。但是,当我运行此PowerShell代码时,我收到以下错误:
Cannot index into a null array.
At C:\Report.ps1:119 char:42
+ foreach ($Row in $global:DataSet.Tables[ <<<< 0].Rows)
+ CategoryInfo : InvalidOperation: (0:Int32) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
这是我用来执行对存储过程的调用的powershell代码。
# Create Shared Helper Objects
$nl = [Environment]::NewLine
function msg
{
param($message,$fgcolor="white",$bgcolor="black")
$output = Write-Host $message -ForegroundColor $fgcolor -BackgroundColor $bgcolor
write-output $output
}
Set-Location "C:\"
$month = Read-Host "Enter Report Month"
$year = Read-Host "Enter Report Year"
msg "$nl Choose a product" "White"
$prompt = ' [D] Desktop [M] Mobile'
switch (Read-Host $prompt) {
"D" {$product = "Desktop"; break}
"M" {$product = "Mobile"; break}
}
msg "$nl Select a Make" "White"
$prompt = ' [F] First [L] Last '
switch (Read-Host $prompt) {
"F" {$SiteMake = "First"; break}
"L" {$SiteMake = "Last"; break}
}
msg "$nl Select a Product" "White"
$prompt = ' [E] Extra [B] Basic'
switch (Read-Host $prompt) {
"E" {$SitePackage = "Extra"; break}
"B" {$SitePackage = "Basic"; break}
}
# Create Database Connection Function
function ConnectSproc
{
param ($SQLServer, $SQLDBName, $SqlQuery)
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server = $SQLServer; Database = $SQLDBName; Integrated Security = True"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $SqlQuery
$SqlCmd.CommandType = [System.Data.CommandType]::StoredProcedure
# Report Month
$SqlCmd.Parameters.Add("@ReportMonth",[system.data.SqlDbType]::VarChar) | out-Null
$SqlCmd.Parameters["@ReportMonth"].Direction = [system.data.ParameterDirection]::Input
$SqlCmd.Parameters["@ReportMonth"].value = $month
# Report Year
$SqlCmd.Parameters.Add("@ReportYear",[system.data.SqlDbType]::VarChar) | out-Null
$SqlCmd.Parameters["@ReportYear"].Direction = [system.data.ParameterDirection]::Input
$SqlCmd.Parameters["@ReportYear"].value = $year
# Product
$SqlCmd.Parameters.Add("@Product",[system.data.SqlDbType]::VarChar) | out-Null
$SqlCmd.Parameters["@Product"].Direction = [system.data.ParameterDirection]::Input
$SqlCmd.Parameters["@Product"].value = $product
# Site Make
$SqlCmd.Parameters.Add("@SiteMake",[system.data.SqlDbType]::VarChar) | out-Null
$SqlCmd.Parameters["@SiteMake"].Direction = [system.data.ParameterDirection]::Input
$SqlCmd.Parameters["@SiteMake"].value = $SiteMake
# Site Package
$SqlCmd.Parameters.Add("@SitePackage",[system.data.SqlDbType]::VarChar) | out-Null
$SqlCmd.Parameters["@SitePackage"].Direction = [system.data.ParameterDirection]::Input
$SqlCmd.Parameters["@SitePackage"].value = $SitePackage
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet) | Out-Null
$SqlConnection.Close()
$DataSet.Tables[0]
}
ConnectSproc "servername" "databasename" "mysproc"
我很茫然。任何帮助将不胜感激!
更新:它实际上看起来程序甚至没有正确执行。这可能解释了我得到的错误。现在我不确定为什么sproc没有执行。
答案 0 :(得分:1)
我明白了。它最终成为一个范围问题,在函数内部创建了$ DataSet变量,并且没有前缀“$ global:”
为了解决这个问题,我改变了
$DataSet = New-Object System.Data.DataSet
到$global:DataSet = New-Object System.Data.DataSet
并在函数外引用它为$global:DataSet.Tables[0]
。
答案 1 :(得分:0)
可能对Fill()
的调用失败了。暂时注释掉| Out_Null
的{{1}}部分,看看是否有任何消息。