我有一个 Powershell 脚本,它针对 SQL Server 2008R2 数据库执行 2 个 SQL 语句,目前它将数据放入 2 个 CSV 文件中。
#Connection Strings
$Database = "*****SQL"
$Server = "L*******2008R2"
#SMTP Relay Server
#Export File
$AttachmentPath = "\\c******.csv"
$TempAttachmentPath = "\\cd*****.cvs"
$AttachmentPath2 = "\\p**********.cvs"
$TempAttachmentPath2 = "\\m**********.cvs"
# Connect to SQL and query data, extract data to SQL Adapter
$SqlQuery = "Select Distinct
s*****t.s*****e,
p*****n.p******y,
p******n.p*****l,
p*****n.p*****m + Replicate(' ', (50 - Len(p*****n.p*****am))),
p*****n.p*****am,
Case When Len(a*****s.*****en) > 0 Then RTrim(ad****s.a*****n) + ', ' Else ''
End + ad****ss.a****t + Replicate(' ',(100 - len(Case When Len(ad*****s.a****n) > 0 Then RTrim(ad*****s.a******n) + ', ' Else ''
End + a******s.a*****t))),
a*******s.a******y,
a*******s.a******n,
a*******s.a******d,
p*****on.p******b
From
p*****n Inner Join
s*****t On p****.pe****d = st****t.p****y Inner Join
a*****s On pe****n.a****ey = a****s.a****d Inner Join
e****l On s*****t.s*****d = e*****l.s*****y Inner Join
c*****o On e****l.c*****y = c*****e.c*****d
Where
s*****t.pa*****y = (Select
Max(s.p*****y)
From
st*****nt s
Where
s.s*****de = st*****t.s******e) And
p*****n.p*****e = 'S**T' And
c****e.p*****ey >= 34
Order By
s******t.s******de"
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Data Source=$Server;Initial Catalog=$Database;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
$DataSet = New-Object System.Data.DataSet
$nRecs = $SqlAdapter.Fill($DataSet)
$nRecs | Out-Null
#Populate Hash Table
$objTable = $DataSet.Tables[0]
#Export Hash Table to CSV File
$objTable | Export-CSV $TempAttachmentPath
gc $TempAttachmentPath | ? { $_ -notlike '#TYPE System.Data.DataRow' } | sc $AttachmentPath
(gc $AttachmentPath) | % {$_ -replace '"', ""} | out-file $AttachmentPath -Fo -En ascii
Remove-Item $TempAttachmentPath
$SqlQuery = "Select
RTrim(c*****e.csecode) + Replicate(' ', 24 - Len(c*****e.****de)),
Left(RTrim(c****e.c******c), 50) + Replicate(' ', 50 - Len(Left(c*****e.c*****c,50))),
s*****s.pa*****r
From
co****e Inner Join
s****s On ****e.p****ey = s****rs.p****d
Where
c*****e.p*****y >= 36"
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Data Source=$Server;Initial Catalog=$Database;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
$DataSet = New-Object System.Data.DataSet
$nRecs = $SqlAdapter.Fill($DataSet)
$nRecs | Out-Null
#Populate Hash Table
$objTable = $DataSet.Tables[0]
#Export Hash Table to CSV File
$objTable | Export-CSV $TempAttachmentPath2
gc $TempAttachmentPath2 | ? { $_ -notlike '#TYPE System.Data.DataRow' } | sc $AttachmentPath2
(gc $AttachmentPath2) | % {$_ -replace '"', ""} | out-file $AttachmentPath2 -Fo -En ascii
Remove-Item $TempAttachmentPath2
此代码有效,但是有一个新请求。用户希望这些数据在一个没有分隔符的文本文件中,只是数据库中的列宽。
表格1
field1 Char(10)
field2 Char(12)
field3 Char(14)
Field4 Char(7)
文件中的数据:
Another One Bites Bites
我是 Powershell 的新手,所以我需要一些帮助
谢谢保罗