troubleshooting Question

Want to add date to SQL results as static column, in Powershell

Avatar of csePixelated
csePixelatedFlag for United States of America asked on
Microsoft SQL ServerPowershellSQL
6 Comments1 Solution83 ViewsLast Modified:
so I have the following powershell script, the script works however i would like to add a column to the front of the table that is simply 'date' and every line would have '$dMonth' in the beginning of the csv.

$dMonth= (Get-Date).AddMonths(-0).ToString("dd-MM-yyyy")
$DBServer = "cdiserv1"
$databasename = "SUBSCRIBER"
$Connection = new-object
$Connection.ConnectionString ="server=$DBServer;database=$databasename;trusted_connection=True"

$SqlCmd = New-Object System.Data.SqlClient.SqlCommand

$SqlQuery = @"
SELECT  isnull(B.AcctLineCode, '') AS OLC,
        isnull(B.AcctNum, '') AS OAN,
        isnull(A.AcctLineCode, '') as NLC,
        isnull(A.AcctNum, '') AS NAN,
        isnull(B.Name, '') AS ONA,
        isnull(A.Name, '') as NNA,
        isnull(B.UserField1, '') AS O1,
        isnull(B.UserField2, '') AS O2,
        isnull(B.UserField3, '') AS O3,
        isnull(A.UserField1, '') AS N1,
        isnull(A.UserField2, '') AS N2,
        isnull(A.UserField3, '') AS N3,
        case when A.AcctNum != B.AcctNum or A.AcctLineCode != B.AcctLineCode then 'ACCOUNT MOVED'
        when A.Name != B.Name then 'NAME CHANGE'
        when A.UserField1 != B.UserField1 OR A.UserField2 != B.UserField2 OR A.UserField3 != B.UserField3 then 'FEATURE CHANGE'
        when A.AccountID is not null and B.AccountID is null then 'ADDED ACCOUNT' 
        when A.AccountID is null and B.AccountID is not null then 'DEELETED ACCOUNT' end [Result]
FROM    [CDISERV1].[SUBSCRIBER].[dbo].[Subscriber Data] A
        FULL OUTER JOIN [CDISERV2].[SUBSCRIBER].[dbo].[Subscriber Data] B ON A.AccountID = B.AccountID
WHERE (A.AccountID is not null and B.AccountID is null)
OR (A.AccountID is null and B.AccountID is not null)
OR A.AcctLineCode != B.AcctLineCode
OR A.AcctNum != B.AcctNum
OR A.Name != B.Name
OR A.UserField1 != B.UserField1
OR A.UserField2 != B.UserField2
OR A.UserField3 != B.UserField3
$SqlCmd.CommandText = $SqlQuery
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$SqlCmd.Connection = $Connection
$dt = New-Object System.Data.DataTable
$SqlAdapter.Fill($dt) | Out-Null
$dt | Export-Csv -Path Z:\OP\CSV\AIS$dMonth.CSV -force -NoTypeInformation

i really wanted to have the script merge into a single file, as i run this script every evening just before midnight, if it would append the date would show when the data was extrapolated essentially as a log, i have discovered sadly this is not possible for Export-Csv with the version of powershell i am using so i merge the files after exporting.

my desired results would look like the following...
"15-05-2018","xxx","1234","xxx","1233","na","na","c7","n3","b5"," "," "," ","ACCOUNT MOVED"
Join our community to see this answer!
Unlock 1 Answer and 6 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros