简体   繁体   English

Powershell CSV到SQL查询

[英]Powershell CSV to SQL Query

I am back on my on-going learning into powershell, I have hit the limits of my capabilities. 我回到了对Powershell的持续学习中,已经达到了我的能力极限。 So wondering if anyone can throw me in the right direction and or tell me if what I am wanting to do is possible? 因此,想知道是否有人可以向正确的方向扔我,或者告诉我我想做的事情是否可能?

I have written a script which downloads a number of file(s) from the web and then deletes out all but the data that I wish to work with. 我编写了一个脚本,该脚本可以从Web上下载许多文件,然后删除我希望使用的数据以外的所有文件。 These are .csv's... 这些是.csv的...

I have added the Powershell code that I have managed to cobble together so far, when the data is downloaded see below. 到目前为止,我已经添加了我已经设法整合在一起的Powershell代码,下载数据时请参见下文。 The data extract of one of these files looks like this. 这些文件之一的数据提取看起来像这样。

Column A contains a description 列A包含说明

Column B contains an ISIN (this is the working information) B列包含一个ISIN(这是工作信息)

Column C contains a numerical figure C列包含一个数字

Column D is where I want the SQL query data to be returned to D列是我希望将SQL查询数据返回到的位置

Please find a link to the sample data Data Example Data Example 请找到示例数据的链接数据示例 数据示例

CSV提取

What I then want to achieve is to run the following SQL command based on the data within Column B 然后,我想要实现的是基于列B中的数据运行以下SQL命令

Select *
From CL
Where CLISIN in ('GB0004835483',
'BE0003793107',
'GB00B7V2GY97',
'GB0000595859',
'GB00B1VCNQ84',
'GB0004992003',
'GB0002369352')

I believe that I will need to export this as another file perhaps? 我相信可能需要将此导出为另一个文件吗? Though using the final results that are exported I then need to place these within Column D in this csv file. 尽管使用导出的最终结果,然后我需要将其放置在此csv文件的D列中。

Hoping that I have made this clear, if not please let me know and I will be as expansive as possible. 希望我已经明确了这一点,如果没有,请告诉我,我将尽可能地广泛。

Long and the short is can I use powershell to automatically run a query for each item in column B and add the results of that matching query to the correlating line in column D? 总而言之,我是否可以使用powershell为B列中的每个项目自动运行查询,并将匹配查询的结果添加到D列中的相关行?

I cannot find the answer via google nor here... 我无法通过Google或此处找到答案...

#### DOWNLOAD LOCATIONS ####
$DownloadPTMLocation =  "L:\Operations Database\TakeOverPanel\PTMDisclosureTable.xls"
$DownloadPTMCSVLocation =  "L:\Operations Database\TakeOverPanel\PTMDisclosureTable.csv"
$DownloadIPTMLocation = "L:\Operations Database\TakeOverPanel\IPTMDisclosureTable.xls"
$DownloadIPTMCSVLocation = "L:\Operations Database\TakeOverPanel\IPTMDisclosureTable.csv"

#### WEB URLS ###
$PTMURL = "http://www.thetakeoverpanel.org.uk/new/disclosureTable/v3/disclosuretable.xls"
$PTMCSVURL = "http://www.thetakeoverpanel.org.uk/new/disclosureTable/v3/disclosuretable.csv"
$IPTMURL = "http://irishtakeoverpanel.ie/disclosure/disclosuretable.xls"
$IPTMCSVURL = "http://irishtakeoverpanel.ie/disclosure/disclosuretable.csv"

$Path = Get-Location

#### Load Web Service #### 
$WebClient = New-Object System.Net.WebClient

#### Start download Process ####
Write-Host "Downloading PTM File 1 of 4" $Path -ForegroundColor Green
    $Url = $PTMURL
    $Path = $DownloadPTMLocation
    $WebClient.DownloadFile($PTMURL, $DownloadPTMLocation)

Write-Host "Downloading IPTM File 2 of 3" $Path -ForegroundColor Blue
    $Url = $IPTMURL 
    $Path = $DownloadIPTMLocation
    $WebClient.DownloadFile($IPTMURL, $DownloadIPTMLocation)

Write-Host "Downloading PTM Csv File 3 of 4" $Path -ForegroundColor Gray
    $Url = $PTMCSVURL
    $Path = $DownloadPTMCSVLocation
    $WebClient.DownloadFile($PTMCSVURL, $DownloadPTMCSVLocation)

Write-Host "Downloading IPTM File 4 of 4" $Path -ForegroundColor Red
    $Url = $IPTMCSVURL 
    $Path = $DownloadIPTMCSVLocation
    $WebClient.DownloadFile($IPTMCSVURL, $DownloadIPTMCSVLocation)



    #####################


    ## PTM ## 

#Customise Vars
$DownloadPTMCSVLocation = "L:\Operations Database\TakeOverPanel\PTMDisclosureTable.csv"
$OutputPTMCSVLocation = "L:\Operations Database\TakeOverPanel\PTMDisclosureTableb.csv"
$Match = "ISIN"
$Matchs = "NSI"

## Strips all lines that do not contain ISIN ##

(Get-Content $DownloadPTMCSVLocation) -match $Match | Out-File $OutputPTMCSVLocation

Remove-Item $DownloadPTMCSVLocation

Rename-Item $OutputPTMCSVLocation -NewName $DownloadPTMCSVLocation

(Get-Content $DownloadPTMCSVLocation) -match $Matchs | Out-File $OutputPTMCSVLocation

Remove-Item $DownloadPTMCSVLocation

Rename-Item $OutputPTMCSVLocation -NewName $DownloadPTMCSVLocation

Get-Content $DownloadPTMCSVLocation | % {
    $_ -replace 'ISIN: ',''
    } | % {
    $_ -replace 'NSI: ',''
    } | Set-Content $OutputPTMCSVLocation

So working on this further and having hit the wall (once more returning SQL query) here is the working code so far... 因此,进一步进行此工作并碰壁(更多返回SQL查询)是到目前为止的工作代码...

###########
## BEGIN ##
###########

#### DOWNLOAD LOCATIONS ####

$DownloadPTMCSVLocation =  "L:\Operations Database\TakeOverPanel\PTMDisclosureTable.csv"
$OutputPTMCSVLocation = "L:\Operations Database\TakeOverPanel\PTMDisclosureTableb.csv"


#### WEB URLS ###

$PTMCSVURL = "http://www.thetakeoverpanel.org.uk/new/disclosureTable/v3/disclosuretable.csv"


$Path = Get-Location

#### Load Web Service #### 
$WebClient = New-Object System.Net.WebClient

#### Start download Process ####


Write-Host "Downloading PTM Csv" $Path -ForegroundColor Gray
    $Url = $PTMCSVURL
    $Path = $DownloadPTMCSVLocation
    $WebClient.DownloadFile($PTMCSVURL, $DownloadPTMCSVLocation)



#####################


## PTM ## 

$Match = "ISIN"
$Matchs = "NSI"

Import-Csv $DownloadPTMCSVLocation -Header @("A", "ISIN", "NSI", "Output") | #Import the CSV
  Where { $_.ISIN -like "ISIN: ????????????" -and $_.NSI -like "NSI:*" } | #Filter rows
  Foreach-Object {
    $_.ISIN = $_.ISIN.Replace("ISIN: ", "")
    $_.NSI = $_.NSI.Replace("NSI: ", "")

    $query = "select CLIALPHASORTCODE, vl2securitynum, sum(cast(vl2beneficial as float)) as beneficial 
    from t5vaultsl2 vl2 left outer join t5client cli
    on vl2.VL2CLIENTNUM = cli.CLICODE

    where vl2.vl2securitynum = '$($_.ISIN)'

    group by CLIALPHASORTCODE, VL2SECURITYNUM"


##Credentials## 

$MISA = 'xx.xx.x.xx'
$MISB = 'xx.xx.x.xx'
$userName = 'UN'
$PassWord='PW'
$DB = 'reporting'


## CREATE MIS CREDENTIALS ##
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Data Source=$MISA;Initial Catalog=$DB;
Initial Catalog=$DB;User ID=$userName;Password=$PassWord;"

## - Runs Script from Set Location

$SqlCmd = New-Object System.Data.SqlClient.SqlCommand;
$SqlCMD.CommandText = $query;
$SqlCmd.Connection = $SqlConnection;

So script is now as follows... 所以脚本现在如下...

#### DOWNLOAD LOCATIONS ####

$DownloadPTMCSVLocation =  "L:\Operations Database\TakeOverPanel\PTMDisclosureTable.csv"
$OutputPTMCSVLocation = "L:\Operations Database\TakeOverPanel\PTMDisclosureTableb.csv"
$ExportLocation = "L:\Operations Database\TakeOverPanel\test.csv"
$ExportLocationb = "L:\Operations Database\TakeOverPanel\test.xml"


#### WEB URLS ###

$PTMCSVURL = "http://www.thetakeoverpanel.org.uk/new/disclosureTable/v3/disclosuretable.csv"
$Path = Get-Location


#### Load Web Service #### 
$WebClient = New-Object System.Net.WebClient

#### Start download Process ####


Write-Host "Downloading PTM Csv" $Path -ForegroundColor Gray
    $Url = $PTMCSVURL
    $Path = $DownloadPTMCSVLocation
    $WebClient.DownloadFile($PTMCSVURL, $DownloadPTMCSVLocation)



################
#### Query  ####
################

$query = @"
    select CLIALPHASORTCODE, vl2securitynum, sum(cast(vl2beneficial as float)) as beneficial 
    from t5vaultsl2 vl2 left outer join t5client cli
    on vl2.VL2CLIENTNUM = cli.CLICODE

    where vl2.vl2securitynum = '$($_.ISIN)'

    group by CLIALPHASORTCODE, VL2SECURITYNUM
"@;


#####################################

$Match = "ISIN"
$Matchs = "NSI"

## Prepare SQL ##

$MISA = 'xx.xx.x.xx'
$MISB = 'xx.xx.x.xx'
$userName = 'UN'
$PassWord='PW'
$DB = 'reporting'


## CREATE SQL Connection ##
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Data Source=$MISA;Initial Catalog=$DB;Initial Catalog=$DB;User ID=$userName;Password=$PassWord;"
$SqlConnection.Open()

# Put everything in a Try block so if there is an error the SQL
# connection is still closed
try
{
    $SqlCmd = $SqlConnection.CreateCommand()

    ## Process CSV ##

    Import-Csv $DownloadPTMCSVLocation -Header @("A", "ISIN", "NSI", "Output") | #Import the CSV
      Where { $_.ISIN -like "ISIN: ????????????" -and $_.NSI -like "NSI:*" } | #Filter rows
      Foreach-Object {
        $_.ISIN = $_.ISIN.Replace("ISIN: ", "")
        $_.NSI = $_.NSI.Replace("NSI: ", "")

        # Get data from SQL
        $query = "select CLIALPHASORTCODE, vl2securitynum, sum(cast(vl2beneficial as float)) as beneficial from t5vaultsl2 vl2 left outer join t5client cli on vl2.VL2CLIENTNUM = cli.CLICODE where vl2.vl2securitynum = '$($_.ISIN)' group by CLIALPHASORTCODE, VL2SECURITYNUM"

        $SqlCmd.CommandText  = $query
        $result = $SqlCmd.ExecuteReader()
        $table = New-Object "System.Data.DataTable"
        $table.Load($result)

        $t = $table.Vl2Beneficial

        # Pass row on through the pipeline
        $_.Output = $table.VL2Beneficial

        $table | Export-Clixml $ExportLocationb

        Write-Output $_
    } | Export-Csv $OutputPTMCSVLocation -NoTypeInformation
}
finally
{
    # Always close SQL connection even if error is encountered.
    $SqlConnection.Close()
}
## PTM ## 

$Match = "ISIN"
$Matchs = "NSI"

## Prepare SQL ##

$MISA = 'xx.xx.x.xx'
$MISB = 'xx.xx.x.xx'
$userName = 'UN'
$PassWord='PW'
$DB = 'reporting'

## CREATE SQL Connection ##
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Data Source=$MISA;Initial Catalog=$DB;Initial Catalog=$DB;User ID=$userName;Password=$PassWord;"
$SqlConnection.Open()

# Put everything in a Try block so if there is an error the SQL
# connection is still closed
try
{
    $SqlCmd = $SqlConnection.CreateCommand()

    ## Process CSV ##

    Import-Csv $DownloadPTMCSVLocation -Header @("A", "ISIN", "NSI", "Output") | #Import the CSV
      Where { $_.ISIN -like "ISIN: ????????????" -and $_.NSI -like "NSI:*" } | #Filter rows
      Foreach-Object {
        $_.ISIN = $_.ISIN.Replace("ISIN: ", "")
        $_.NSI = $_.NSI.Replace("NSI: ", "")

        # Get data from SQL
        $query = "select CLIALPHASORTCODE, vl2securitynum, sum(cast(vl2beneficial as float)) as beneficial from t5vaultsl2 vl2 left outer join t5client cli on vl2.VL2CLIENTNUM = cli.CLICODE where vl2.vl2securitynum = '$($_.ISIN)' group by CLIALPHASORTCODE, VL2SECURITYNUM"

        $SqlCmd.CommandText  = $query
        $result = $SqlCmd.ExecuteReader()
        $table = New-Object "System.Data.DataTable"
        $table.Load($result)

        # Pass row on through the pipeline
        $_.Output = $table.VL2Beneficial

        Write-Output $_
    } | Export-Csv $OutputPTMCSVLocation -NoTypeInformation
}
finally
{
    # Always close SQL connection even if error is encountered.
    $SqlConnection.Close()
}

Basically I threw out all the file renaming as it's completely unnecessary (use variables for that sort of thing if you need to), Import-Csv returns an array of objects, one for each row so I pipe them to the filter (where ISIN is checked to be 12 chars only) then loops through sorting out the data. 基本上,我删除了所有文件重命名,因为它完全没有必要(如果需要,可以使用变量进行此类操作),Import-Csv返回对象数组,每行一个对象,因此我将它们通过管道传递给过滤器(其中ISIN是(仅限12个字符),然后循环遍历数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM