全局变量没有获得价值
Global Variable Not Getting Value
我想从我的函数的每次迭代中获取行数并将其分配给一个变量,这样我就可以在最后通过电子邮件发送结果。我的语法产生了一个错误,它对我说 $rowcount
实际上从未被分配给它?
Send-MailMessage: Cannot validate argument on parameter 'Body'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
#Declaring Global Variable
$myArray = $null
$GoodSyntax = "Select * From tableunknown"
$extractFile = "C:\Test.csv"
$dirName = "C:\Completed\"
$date = Get-Date -f 'MM.dd.yy'
#Call function
Execute-SQLquery
if (Execute-SQLquery $GoodSyntax)
$EmailBody = $myArray | Out-String
send-mailmessage -to "abc123@gmail.com" -from "barkbarksalon123@gmail.com" -Body $EmailBody -BodyAsHtml:$true -subject "Testing Through Powershell"
Function Execute-SQLquery {
param ($GoodSyntax)
$server = "Server01"
$database = "database01"
$connectionTemplate = "Data Source={0};Integrated Security=SSPI;Initial Catalog={1};"
$connectionString = [string]::Format($connectionTemplate, $server, $database)
$connection = New-Object System.Data.SqlClient.SqlConnection
$connection.ConnectionString = $connectionString
$command = New-Object System.Data.SqlClient.SqlCommand
$command.CommandText = $QueryString
$command.Connection = $connection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $command
$DataSet = New-Object System.Data.DataSet
$rowCount = $SqlAdapter.Fill($DataSet)
if ($rowCount -gt 0)
{
[System.IO.Directory]::CreateDirectory($dirName)
$filename = [IO.Path]::GetFileNameWithoutExtension($extractFile) + "_$date" + [IO.Path]::GetExtension($extractFile)
$extractFile = Join-Path $dirname $filename
$DataSet.Tables[0] | Export-Csv $extractFile -NoTypeInformation
$myArray += $rowCount
}
$connection.Close()
}
$rowCount
不是全局的……全局变量列在代码的顶部……应该是这样的……
#Declaring Global Variable
$myArray = $null
$rowCount = 0
$GoodSyntax = "Select * From tableunknown"
...
有道理吗?
编辑:
我的错!我看到您正在将 $rowCount
添加到 $myArray
...您的问题中并不清楚。
试试这个...
#Declaring Global Variable
$rowCountTotal = 0
$GoodSyntax = "Select * From tableunknown"
...
$EmailBody = $rowCountTotal | Out-String
...
$DataSet.Tables[0] | Export-Csv $extractFile -NoTypeInformation
$rowCountTotal += $rowCount
}
我想从我的函数的每次迭代中获取行数并将其分配给一个变量,这样我就可以在最后通过电子邮件发送结果。我的语法产生了一个错误,它对我说 $rowcount
实际上从未被分配给它?
Send-MailMessage: Cannot validate argument on parameter 'Body'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
#Declaring Global Variable
$myArray = $null
$GoodSyntax = "Select * From tableunknown"
$extractFile = "C:\Test.csv"
$dirName = "C:\Completed\"
$date = Get-Date -f 'MM.dd.yy'
#Call function
Execute-SQLquery
if (Execute-SQLquery $GoodSyntax)
$EmailBody = $myArray | Out-String
send-mailmessage -to "abc123@gmail.com" -from "barkbarksalon123@gmail.com" -Body $EmailBody -BodyAsHtml:$true -subject "Testing Through Powershell"
Function Execute-SQLquery {
param ($GoodSyntax)
$server = "Server01"
$database = "database01"
$connectionTemplate = "Data Source={0};Integrated Security=SSPI;Initial Catalog={1};"
$connectionString = [string]::Format($connectionTemplate, $server, $database)
$connection = New-Object System.Data.SqlClient.SqlConnection
$connection.ConnectionString = $connectionString
$command = New-Object System.Data.SqlClient.SqlCommand
$command.CommandText = $QueryString
$command.Connection = $connection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $command
$DataSet = New-Object System.Data.DataSet
$rowCount = $SqlAdapter.Fill($DataSet)
if ($rowCount -gt 0)
{
[System.IO.Directory]::CreateDirectory($dirName)
$filename = [IO.Path]::GetFileNameWithoutExtension($extractFile) + "_$date" + [IO.Path]::GetExtension($extractFile)
$extractFile = Join-Path $dirname $filename
$DataSet.Tables[0] | Export-Csv $extractFile -NoTypeInformation
$myArray += $rowCount
}
$connection.Close()
}
$rowCount
不是全局的……全局变量列在代码的顶部……应该是这样的……
#Declaring Global Variable
$myArray = $null
$rowCount = 0
$GoodSyntax = "Select * From tableunknown"
...
有道理吗?
编辑:
我的错!我看到您正在将 $rowCount
添加到 $myArray
...您的问题中并不清楚。
试试这个...
#Declaring Global Variable
$rowCountTotal = 0
$GoodSyntax = "Select * From tableunknown"
...
$EmailBody = $rowCountTotal | Out-String
...
$DataSet.Tables[0] | Export-Csv $extractFile -NoTypeInformation
$rowCountTotal += $rowCount
}