We needed to transfer some data from Microsoft Dynamics AX to a old system running on DB2, to keep the data updated.
This script runs daily and checks for changes made yesterday and creates a .csv file with the result, which can be imported/converted to most system. See comments for explanation:
This script needs to be run on a server with SQL installed, as it uses the SQL server snapin.
set-executionpolicy unrestricted
Add-PSSnapin SqlServerCmdletSnapin100
Add-PSSnapin SqlServerProviderSnapin100
#Your SQL query
$SQLquery =@"
SELECT CUSTACCOUNT, FIRSTNAME + ' ' + MIDDLENAME as FIRSTMIDDLE,LASTNAME,STREET,ZIPCODE,CITY,PHONE,CELLULARPHONE,EMAIL,BIRTHDATE,CPR FROM [Your server].[dbo].[table]
where DATAAREAID = 'ID' AND
convert(varchar(10), CREATEDDATETIME, 102) = dateadd(day,datediff(day,0,getdate()),-1)
"@
#Run the query and export the result
$result = invoke-sqlcmd -query $SQLquery -serverinstance Server -database database
$result |export-csv c:\file.csv -notypeinformation -Encoding UTF8 -Delimiter ";"
#Give the file 5 sec to be created
Start-Sleep -s 5
#Remove all " from file - PowerShell adds them when using export-csv
(Get-Content c:\file.csv) | % {$_ -replace '"', ""} | out-file -FilePath c:\file.csv -Force
#Give the file 2 sec to be created
Start-Sleep -s 2
#Load the file and remove the headers - first line in csv
$csv = Get-Content c:\file.csv
$csv = $csv[1..($csv.count - 1)]
$csv > c:\file.csv
Ingen kommentarer:
Send en kommentar