I use SQL Server and I have a SQL table called [dbo].[TemplatePackageContent] which has only two fields:
[TemplatePackageContentId] [uniqueidentifier] NOT NULL,
[Content] [varbinary](max) NULL
I'd like to create PowerShell script which reads whole content from this table and for each row, it will generate a file, in a given directory with format {TemplatePackageContentId}.pkg based on the Content field.
So far I've managed how to read the whole content of the table:
param(
[string] $dataSource = "(localdb)\mssqlLocalDb",
[string] $database = "Hda_tenancy1",
[string] $sqlCommand = $("SELECT * FROM TemplatePackageContent")
)
$connectionString = "Data Source=$dataSource; " +
"Integrated Security=SSPI; " +
"Initial Catalog=$database"
$connection = new-object system.data.SqlClient.SQLConnection($connectionString)
$command = new-object system.data.sqlclient.sqlcommand($sqlCommand,$connection)
$connection.Open()
$adapter = New-Object System.Data.sqlclient.sqlDataAdapter $command
$dataset = New-Object System.Data.DataSet
$adapter.Fill($dataSet) | Out-Null
$connection.Close()
$dataSet.Tables
Now I would like to make a mentioned conversion of received result, presumably looping thru each row.
I found this article which solves a similar problem in c# and I was thinking about using some of the logic from there and try to convert it into a PowerShell script.
What is the most optimal way to convert all the "Content" fields into files with .pkg format and what library should I use or what approach?
Any ideas?
Cheers
I ended up with this solution:
param(
[string] $dataSource = "(localdb)\mssqlLocalDb",
[string] $database = "Hda_tenancy1",
[string] $templatePath = "C:\dev\hubadvance\Seeding\Templates\"
)
$sqlCommand = $("SELECT * FROM TemplatePackageContent");
$connection = new-object System.Data.SqlClient.SQLConnection("Data Source=$dataSource;Integrated Security=SSPI;Initial Catalog=$database");
$cmd = new-object System.Data.SqlClient.SqlCommand($sqlCommand, $connection);
$connection.Open();
$reader = $cmd.ExecuteReader();
$results = #();
while ($reader.Read())
{
$row = #{}
for ($i = 0; $i -lt $reader.FieldCount; $i++)
{
$row[$reader.GetName($i)] = $reader.GetValue($i);
}
$results += new-object psobject -property $row;
}
$connection.Close();
foreach ($row in $results)
{
Write-Host $row.TemplatePackageContentId;
$path = $templatePath + $row.TemplatePackageContentId + ".pkg";
[System.IO.File]::AppendAllText($path, $row.Content);
}
Related
We take a lot of photograph's for product imagery . I have found a piece of software that will read a Barcode embeded in the image and rename the Image to the Barcode within the image , what i now want to do is rename the File again with the Productid using powershell with an SQL query within ,
declare #$CliRef nvarchar(max)
set #$CliRef = '5015807416980'
Select convert(nvarchar(max), productid) as ID FROM products WHERE products.Barcode = #$CliRef
I Found a Powershell script online but I must be doing something wrong as its renaming the file blank rather than a productid , I'm really new to Powershell so a little stuck so any help appreciated
$Database = "training"
$Server = "ART-WM01"
##Not sure i need this bit
function Select-Info($CliRef)
{
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Data Source=$Server;Initial Catalog=$Database;Integrated Security = True"
$SqlConnection.Open()
$conn.open()
$query = “Select convert(varchar,producdid )as ID FROM products WHERE barcode = '$CliRef'”
$cmd = New-Object System.Data.SqlClient.SqlCommand($sql,$conn)
$cmd.connection = $SqlConnection
$cmd.commandtext = $query
$result = $cmd.executenonquery()
$SqlConnection.Close()
return $query
}
## Return ID from Database
function Return-Info($CliRef)
{
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Data Source=$Server;Initial Catalog=$Database;Integrated Security = True"
$SqlConnection.Open()
$cmd = New-Object System.Data.SqlClient.SqlCommand
$cmd.commandtext = “Select convert(varchar, productid) as ID FROM products WHERE barcode = '$CliRef'”
$cmd.connection = $SqlConnection
$result = $cmd.ExecuteScalar()
$SqlConnection.Close()
return $result
}
## Collect the image names
$FiNms = Get-ChildItem C:\JJC\Test_Dump -Name
## Loop through each Image name
foreach ($FiNm in $FiNms)
{
## Variable for current File path
$file = “C:\JJC\Test_Dump\” + $FiNm
## Variable for new File path
$newFile = “C:\JJC\Test_Dump\renamed” + $FiNm
$ID = Return-Info $FiNm
$ID = $ID + “.tiff”
##Copy-Item $file -Destination $newFile
##Copy-Item $file -Destination = “C:\JJC\\Test_Dump\renamed\”
Rename-Item $file $ID -force
}
I have an excel file that contain a list of staff information,
now I want to use the column Staff_ID in xlsx to select those Staff_ID from sql table and get the result by export_CSV, so I try to write a powershell that could be run in schedule task.
How should I get the information from excel and select those data from sql table?
$SQLServer = "ServerName"
$SQLDBName = "DBName"
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server = $SQLServer; Database =
$SQLDBName; User ID= YourUserID; Password= YourPassword"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = 'StoredProcName'
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$SqlConnection.Close()
$gSqlAuthMode = True
SELECT * FROM [DBName].[dbo].[DETAILS] WHERE [STATUS] not like 'Inactive-%' ORDER BY NUMBER ASC
$List = Import-XLSX -Path "D:\Stafflist.xlsx"
foreach ($item in $List){
If ((?? -eq $item.StaffCuid.toUpper().trim())) {
$Report = [PSCustomObject] #{
}
}
}
$Report | Export-Csv D:\report.csv -Delimiter "," -NoTypeInformation -append
}
You could use Import-Excel cmdlet from the famous ImportExcel module and Invoke-SQLCmd (part of SQL management tools)
$Excel = Import-Excel -Path D:\Stafflist.xlsx
Foreach($Row in $Excel) {
$StaffID = $Row.StaffCuid.toUpper().trim()
$Query = "create your query here with $StaffId"
Invoke-SQLCmd -Query $Query -ServerInstance "$ServerName\$DBName"
}
I have a stored procedure which takes a date as a paramter and returns rows, in the return from the function in powershell an object array is returned containing the parameters and the rows.
I know I can get around this by referencing the rows and working with them but I want to know why it returns the parameter. Can anyone shed some light please?
function Invoke-SQL($dataSource, $database, $sqlCommand, $tradeDate) {
$result = New-Object System.Data.DataTable
$connectionString = "Data Source=$dataSource; Integrated Security=SSPI; Initial Catalog=$database"
$connection = new-object system.data.SqlClient.SQLConnection($connectionString)
$command = new-object system.data.sqlclient.sqlcommand($sqlCommand,$connection)
$command.CommandType = [System.Data.CommandType]::StoredProcedure
$parameter = New-Object System.Data.SqlClient.SqlParameter ("#TradeDateParam", $tradeDate)
$command.Parameters.Add($parameter);
$adapter = New-Object System.Data.sqlclient.sqlDataAdapter $command
$connection.Open()
$adapter.Fill($result) | Out-Null
$connection.Close()
return $result
}
$resultsDataTable = New-Object system.Data.DataTable
$resultsDataTable = Invoke-SQL "server" "db" "sproc" "dateparam"
ALTER PROCEDURE [dbo].[spCheckTradeActivityBetweenTradeDates]
#TradeDateParam date = null
AS
DECLARE #TradeDate date = #TradeDateParam;
SELECT 'HEllO' AS hello
return 0
Image of returned object array
So after some reading on Michael Sorens answer it appears functions in Powershell can sometimes return more output than the value you are returning. I used an Out-Null pipe to suppress the output of the paramter as you can see below, it is worth noting that the Out-Null pipe is not the most efficient way to handle this.
Whilst the pipe does work I still do not know why it added a parameter to a SQL command generates output.
function Invoke-SQL($dataSource, $database, $sqlCommand, $tradeDate) {
$result = New-Object System.Data.DataSet
$connectionString = "Data Source=$dataSource; Integrated Security=SSPI; Initial Catalog=$database"
$connection = new-object system.data.SqlClient.SQLConnection($connectionString)
$command = new-object system.data.sqlclient.sqlcommand($sqlCommand, $connection)
$command.CommandType = [System.Data.CommandType]::StoredProcedure
# $parameter = New-Object System.Data.SqlClient.SqlParameter ("#TradeDateParam", $tradeDate)
$command.Parameters.AddWithValue("#TradeDateParam", $tradeDate) | Out-Null # <--Added this pipe to suppress parameter output
$adapter = New-Object System.Data.sqlclient.sqlDataAdapter $command
$connection.Open()
$adapter.Fill($result) | Out-Null
$connection.Close()
return $result.Tables
}
I'm struggling to get my head around this one, I've only just begun looking at scripting in SQL, and my powershell is very limited. The requirments are basically this:
Utilisng Powershell, import a csv file which contains one column that needs to feed into multiple SQL queries via a loop, exporting a seperate csv file for each different query.
example import of csv:
Project (heading)
1000
1001
1002
Powershell:
$importProjectsCSV = e:\Projects.csv
$server = servername
$database = database
import-csv $importProjectsCSV | ForEach-Object {
$query = "
Select ProjectLeader, ProjectTitle
FROM dbo.PROJECTS
Where Project = $_.Project;
Select ProjectClient, Name
FROM dbo.CLIENTS
Where Project = $_.Project;
$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 = $query
$command.Connection = $connection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $command
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$connection.Close()
$dataset.Table[0] | Export-csv "E:\" + $_.ProjectName + ".csv"
$dataset.Table[1] | Export-csv "E:\" + $_.ProjectName + ".csv"
The problem is that the variable isn't coming into the SQL query.
Is there a better way to handle this type of example?
Appreciate any pointers
Paul.
I would do something like this:
I must admit i havent been able to test it, and personally i usually use c# to query sql servers. So i might have gone a bit wrong somewhere.
$importProjectsCSV = e:\Projects.csv
$server = servername
$database = database
$Projects = Import-Csv -Path $importProjectsCSV | % {$_.Project}
$DS_Projects = New-Object System.Data.DataSet
$DS_Clients = New-Object System.Data.DataSet
$query_pro = "Select ProjectLeader, ProjectTitle, Project FROM dbo.PROJECTS";
$query_Clients = "Select ProjectClient, Name, Project FROM dbo.CLIENTS";
$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
$connection.Open()
$command = $connection.CreateCommand();
$command.CommandText = $query_pro;
$sqlAdap = New-Object System.Data.SqlClient.SqlDataAdapter($command)
$sqlAdap.Fill($DS_Projects)
$command2 = $connection.CreateCommand();
$command2.CommandText = $query_Clients;
$sqlAdap2 = New-Object System.Data.SqlClient.SqlDataAdapter($command2)
$sqlAdap2.Fill($DS_Clients)
$connection.Close();
foreach($project in $Projects)
{
$DS_Projects.Tables[0].Select("Project=$project") | Export-Csv "E:\$project.csv"
$DS_Clients.Tables[0].Select("Project=$project") | Export-Csv "E:\$project.csv"
}
If I get every right you could do this with something like this:
$Projects = Import-Csv -Path 'C:\Temp\Projects.csv'
ForEach ($Project in $Projects.Projects) {
$Query= #"
Select ProjectLeader, ProjectTitle
FROM dbo.PROJECTS
Where Project = '%{0}';
"# -f $Project
# just dummy action
$results = Invoke-Sqlcmd -ServerInstance "foo" -Database "bar" -Query $Query
# In Case results is a dataset do something like
$results | Export-Csv -Path ("E:\{0}.csv" -f $Project )
}
I'm having problems getting PowerShell to run multiple SQL queries and export the results as CSV.
I'm trying to accomplish this using a Function but the problem occurs in the Process block when I expect two queries to run and output two CSV files.
I tried creating one function to run the query and a second function to create the CSV files but that didn't even run the SQL queries. I'm doing this without SQL being installed where this powershell script is executed from. -thanks!
Function Run-Query {
param([string[]]$queries,[string[]]$sheetnames)
Begin{
$SQLServer = 'ServerName'
$Database = 'DataBase'
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server = $SQLServer; Database = $Database; Integrated Security = True"
}#End Begin
Process{
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $queries
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$DataSet.Tables[0] | Export-Csv -NoTypeInformation -Path "C:\Scripts\$sheetnames.csv"
}#End Process
End{
$SqlConnection.Close()
}
}#End function run-query.
$queries = #()
$queries += #'
Select * from something
'#
$queries += #'
Select * from something2
'#
$sheetnames = #()
$sheetnames += 'Cert'
$sheetnames += 'Prod'
Run-Query -queries $queries
I'm not sure if SQL processes multiple queries separately so while you might be passing two different queries the SQL server might be interpreting them as one query (Not 100% sure this is happening, just a guess really)
You've put your queries in an array so we can easily loop through the array, run each query by itself and put the results into a CSV.
Here's how i'd modify your code to start with:
Function Run-Query
{
param([string[]]$queries,[string[]]$sheetnames)
Begin
{
$SQLServer = 'ServerName'
$Database = 'DataBase'
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server = $SQLServer; Database = $Database; Integrated Security = True"
}#End Begin
Process
{
# Loop through each query
For($i = 0; $i -lt $queries.count; $i++)
{
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
# Use the current index ($i) to get the query
$SqlCmd.CommandText = $queries[$i]
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
# Use the current index ($i) to get the sheetname for the CSV
$DataSet.Tables[0] | Export-Csv -NoTypeInformation -Path "C:\Scripts\$($sheetnames[$i]).csv"
}
}#End Process
End
{
$SqlConnection.Close()
}
}#End function run-query.
$queries = #()
$queries += #'
Select * from something
'#
$queries += #'
Select * from something2
'#
$sheetnames = #()
$sheetnames += 'Cert'
$sheetnames += 'Prod'
Run-Query -queries $queries -sheetnames $sheetnames