Quantcast
Channel: Windows PowerShell Forum
Viewing all articles
Browse latest Browse all 2562

Including XML data in Output File Name

$
0
0

I am using the following powershell script to extract data from a SQL database and create an XML file.  I want to use the data in the "Job" XML tag in the filename of each file being created from each row of data.  How can I accomplish this?

$DestinationDirectory = "\\fileserver\XMLfromPS\JobData"

$con = New-Object System.Data.SqlClient.SqlConnection
$con.ConnectionString = "Server=SQL4;Initial Catalog=InkJetDataDosRun;Integrated Security=True"

$cmd = New-Object System.Data.SqlClient.SqlCommand
$cmd.Connection = $con
$cmd.CommandText = "
SELECT     JobNumber as Job
   ,ActivityCode
   ,ActivityDescr
   ,ProcessOk
   , TrimSize
              
FROM         SQLTABLEX
 FOR XML
  PATH('Job')
  ,ROOT('Export')
"

$con.Open()

$Time = (get-date).ToString("yyyyMMddhhmm")
$Iterator = 0
$Job = ?????

$rdr = $cmd.ExecuteXmlReader()
While ($rdr.ReadToFollowing("Job"))
 {
  $Iterator += 1
  $FileName = $Job + '_' + $Time + '_' + ($Iterator).ToString("D3") + '.xml'
  $OutputFile = "$DestinationDirectory\$FileName"
  $JobReader = $rdr.ReadSubtree()
  $JobReader.Read() | Out-Null
  '<?xml version="1.0" encoding="utf-8" ?>' | Out-File $OutputFile -encoding 'UTF8'
  $JobReader.ReadOuterXml() | Out-File $OutputFile -append -encoding 'UTF8'
  $JobReader.Close()
 }


$rdr.Close()

$cmd.Dispose()
$con.Dispose()


Barb SS


Viewing all articles
Browse latest Browse all 2562

Trending Articles