I have a stored procedure that ends with FOR XML EXPLICIT and I need to put the xml file in a folder.
I've set up the following PowerShell, and run the script providing the two parameters.
The name of the script is xml.ps1
./xml "xml" "c:\test.xml"
param
(
[string]$outputType,
[string]$filename
)
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server=SLISQL;Database=Development_App;Integrated Security=True"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = "dbo._REX_NewSP"
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$SqlConnection.Close()
$DataSet.Tables[0]
if ($outputType -eq "Text")
{
$DataSet.Tables[0] | format-table -auto > $filename
}
if ($outputType -eq "xml")
{
$DataSet.Tables[0] |Export-Clixml $filename
}
I see the xml output in the PowerShell screen, but no text.xml file is output.
Best Answer
I turned your PS into a function, just changing the calls, ran it through the PS ISE, and it worked just fine for me in testing, outputting the data to the location. I would ensure that you have the ability to write to the root of the C: drive. Typically this can require admin permissions. It's possible you are swallowing the error somewhere.
Created the file in the correct location with the contents:
Here's an updated version (a little hacky, I know) that writes the header to a file, and then just the XML data that is produced. See if this gets you a little closer.