2017-12-21 1 views
0

나는 PowerShell을 사용하여 bcp 데이터를 출력하고 싶습니다. 여러 bcp 명령을 시작해야하고 bcp 명령 문자열을 채우기 위해 텍스트 파일에서 1. 테이블 이름과 2. 필드 이름을 읽으려고합니다.텍스트 파일에서 2 개의 변수 읽기 및 반복하기

현재이 방법은 효과가 없지만 2 가지 변수를 통합하는 방법을 모르겠습니다. 내 사기 스크립트를 수정 한

 
table: table1 
table: table2 
field: field1 
field: field2 
# Log file time stamp: 
$LogTime = Get-Date -Format "MM-dd-yyyy_hh-mm-ss" 
# Log file name: 
$LogFile = "C:\Administration\Logs\BCPEXPORTLOG_" + $LogTime + ".log" 

$database = "database" 
$schema = "dbo" 
$table = "TableName" 
$tablename = Get-Content 'C:\Administration\Scheduled Tasks\CUBTableList.txt' | 
      ? { $_ -match '^\s*table:\s*' } | 
      select -First 1 | 
      % { ($_ -split ':\s*', 2)[1] } 
$fieldname = Get-Content 'C:\Administration\Scheduled Tasks\CUBTableList.txt' | 
      ? { $_ -match '^\s*field:\s*' } | 
      select -First 1 | 
      % { ($_ -split ':\s*', 2)[1] } 

foreach ($line in Get-Content 'C:\Administration\Scheduled Tasks\CUBTableList.txt') { 
    $bcp_command = "bcp 'SELECT * FROM $database.$schema.$tablename WHERE ($fieldname <= DATEADD(ms, -3, GETDATE()))' QUERYOUT 'D:\BCPOut\$database`_$tablename.txt' -c -U 'user' -P 'password'" 
    Tee-Object -FilePath $LogFile -InputObject $bcp_command -Append 
    $bcp_results = Invoke-Expression $bcp_command 
    Tee-Object -FilePath $LogFile -InputObject $bcp_results -Append 
} 

답변

0

: 같은

텍스트 파일을 찾습니다.

대신 import-csv를 사용하십시오. 이 코드

# Log file time stamp: 
$LogTime = Get-Date -Format "MM-dd-yyyy_hh-mm-ss" 
# Log file name: 
$LogFile = "C:\Administration\Logs\BCPEXPORTLOG_"+$LogTime+".log" 

$database = "database" 
$schema = "dbo" 
$table = "TableName" 

Import-CSV 'C:\Administration\Scheduled Tasks\CUBList.csv' | ForEach{ 

     $bcp_command = "bcp 'SELECT * FROM $database.$schema." + $_.tablename + " WHERE (" + $_.fieldname + " <= DATEADD(ms, -3, GETDATE()))' QUERYOUT 'D:\BCPOut\$database`_" + $_.tablename + ".txt' -c -U 'user' -P 'password'" 
     Tee-Object -FilePath $LogFile -InputObject $bcp_command -Append 
     $bcp_results = Invoke-Expression $bcp_command 
     Tee-Object -FilePath $LogFile -InputObject $bcp_results -Append 
    } 
0

시도 :

$LogFile = "c:\temp\BCPEXPORTLOG_{0:MM-dd-yyyy_hh-mm-ss}.log" -f (Get-Date) 
$database = "database" 
$schema = "dbo" 

#split with 'table:' 
$Split1=(Get-Content c:\temp\CUBTableList.txt) -split 'table:' 

#split fields and take only tablename <>'' and table have field 
$Elements=$Split1 | %{ 
$Split2=$_ -split 'field:' 
$table=$Split2[0]; 
[pscustomobject]@{TableName=$Split2[0].Trim(); Fields=$Split2[1..$($Split2.Length)] | %{$_.Trim()}} | where {$_.TableName -ne '' -and $_.Fields -ne $null} 
} 

[email protected]() 

#build bcp commands 
$Elements | %{ 

    $TableName=$_.TableName 

    $_.Fields | %{ 

    $bcp_command+="bcp 'SELECT * FROM {0}.{1}.{2} WHERE {3} <= DATEADD(ms, -3, GETDATE())' QUERYOUT 'D:\BCPOut\{0}_{2}_{3}.txt' -c -U 'user' -P 'password'" -f $database, $schema, $TableName, $_ 

    } 
} 

#run commands 
$bcp_command | %{ 

    $_ | out-file $LogFile -Append 

    try 
    { 
     $result=Invoke-Expression $_ 
    } 
    catch 
    { 
     $result=$_.Exception.Message 
    } 
    finally 
    { 
     $result | Out-File $LogFile -Append 
    } 


} 
관련 문제