我想在powerShell中编写一个查询,该查询检查名为(xml_multiple)的xml列中的属性值,如果存在则返回布尔值1(否则为0)并将其传递给变量并调用sendemail函数。 根据变量的值,将发送电子邮件。 1-成功 0-失败
我是PowerShell的新手并不是很擅长。只要它有效,我愿意接受建议。谢谢。检查下面的代码和xml
$dataSource = "DB.abc.com"
$connectionString = "Server=$dataSource;uid=$user; pwd=$pwd;Database=$database;Integrated Security=False;"
$connection = New-Object System.Data.SqlClient.SqlConnection
$connection.ConnectionString = $connectionString
$connection.Open()
$query = “ ” <#here i want to write my query#>
$command = $connection.CreateCommand()
$command.CommandText = $query
$result = $command.ExecuteReader()
$table = new-object “System.Data.DataTable”
$table.Load($result)
$connection.Close()
function sendemail()
{
$Outlook = New-Object -ComObject Outlook.Application
$Mail = $Outlook.CreateItem(0)
$Mail.To = "abc@xyz.com"
if ($send -eq 1) <#here i want to pass value from db#>
{
$Mail.Subject = "Process Successful"
$Mail.Body ="Success`n`nThank you"
}
else
{
$Mail.Subject = "Process Unsuccessful"
$Mail.Body ="Unsuccess`n`nPlease look into it"
}
$Mail.Send()
}
注意:这是xml,如果任何xml有一个属性start =“1”,则返回1,否则为0.对于特定的一天(一天中只有一条记录会有这个属性,所以我们可以使用查询中的过滤器)
<jobparameters start="1">
<work>1
</work>
</jobparameters>
P
租赁提出建议
答案 0 :(得分:1)
可以直接从SQL Server获取所需的结果:
DECLARE @x XML = '
<jobparameters start="1">
<work>1
</work>
</jobparameters>';
DECLARE @t TABLE (xml_multiple XML);
INSERT @t(xml_multiple) VALUES(@x);
SELECT c.value('@start','INT') send
FROM @t
OUTER APPLY xml_multiple.nodes('/jobparameters')x(c);