Cyclic csv-Import to SQL file by file - sql

I try to write a short script that cyclic imports csv-files to a SQL data table that are being dropped in an import folder ($sourceSQL). Each csv-file consists of one line of information (here three columns). To make sure that the file was successfully written to the data table, I check if the unique Id can be found in the table.
So far it works with the first file. However, the second file gets only moved to the destination folder without being written to the data table. I can't find the problem. Is it because of the $data variable?
$StartButton.Add_Click({
$script:ActiveLoop = $true
while ($script:ActiveLoop){
If (Test-Path $sourceSQL){
$data = $null
Do{
$data = import-csv $impcsv -Header A,B,C
foreach($i in $data)
{
$Id = $i.A
$State = $i.B
$Sequence = $i.C
$query = INSERT INTO $SQLTable (Id, State, Sequence)
VALUES ('$Id','$State','$Sequence')"
$impcsv = invoke-sqlcmd -Database $SQLDatabase -Query $query -serverinstance $SQLInstance -Username $SQLUsername -Password $SQLPassword}
$SqlQueryId = "SELECT TOP 1 Id from $SQLTable ORDER BY Id DESC"
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server = $SQLInstance; Database = $SQLDatabase; User ID = $SQLUsername; Password = $SQLPassword"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $SqlQueryId
$SqlCmd.Connection = $SqlConnection
$SqlConnection.Open()
$IdCheck= [string]$SqlCmd.ExecuteScalar()
$SqlConnection.Close()
} Until ($Id -eq $IdCheck)
Move-Item $sourceSQL -Destination $destinationSQL
}
[System.Windows.Forms.Application]::DoEvents()
}
})
$objForm.Controls.Add($StartButton)

Related

Powershell Rename file based on SQL query

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
}

How to use powershell to obtain the data from excel and select from SQL

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"
}

Powershell to save sql return dataset into csv file

I have the following script to run a script and to return the dataset when I would like the result saved into the folder under csv formet:
$connectionString = "Data Source=$sqlserver; User ID = $username; Password = $pws;Initial Catalog=$database;"
$connection = New-Object System.Data.SqlClient.SqlConnection
$connection.ConnectionString = $connectionString
$connection.Open()
$query = “SELECT TOP 5 * FROM Test”
$command = $connection.CreateCommand()
$command.CommandText = $query
$result = $command.ExecuteReader()
$result |export-csv c:\temp\Test.csv -notypeinformation
$connection.Close()
It's generate the csv file, however the contains inside the csv file is the fields count of each row of the dataset instead of the dataset itself. Does anyone know what is going wrong with my script?
Try to change your $result variable from $command.ExecuteReader() to an invoke-sqlcmd
$result = invoke-sqlcmd -query $query -serverinstance $sqlserver -database $database

Granting SQL Server role in a remote server

I am working on an activity where I have a table (t1) in server1 which stores servername, userid and role information.
I created a PowerShell script to run on server1, collect these information from the table, connect to the remote server(servername) and grant the requested role to the userid.
Please find the script below:
$SQLServer = "Server1"
$SQLDatabase = "Db1"
$user = "select userid from T1"
$role = "select access from T1"
$server1 = "Select servername from T1"
<#store the table information in a text file for auditing#>
$Sqlquery = "Select servername,userid,access from T1"
$Test = Invoke-SQLCMD -Query $SqlQuery -serverinstance $SQLServer -Database
$SQLDatabase
$Test | format-list servername,userid,access| out-file "G:\EP\test.txt"
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server = $Server1; Integrated Security =
True"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $SqlQuery
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$sqlSrv = New-Object 'Microsoft.SqlServer.Management.Smo.Server'
"SQLServer\Instance"
foreach ($login in $user)
{
$login.AddToRole($role)
}
$SqlConnection.Close()
While running the above code, I see the following error:
Method invocation failed because [System.String] does not contain a method named 'AddToRole'.
Where is the $User in foreach ($login in $user) defined ?
Its null and foreach iterates to null (expexted behaviour) and $login becomes nothing here.
You might have missed setting $user here.

Powershell import csv to SQL queries, export multiple csvs

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 )
}