将CSV内容通过电子邮件发送到表格

时间:2018-12-03 15:37:24

标签: powershell

我正在开发一个Powershell脚本,该脚本从五列逗号分隔符CSV中获取数据,并将其加载到五列HTML表中以通过电子邮件发送。当前,该代码仅输出一行,而不是CSV文件中的15行。您能帮我将CSV中的15行打印到表格中吗?

$current_date = Get-Date
$current_date.ToUniversalTime()

$server_name = $env:COMPUTERNAME

$file = Get-Content .\data_file.csv 
Write-Output "file length: " $file.Length
for ($i = 1; $i -lt $file.Length; $i++) {
    $firstname = ([string]$file[$i]).Split(',')[0];
    $lastname = ([string]$file[$i]).Split(',')[1]; 
    $gender = ([string]$file[$i]).Split(',')[2]; 
    $age = ([string]$file[$i]).Split(',')[3];
    $dob = ([string]$file[$i]).Split(',')[4];
}

$mailsender = @{
    Body = "<p style='font-family: Calibri;'>Audit Report as of: $current_date</p>
<table style='border-collapse: collapse; font-family: Calibri;'>
    <tr style='border: 1px solid #dddddd;text-align: left;padding: 8px;background-color:#f7f7f7;'>
        <th style='border: 1px solid #dddddd;text-align: left;padding: 8px;'>First Name</th>
        <th style='border: 1px solid #dddddd;text-align: left;padding: 8px;'>Last Name</th>
        <th style='border: 1px solid #dddddd;text-align: left;padding: 8px;'>Gender</th>
        <th style='border: 1px solid #dddddd;text-align: left;padding: 8px;'>Age</th>
        <th style='border: 1px solid #dddddd;text-align: left;padding: 8px;'>DoB</th>
    </tr>
    <tr style='border: 1px solid #dddddd;text-align: left;padding: 8px;'>
        <th style='border: 1px solid #dddddd;text-align: left;padding: 8px;'>$firstname</td>
        <th style='border: 1px solid #dddddd;text-align: left;padding: 8px;'>$lastname</td>
        <th style='border: 1px solid #dddddd;text-align: left;padding: 8px;'>$gender</td>
        <th style='border: 1px solid #dddddd;text-align: left;padding: 8px;'>$age</td>
        <th style='border: 1px solid #dddddd;text-align: left;padding: 8px;'>$dob</td>
    </tr>
</table>"
    From = 'john.doe@email.com'
    To = 'john.doe@email.com'
    SmtpServer = 'mailhost.net'
    BodyAsHtml = $true
    Subject = "[$env:COMPUTERNAME] Audit Report"
}

Send-MailMessage @mailsender

3 个答案:

答案 0 :(得分:1)

您必须在for循环中定义一个变量,并向其中添加每组表行,然后在Body声明中调用该变量:

$current_date = Get-Date
$current_date.ToUniversalTime()

$server_name = $env:COMPUTERNAME

$file = Get-Content .\data_file.csv 
Write-Output "file length: " $file.Length
for ($i = 1; $i -lt $file.Length; $i++)
{ 
$firstname = ([string]$file[$i]).Split(',')[0] ;
$lastname = ([string]$file[$i]).Split(',')[1] ; 
$gender = ([string]$file[$i]).Split(',')[2] ; 
$age = ([string]$file[$i]).Split(',')[3] ; 
$dob = ([string]$file[$i]).Split(',')[4] ;

$data += "<tr style='border: 1px solid #dddddd;text-align: left;padding: 8px;'>
        <th style='border: 1px solid #dddddd;text-align: left;padding: 8px;'>$firstname</td>
        <th style='border: 1px solid #dddddd;text-align: left;padding: 8px;'>$lastname</td>
        <th style='border: 1px solid #dddddd;text-align: left;padding: 8px;'>$gender</td>
        <th style='border: 1px solid #dddddd;text-align: left;padding: 8px;'>$age</td>
        <th style='border: 1px solid #dddddd;text-align: left;padding: 8px;'>$dob</td>
    </tr>"
}

$mailsender=@{
Body="<p style='font-family: Calibri;'>Audit Report as of: $current_date</p>
<table style='border-collapse: collapse; font-family: Calibri;'>
    <tr style='border: 1px solid #dddddd;text-align: left;padding: 8px;background-color:#f7f7f7;'>
        <th style='border: 1px solid #dddddd;text-align: left;padding: 8px;'>First Name</th>
        <th style='border: 1px solid #dddddd;text-align: left;padding: 8px;'>Last Name</th>
        <th style='border: 1px solid #dddddd;text-align: left;padding: 8px;'>Gender</th>
        <th style='border: 1px solid #dddddd;text-align: left;padding: 8px;'>Age</th>
        <th style='border: 1px solid #dddddd;text-align: left;padding: 8px;'>DoB</th>
    </tr>
    $data
</table>"
From='john.doe@email.com'
To='john.doe@email.com'
   SmtpServer='mailhost.net'
    BodyAsHtml=$true
    Subject="[$env:COMPUTERNAME] Audit Report"
}

Send-MailMessage @mailsender

答案 1 :(得分:1)

使用PreparedStatement读取您的数据,然后使用protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); rowItem = new AppList(); final ListView userInstalledApps = (ListView)findViewById(R.id.appListView); List<AppList> installedApps = getInstalledApps(); CustomAppAdapter installedAppAdapter = new CustomAppAdapter(MainActivity.this, installedApps); userInstalledApps.setAdapter(installedAppAdapter); userInstalledApps.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { TextView appName = (TextView)adapterView.findViewById(R.id.list_app_name); Toast.makeText(getApplicationContext(), "Clicked "+ installedApps.get(i).getName(),Toast.LENGTH_LONG).show(); } }); } 将其转换为HTML。添加CSS作为标题。

Import-Csv

如果您需要将更多内容(而不只是表)放入HTML正文中,请将日期转换为HTML片段,并在构建最终HTML时使用该片段:

ConvertTo-Html

答案 2 :(得分:0)

cmdlet Import-Csv比字符串拆分更好地为您提供服务。尝试导入CSV并像这样在循环中创建行:

$current_date = Get-Date
$current_date.ToUniversalTime()

$server_name = $env:COMPUTERNAME

$csv = Import-Csv ".\my_file.csv"
$html = ""
foreach ($row in $csv) {
    $html += "<tr style='border: 1px solid #dddddd;text-align: left;padding: 8px;'>"
    $html += "<th style='border: 1px solid #dddddd;text-align: left;padding: 8px;'>$($row.firstname)</td>"
    $html += "<th style='border: 1px solid #dddddd;text-align: left;padding: 8px;'>$($row.lastname)</td>"
    $html += "<th style='border: 1px solid #dddddd;text-align: left;padding: 8px;'>$($row.gender)</td>"
    $html += "<th style='border: 1px solid #dddddd;text-align: left;padding: 8px;'>$($row.age)</td>"
    $html += "<th style='border: 1px solid #dddddd;text-align: left;padding: 8px;'>$($row.dob)</td>"
    $html += "</tr>"
}

$mailsender = @{
    Body       = "<p style='font-family: Calibri;'>Audit Report as of: $current_date</p>
<table style='border-collapse: collapse; font-family: Calibri;'>
    <tr style='border: 1px solid #dddddd;text-align: left;padding: 8px;background-color:#f7f7f7;'>
        <th style='border: 1px solid #dddddd;text-align: left;padding: 8px;'>First Name</th>
        <th style='border: 1px solid #dddddd;text-align: left;padding: 8px;'>Last Name</th>
        <th style='border: 1px solid #dddddd;text-align: left;padding: 8px;'>Gender</th>
        <th style='border: 1px solid #dddddd;text-align: left;padding: 8px;'>Age</th>
        <th style='border: 1px solid #dddddd;text-align: left;padding: 8px;'>DoB</th>
    </tr>
    $html    
</table>"
    From       = 'john.doe@email.com'
    To         = 'john.doe@email.com'
    SmtpServer = 'mailhost.net'
    BodyAsHtml = $true
    Subject    = "[$env:COMPUTERNAME] Audit Report"
}