SQL Server 2008R2 データベースに対して 2 つの SQL ステートメントを実行する Powershell スクリプトがあり、現在、データを 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を初めて使用するので、助けてください
ありがとうポール