整理Powershell脚本

时间:2018-11-12 21:55:06

标签: powershell

所以我需要帮助整理我拥有的脚本。该脚本的目的是根据18个不同的列标题下的数据制作18个不同的sql文件。我的脚本现在要做的是根据我选择通过“读取主机”输入的列制作1个sql文件。这是我当前的脚本

function get-header
{Read-Host "Type the Column header betwen B-Z for which sql files needs to be created"
}
 function get-column 
{ 
Read-Host "Type the Column number"
}

do
{
$val = get-header
}
while(!($val))

do
{$col = get-column
}
while(!($col))

switch ($val)
    {
       "B"{$column = "1"}
       "C"{$column = "2"}
       "D"{$column = "3"}
       "E"{$column = "4"}
       "F"{$column = "5"}
       "G"{$column = "6"}
       "H"{$column = "7"}
       "I"{$column = "8"}
       "J"{$column = "9"}
        "K"{$column = "10"}
    "L"{$column = "11"}
    "M"{$column = "12"}
    "N"{$column = "13"}
    "O"{$column = "14"}
    "P"{$column = "15"}
    "Q"{$column = "16"}
    "R"{$column = "17"}
    "S"{$column = "18"}
    "T"{$column = "19"}
    "U"{$column = "20"}
    "V"{$column = "21"}
    "W"{$column = "22"}
    "X"{$column = "23"}
    "Y"{$column = "24"}
    "Z"{$column = "25"}
    default { $column = 'Unknown' }

    }

    if ($column -eq 'Unknown')
   {
Write-Warning "Not a valid input"
return
}

$csv = Import-Csv "Indices Updates - September 2018.csv" -Header 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26

$date = (Get-Date -Format "MM/dd/yyyy").Replace("-","/")

$sql = @("INSERT INTO benchmark_values(created_at,benchmark_id,date,amount,created_by_username)


 foreach($data in $csv)
{

$secondcolumn = [int]$column + 1

$sql += "('$date',$col,'$($data.1)',$($data.$secondcolumn),'BPylla'),"

}


$sql | Out-File "sqldata.sql"

现在,我想完全摆脱读取主机,因为我不想输入任何值。我还将提供一个示例,说明csv文件的外观和sql文件的外观。

CSV File Image

SQL File Image

因此,目标是使用发布的sql格式从信息的每一列中生成不同的sql文件。现在我的脚本中已经有了用于该模板的模板,我只需要该脚本即可基于标题创建所有sql文件,并且仍在新sql文件中的标题下方输入数据。任何帮助将不胜感激!谢谢!

0 个答案:

没有答案