Powershell SQL查询结果convertto-XML

时间:2013-04-03 23:36:02

标签: sql sql-server xml powershell

我一直在努力解决这个问题。让我试着解释一下我想要完成的事情,我希望我能够足够清楚。

我向MSSQL数据库发送两个查询并将其接收回来。下面的代码很完美,但我想在写入XML文件之前稍微操作XML的格式。我目前得到3列(serviceGroupName,numAccounts,numDevices)我想完成2件事之一:

1)添加一个名为“ReportType”的新列,并将其填入“Monthly”或“Total”,具体取决于它是否为foreach循环的第1或第2个传递(SQLQuery1是每月报告,SQLQuery2是自以后的总数)以来)

2)创建一个新的PSObject并让它填写相应的信息,例如它收到的数据(serviceGroupName,numAccounts,numDevices)

以下是我目前的代码。正如我所提到的,它确实有效,并且它生成了一个XML,但如果可能的话,我想在管道之前添加更多信息到ConvertTo-XML。

### Dates to use
$Date = (Get-Date -f MM-dd-yyyy)
$FDoTM = ((Get-Date -Day 01).AddMonths(0)).AddDays(0)
$LDo2PM = ((Get-Date -Day 01).AddMonths(-1)).AddDays(-1)
$TempDir = "C:\Temp"
$WebDir =     @("\\x.x.x.x\c$\inetpub\wwwroot\Reports\Accounts","\\x.x.x.x\c$\inetpub\wwwroot\Reports\Accounts")

### Something

$OutputXML = "$Date-Monthly-AccountReport.xml"

### Connection settings, uses windows authentication

$DBServer = "OMMITED"
$databasename = "OMMITED"
$Connection = new-object system.data.sqlclient.sqlconnection #Set new object to connect to sql database
$Connection.ConnectionString ="server=$DBServer;database=$databasename;trusted_connection=True" # Connectiongstring setting for local machine database with window authentication
Write-host "Connection Information:"  -foregroundcolor yellow -backgroundcolor black
$Connection #List connection information


### Connect to Database and Run Query

$SqlCmd = New-Object System.Data.SqlClient.SqlCommand #setting object to use sql commands

$OutputHeader1 = "This Month's counts"
$SqlQuery1 = @"

SET NOCOUNT ON;

WITH AccountDeviceStats(serviceGroupName,numAccounts,numDevices)
AS
(
    SELECT svg.name,COUNT(acct.serviceGroupId) as Accounts, NULL FROM bm_account acct WITH     (NOLOCK)
    INNER JOIN bm_servicegroup svg WITH (NOLOCK) ON svg.servicegroupId = acct.serviceGroupId

    where acct.CreateStamp between '$($LDo2PM)' and '$($FDoTM)'
GROUP BY acct.serviceGroupId,svg.name
UNION ALL
SELECT svg.name, NULL, COUNT(device.serviceGroupId) as Devices FROM bm_device device WITH (NOLOCK)
INNER JOIN bm_servicegroup svg WITH (NOLOCK) ON svg.servicegroupId = device.serviceGroupId, bm_account acct

where device.accountID=acct.accountId and acct.CreateStamp between '$($LDo2PM)' and '$($FDoTM)'
GROUP BY device.serviceGroupId,svg.name
)
SELECT ad1.serviceGroupName,ad1.numAccounts,ad2.numDevices FROM AccountDeviceStats ad1
INNER JOIN AccountDeviceStats ad2 ON ad1.serviceGroupName = ad2.serviceGroupName
WHERE ad1.numAccounts IS NOT NULL AND ad2.numDevices IS NOT NULL
ORDER BY numAccounts DESC,numDevices DESC
"@

$OutputHeader2 = "Total Counts"
$SqlQuery2 = @"

SET NOCOUNT ON;

WITH AccountDeviceStats(serviceGroupName,numAccounts,numDevices)
AS
(
SELECT svg.name,COUNT(acct.serviceGroupId) as Accounts, NULL FROM bm_account acct WITH     (NOLOCK)
INNER JOIN bm_servicegroup svg WITH (NOLOCK) ON svg.servicegroupId = acct.serviceGroupId

where acct.CreateStamp < '12-31-2099'
GROUP BY acct.serviceGroupId,svg.name
UNION ALL
SELECT svg.name, NULL, COUNT(device.serviceGroupId) as Devices FROM bm_device device WITH (NOLOCK)
INNER JOIN bm_servicegroup svg WITH (NOLOCK) ON svg.servicegroupId = device.serviceGroupId, bm_account acct

where device.accountID=acct.accountId and acct.CreateStamp < '12-31-2099'
GROUP BY device.serviceGroupId,svg.name
)
SELECT ad1.serviceGroupName,ad1.numAccounts,ad2.numDevices FROM AccountDeviceStats ad1
INNER JOIN AccountDeviceStats ad2 ON ad1.serviceGroupName = ad2.serviceGroupName
WHERE ad1.numAccounts IS NOT NULL AND ad2.numDevices IS NOT NULL
ORDER BY numAccounts DESC,numDevices DESC
"@

$sqlQueries = @($SqlQuery1, $SqlQuery2)

$Results = @()

Foreach ($Query in $sqlQueries){
    $Connection.open()
    Write-host "Connection to database successful." -foregroundcolor green -backgroundcolor black
    $SqlCmd.CommandText = $Query
    $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
    $SqlAdapter.SelectCommand = $SqlCmd
    $SqlCmd.Connection = $Connection
    $DataSet = New-Object System.Data.DataSet
    $SqlAdapter.Fill($DataSet)
    $Connection.Close()

$Results += $DataSet.Tables[0]

($Results | ConvertTo-XML -NoTypeInformation).Save("$TempDir\$OutputXML")
}

if ((Get-ChildItem $TempDir -filter "$Date-*.xml").count -gt 0){    
Foreach ($file in (Get-ChildItem $TempDir -filter "$Date-*.xml" -recurse)){
    Foreach ($webserver in $WebDir){
        Copy-Item $file.fullname "$webserver\$file" -force
        }
    Remove-Item $file.fullname -force
    }
}

这是XML的输出格式

<?xml version="1.0"?>
<Objects>
  <Object>
    <Property Name="serviceGroupName">ServiceGroup1</Property>
    <Property Name="numAccounts">15</Property>
    <Property Name="numDevices">28</Property>
    <Property Name="RowError" />
    <Property Name="RowState">Unchanged</Property>
    <Property Name="Table">
      <Property>System.Data.DataRow</Property>
    </Property>
    <Property Name="ItemArray">
      <Property>ServiceGroup1</Property>
      <Property>15</Property>
      <Property>28</Property>
    </Property>
    <Property Name="HasErrors">False</Property>
  </Object>
  <Object>
    <Property Name="serviceGroupName">ServiceGroup1</Property>
    <Property Name="numAccounts">45</Property>
    <Property Name="numDevices">69</Property>
    <Property Name="RowError" />
    <Property Name="RowState">Unchanged</Property>
    <Property Name="Table">
      <Property>System.Data.DataRow</Property>
    </Property>
    <Property Name="ItemArray">
  <Property>ServiceGroup1</Property>
  <Property>45</Property>
  <Property>69</Property>
</Property>
<Property Name="HasErrors">False</Property>

最后一件事。如果可以从XML中删除多余的膨胀,正如您所看到的那样,它会使数据输出加倍,因为它会创建一个名为ItemArray的节点,其中包含所有相同的信息。

我希望这很容易理解。如果您需要更多信息,请告诉我。并提前感谢您提供的任何帮助。

2 个答案:

答案 0 :(得分:1)

我认为你需要做的就是在powershell脚本中更新你的两个T-sql查询。首先,添加如下代码:


...., "Monthly" as ReportType FROM AccountDeviceStats ad1...
第二个,添加如下代码:


...., "Total" as ReportType FROM AccountDeviceStats ad1...

### Dates to use
$Date = (Get-Date -f MM-dd-yyyy)
$FDoTM = ((Get-Date -Day 01).AddMonths(0)).AddDays(0)
$LDo2PM = ((Get-Date -Day 01).AddMonths(-1)).AddDays(-1)
$TempDir = "C:\Temp"
$WebDir =     @("\\x.x.x.x\c$\inetpub\wwwroot\Reports\Accounts","\\x.x.x.x\c$\inetpub\wwwroot\Reports\Accounts")

### Something

$OutputXML = "$Date-Monthly-AccountReport.xml"

### Connection settings, uses windows authentication

$DBServer = "OMMITED"
$databasename = "OMMITED"
$Connection = new-object system.data.sqlclient.sqlconnection #Set new object to connect to sql database
$Connection.ConnectionString ="server=$DBServer;database=$databasename;trusted_connection=True" # Connectiongstring setting for local machine database with window authentication
Write-host "Connection Information:"  -foregroundcolor yellow -backgroundcolor black
$Connection #List connection information


### Connect to Database and Run Query

$SqlCmd = New-Object System.Data.SqlClient.SqlCommand #setting object to use sql commands

$OutputHeader1 = "This Month's counts"
$SqlQuery1 = @"

SET NOCOUNT ON;

WITH AccountDeviceStats(serviceGroupName,numAccounts,numDevices)
AS
(
    SELECT svg.name,COUNT(acct.serviceGroupId) as Accounts, NULL FROM bm_account acct WITH     (NOLOCK)
    INNER JOIN bm_servicegroup svg WITH (NOLOCK) ON svg.servicegroupId = acct.serviceGroupId

    where acct.CreateStamp between '$($LDo2PM)' and '$($FDoTM)'
GROUP BY acct.serviceGroupId,svg.name
UNION ALL
SELECT svg.name, NULL, COUNT(device.serviceGroupId) as Devices FROM bm_device device WITH (NOLOCK)
INNER JOIN bm_servicegroup svg WITH (NOLOCK) ON svg.servicegroupId = device.serviceGroupId, bm_account acct

where device.accountID=acct.accountId and acct.CreateStamp between '$($LDo2PM)' and '$($FDoTM)'
GROUP BY device.serviceGroupId,svg.name
)
SELECT ad1.serviceGroupName,ad1.numAccounts,ad2.numDevices, ""Monthly"" as ReportType  FROM AccountDeviceStats ad1
INNER JOIN AccountDeviceStats ad2 ON ad1.serviceGroupName = ad2.serviceGroupName
WHERE ad1.numAccounts IS NOT NULL AND ad2.numDevices IS NOT NULL
ORDER BY numAccounts DESC,numDevices DESC
"@

$OutputHeader2 = "Total Counts"
$SqlQuery2 = @"

SET NOCOUNT ON;

WITH AccountDeviceStats(serviceGroupName,numAccounts,numDevices)
AS
(
SELECT svg.name,COUNT(acct.serviceGroupId) as Accounts, NULL FROM bm_account acct WITH     (NOLOCK)
INNER JOIN bm_servicegroup svg WITH (NOLOCK) ON svg.servicegroupId = acct.serviceGroupId

where acct.CreateStamp < '12-31-2099'
GROUP BY acct.serviceGroupId,svg.name
UNION ALL
SELECT svg.name, NULL, COUNT(device.serviceGroupId) as Devices FROM bm_device device WITH (NOLOCK)
INNER JOIN bm_servicegroup svg WITH (NOLOCK) ON svg.servicegroupId = device.serviceGroupId, bm_account acct

where device.accountID=acct.accountId and acct.CreateStamp < '12-31-2099'
GROUP BY device.serviceGroupId,svg.name
)
SELECT ad1.serviceGroupName,ad1.numAccounts,ad2.numDevices, ""Total"" as ReportType FROM AccountDeviceStats ad1
INNER JOIN AccountDeviceStats ad2 ON ad1.serviceGroupName = ad2.serviceGroupName
WHERE ad1.numAccounts IS NOT NULL AND ad2.numDevices IS NOT NULL
ORDER BY numAccounts DESC,numDevices DESC
"@

$sqlQueries = @($SqlQuery1, $SqlQuery2)

$Results = @()

Foreach ($Query in $sqlQueries){
    $Connection.open()
    Write-host "Connection to database successful." -foregroundcolor green -backgroundcolor black
    $SqlCmd.CommandText = $Query
    $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
    $SqlAdapter.SelectCommand = $SqlCmd
    $SqlCmd.Connection = $Connection
    $DataSet = New-Object System.Data.DataSet
    $SqlAdapter.Fill($DataSet)
    $Connection.Close()

$Results += $DataSet.Tables[0]

($Results | ConvertTo-XML -NoTypeInformation).Save("$TempDir\$OutputXML")
}

if ((Get-ChildItem $TempDir -filter "$Date-*.xml").count -gt 0){    
Foreach ($file in (Get-ChildItem $TempDir -filter "$Date-*.xml" -recurse)){
    Foreach ($webserver in $WebDir){
        Copy-Item $file.fullname "$webserver\$file" -force
        }
    Remove-Item $file.fullname -force
    }
}

答案 1 :(得分:0)

原始问题也询问了如何从XML中删除膨胀。我一直在寻找一种解决方案,我从SQL结果中生成的XML必须是一个绝对特定的格式,并带有正确的标签和所有内容。我发现,一旦你有了数据集对象($ DataSet),那么如果你想看看它有哪些方法和属性,($ DataSet | gm)那么其中一个是GetXML()。

这会自动格式化您的SQL输出,使得每个返回的列(或列别名)作为单独的标记返回(尽管注意,它不会为空值生成空标记)所以在这个实例中如果使用$ DataSet .GetXML()我原本希望看到输出符合

的内容
 <NewDataSet>
  <Table>
    <serviceGroupName>ServiceGroup1</serviceGroupName>
    <numAccounts>15</numAccounts>
    <numDevices>28</numDevices>
  </Table>
</NewDataSet>

所以没有膨胀!

由于这只是一系列字符串,你可以做一些事情,比如($ Dataset.GetXML())。替换('NewDataSet','OuterTag')。替换('Table','InnerTag')给出更好的XML标签。一旦你对此感到满意,你就可以输出

SET-CONTENT -PATH $xmlfilename -VALUE '<?xml version="1.0" ?>' 

或者某些文件,然后附加你的GetXML()方法的输出,这样你就有了更整洁的XML格式!

($DataSet.GetXML()).Replace('NewDataSet','OuterTagName').Replace('Table','InnerTagName') | ADD-CONTENT -PATH $xmlfilename