Основная проблема с вашим кодом заключается в том, что вы используете Set-Content
без пути и просто добавляете байтовые данные в тег <td>..</td>
.
HTML имеет тег <img>
для см., например, здесь .
Затем, часть для получения желаемых пользовательских свойств слишком сложна (сначала вы получаете ВСЕ свойства с помощью -Properties *
, а затем выполняете Get-ADUser
во второй раз, снова возвращая все свойства, чтобы окончательно отфильтровать их, используя Select-Object
).
Попробуйте ниже:
$title = 'All ADUsers'
# collect an array of html <tr> table rows and join together with a newline
$table = (Get-ADUser -Filter * -Properties DisplayName,EmailAddress,OfficePhone,Department,Title,thumbnailPhoto |
Sort-Object Department,Enabled |
ForEach-Object {
# if there is a thumbnailPhoto for this user, create an <img> tag, otherwise use a non breaking space
$photo = if ($_.thumbnailPhoto) {
# method 1)
# save the image data to file in the current path and link that in the HTML
$file = "$($_.DisplayName).jpg"
$_.thumbnailPhoto | Set-Content -Path $file -Encoding Byte -Force
'<img src="{0}" alt="{1}"/>' -f $file, $_.DisplayName
# method 2)
# encode the bytes of the image in a Base64 string and inline embed it in the page
# the resulting HTML file will become quite large because of this, but the advantage is
# that there is no need to store the images as publically accessable files.
# '<img src="data:image/jpeg;charset=utf-8;base64,{0}" alt="{1}"/>' -f [Convert]::ToBase64String($_.thumbnailPhoto), $_.DisplayName
}
else { ' ' }
# create and output a new table row with the properties for the user
'<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td></tr>' -f $_.DisplayName,
$_.EmailAddress,
$_.OfficePhone,
$_.Department,
$_.Title,
$photo
}) -join [Environment]::NewLine
# next, create the html using a here-string
$html = @"
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>$title</title>
<style type="text/css">
{
margin: 0;
padding: 0
}
@import url(https://fonts.googleapis.com/css?family=Indie+Flower|Josefin+Sans|Orbitron:500|Yrsa);
body {
text-align: center;
font-family: 14px/1.4 'Indie Flower', 'Josefin Sans', Orbitron, sans-serif;
font-family: 'Indie Flower', cursive;
font-family: 'Josefin Sans', sans-serif;
font-family: Orbitron, sans-serif
}
#page-wrap {
margin: 50px
}
tr:nth-of-type(odd) {
background: #eee
}
th {
background: #EF5525;
color: #fff;
font-family: Orbitron, sans-serif
}
td,
th {
padding: 6px;
border: 1px solid #ccc;
text-align: center;
font-size: large
}
table {
width: 90%;
border-collapse: collapse;
margin-left: auto;
margin-right: auto;
font-family: Yrsa, serif
}
</style>
</head>
<body>
<table>
<tr><th>Name</th><th>Email</th><th>Phone Number</th><th>Department</th><th>Title</th><th>Photo</th></tr>
$table
</table>
</body>
</html>
"@
# save the html file in the current directory or send it with Send-MailMessage
$html | Set-Content -Path 'AllUsers.html' -Encoding UTF8