HTML 格式化报告 – 通过 PowerShell 发送电子邮件

HTML formatted report – email via PowerShell

用例

  1. 生成过去 30 天内创建的 AD 帐户
  2. 导出为 Csv 或 txt
  3. 将数据导入 html 格式的报告电子邮件给系统管理员

问题:收到的电子邮件中所有行数据显示为:System.Object[] 回购步骤: 导出数据:

$When = ((Get-Date).AddDays(-30)).Date
Get-ADUser -Filter {whenCreated -ge $When} -Properties Name, EmailAddress,SamAccountName, LastLogonDate,whenCreated, passwordlastset, Enabled, lockedout |
Select SamAccountName,Name,EmailAddress,LastLogonDate,whenCreated,passwordlastset,Enabled,lockedout | 
Export-Csv -Path "\iteminfo.txt" –NoTypeInformation 

导入以创建报告 如此 post

$users = Get-Content "\iteminfo.txt" 

# Table name
$tabName = "Report"

#Create Table object
$table = New-Object system.Data.DataTable "$tabName"

#Define Columns
$col1 = New-Object system.Data.DataColumn "User Name",([string])
$col2 = New-Object system.Data.DataColumn "Email Adderss",([string])
$col3 = New-Object system.Data.DataColumn "Login ID",([string])
$col4 = New-Object system.Data.DataColumn "Creation date",([string])
$col5 = New-Object system.Data.DataColumn "Last logon date",([string])
$col6 = New-Object system.Data.DataColumn "Last password reset",([string])
$col7 = New-Object system.Data.DataColumn "Enabled",([string])
$col8 = New-Object system.Data.DataColumn "Locked",([string])

#Add the Columns
$table.columns.add($col1)
$table.columns.add($col2)
$table.columns.add($col3)
$table.columns.add($col4)
$table.columns.add($col5)
$table.columns.add($col6)
$table.columns.add($col7)
$table.columns.add($col8)

ForEach ($item in $users)
{
        $userdata = (Get-ADUser -identity $item -properties Name,EmailAddress,SamAccountName,LastLogonDate,whenCreated,passwordlastset,Enabled,lockedout )
        
        #Create a row
        $row = $table.NewRow()

        #Enter data in the row
        $row."User name" = ($userdata."Name")
        $row."Email Address" = ($userdata."EmailAddress")
        $row."User ID" = ($userdata."SamAccountName")
        $row."Creation date" = ($userdata."created")
        $row."Last logon date" = ($userdata."LastLogonDate")
        $row."Last password reset" = ($userdata."PasswordLastSet")
        $row."Enabled" = ($userdata."Enabled")
        $row."Locked" = ($userdata."Lockedout")

        #Add the row to the table
        $table.Rows.Add($row)

}
#Communication template
#Creating head style
$Head = @"
 
<style>
  body {
    font-family: "Arial";
    font-size: 8pt;
    color: #4C607B;
    }
  th, td { 
    border: 1px solid #e57300;
    border-collapse: collapse;
    padding: 5px;
    }
  th {
    font-size: 1.2em;
    text-align: left;
    background-color: #003366;
    color: #ffffff;
    }
  td {
    color: #000000;
    }
  .even { background-color: #ffffff; }
  .odd { background-color: #bfbfbf; }
</style>
 
"@

# Creating body
[string]$body = [PSCustomObject]$table | select -Property "User name","Email Address","User ID","Creation date","Last logon date","Last password reset","Enabled","Locked" | sort -Property "User name"  | ConvertTo-HTML -head $head -Body "<font color=`"Red`"><h4> Account(s) Created In The Last 30 Days and Status </h4></font> 

而且我一直收到 system.object[] 错误,我认为这可能是由于我导入数据的方式所致。有谁知道如何解决这个问题?

你的例子有一些问题。

  1. 您需要使用
  2. 将内容转换为 PSObject

ConvertFrom-Csv $users = Get-Content "\iteminfo.txt" -Raw | ConvertFrom-Csv

  1. 在列定义/分配之间发现了一些其他错误(电子邮件地址/电子邮件地址和登录 ID/用户 ID

  2. 在执行 ForEach ($item...) 时,您将需要使用 $item.SamAccountName,因为 ConvertFrom-csv 已在第一步中实现,所以可以使用。

  3. 您实际上根本不需要为此使用数据table/数据行对象。如果您查询 SQL 数据库,您通常必须处理这个问题。在您的情况下,这是一层复杂性,不会给 table.

这是修改后的脚本

$When = ((Get-Date).AddDays(-30)).Date
Get-ADUser -Filter {whenCreated -ge $When} -Properties Name, EmailAddress,SamAccountName, LastLogonDate,whenCreated, passwordlastset, Enabled, lockedout |
Select SamAccountName,Name,EmailAddress,LastLogonDate,whenCreated,passwordlastset,Enabled,lockedout | 
Export-Csv -Path "\iteminfo.txt" –NoTypeInformation 

$users = Get-Content "\iteminfo.txt" -Raw | ConvertFrom-Csv

# Table name
$tabName = "Report"

#Create Table object
$table = New-Object system.Data.DataTable "$tabName"

#Define Columns
$AddCol = {}

$col1 = New-Object system.Data.DataColumn "User Name",([string])
$col2 = New-Object system.Data.DataColumn "Email Address",([string])
$col3 = New-Object system.Data.DataColumn "Login ID",([string])
$col4 = New-Object system.Data.DataColumn "Creation date",([string])
$col5 = New-Object system.Data.DataColumn "Last logon date",([string])
$col6 = New-Object system.Data.DataColumn "Last password reset",([string])
$col7 = New-Object system.Data.DataColumn "Enabled",([string])
$col8 = New-Object system.Data.DataColumn "Locked",([string])

#Add the Columns
$table.columns.add($col1)
$table.columns.add($col2)
$table.columns.add($col3)
$table.columns.add($col4)
$table.columns.add($col5)
$table.columns.add($col6)
$table.columns.add($col7)
$table.columns.add($col8)

ForEach ($item in $users)
{
        $userdata = (Get-ADUser -identity $item.SamAccountName -properties Name,EmailAddress,SamAccountName,LastLogonDate,whenCreated,passwordlastset,Enabled,lockedout )

        #Create a row
        $row = $table.NewRow()

        #Enter data in the row
        $row."User name" = ($userdata."Name")
        $row."Email Address" = ($userdata.EmailAddress)
        $row."Login ID" = ($userdata.SamAccountName)
        $row."Creation date" = ($userdata."created")
        $row."Last logon date" = ($userdata."LastLogonDate")
        $row."Last password reset" = ($userdata."PasswordLastSet")
        $row."Enabled" = ($userdata."Enabled")
        $row."Locked" = ($userdata."Lockedout")

        #Add the row to the table
        $table.Rows.Add($row)

}
#Communication template
#Creating head style
$Head = @"

<style>
  body {
    font-family: "Arial";
    font-size: 8pt;
    color: #4C607B;
    }
  th, td { 
    border: 1px solid #e57300;
    border-collapse: collapse;
    padding: 5px;
    }
  th {
    font-size: 1.2em;
    text-align: left;
    background-color: #003366;
    color: #ffffff;
    }
  td {
    color: #000000;
    }
  .even { background-color: #ffffff; }
  .odd { background-color: #bfbfbf; }
</style>

"@

# Creating body
[string]$body = [PSCustomObject]$table | select -Property "User name","Email Address","User ID","Creation date","Last logon date","Last password reset","Enabled","Locked" | sort -Property "User name"  | ConvertTo-HTML -head $head -Body "<font color=`"Red`"><h4> Account(s) Created In The Last 30 Days and Status </h4></font>"

关于您的脚本的补充说明

因为您在导入后为报告中的每个用户使用 Get-ADUser,所以 LastLogonDate / PasswordLastSet 等一些内容不会是您导出的值,而是来自 AD 的最实际值。

简化版

在所有情况下,您都不需要此处的数据 table 和数据行。 您可以简单地使用 Select 为您的数据分配自定义标签并从那里直接将其转换为 html(请注意,我在此处删除了辅助 Get-AdUser 以使用 CSV 报告数据,但是如果你想再次查询 AD,你可以在没有数据 table / 数据行的情况下以类似的方式进行。

$users = Get-Content "\iteminfo.txt" -Raw | ConvertFrom-Csv

$Head = @"

<style>
  body {
    font-family: "Arial";
    font-size: 8pt;
    color: #4C607B;
    }
  th, td { 
    border: 1px solid #e57300;
    border-collapse: collapse;
    padding: 5px;
    }
  th {
    font-size: 1.2em;
    text-align: left;
    background-color: #003366;
    color: #ffffff;
    }
  td {
    color: #000000;
    }
  .even { background-color: #ffffff; }
  .odd { background-color: #bfbfbf; }
</style>

"@


# Creating body
[string]$body = $users | Sort -Property Name |
select @{Name = 'User Name';Expression = {$_.Name}},
@{Name = 'Email Address';Expression = {$_.EmailAddress}},
@{Name = 'Login ID';Expression = {$_.SamAccountName}},
@{Name = 'Creation date"';Expression = {$_.created}},
@{Name = 'Last logon date';Expression = {$_.LastLogonDate}},
@{Name = 'Last password reset';Expression = {$_.PasswordLastSet}},
Enabled,
@{Name = 'Locked';Expression = {$_.Lockedout}} | 
ConvertTo-HTML -head $head -Body "<font color=`"Red`"><h4> Account(s) Created In The Last 30 Days and Status </h4></font>"