arrays - HTML 格式的报告 – 通过 PowerShell 发送电子邮件

标签 arrays windows azure powershell powershell-3.0

用例:

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

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

$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[] 错误,我认为这可能是由于我导入数据的方式造成的。有谁知道如何解决这个问题? Received email

最佳答案

你的例子有一些问题。

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

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

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

  • 在执行 ForEach ($item...) 时,您将需要使用 $item.SamAccountName ,它将在 ConvertFrom- csv已在第一步中实现。

  • 您实际上根本不需要为此使用数据表/数据行对象。如果您查询 SQL 数据库,您通常必须处理这个问题。就您而言,这是一层复杂性,不会带来任何好处。

  • 这是修改后的脚本

    $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)将不是您导出的值,而是最实际的值广告。

    简化版本

    在所有情况下,您都不需要此处的数据表和数据行。 您只需使用 Select 为您的数据分配自定义标签,然后将其直接转换为 html(请注意,我在此处删除了辅助 Get-AdUser 以使用 CSV 报告数据,但如果您想再次查询 AD,您可以以类似的方式执行此操作,无需数据表/数据行)。

    $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>"
    

    关于arrays - HTML 格式的报告 – 通过 PowerShell 发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59711882/

    相关文章:

    c++ - MSVC 上的双字节编码 (std::codecvt):无法识别前导字节

    azure - 是否可以通过脚本或其他方式在 Azure App Insight 中自动创建警报?

    javascript - 我想在 javascript 中生成这样的数组 [ 1, 2, ..., 9, 10 ]

    objective-c - 初始化作为 C 数组的 Objective-C 类 ivar

    javascript - 使用变量访问未定义的数组结果

    azure - 新的 Azure SDK 1.7 和增强的发布是否允许自动将静态文件发布到 Blob 存储?

    azure - 保护 Azure Active Directory B2C 访问 token 和刷新 token

    javascript - 如何将生成的元素插入数组并在 React 中渲染它们

    java - 如何在 ScrolledComposite 中显示多行文本?

    linux - 应用时错误拉取图像配置./get-docker-images.sh