【问题标题】:Powershell Script - Decode Active Directory PhotoPowershell 脚本 - 解码 Active Directory 照片
【发布时间】:2020-05-08 19:46:37
【问题描述】:

我正在设置一个查看活动目录并获取用户名称、标题和缩略图照片的 powershell 脚本。

但是,当它试图显示照片时,它会以二进制形式返回而不是对其进行解码?我试图实现以粗体显示的代码的解码部分,但它不起作用。我试图让它在标签中显示图像作为最终产品。

尝试对其进行编码的代码如下:

$item.thumbnailPhoto | Set-Content $PhotoPath -Encoding byte

它通过 if 语句检查用户是否有照片。

主代码:

 PROCESS{
    Write-Verbose "Getting information from users"  
    $usersnames= get-aduser -filter * -Properties *
    $computer = $env:computername
    $AllInfoProperties=@()
    foreach($user in $usersnames){
        $IDName=$user.SamAccountName
        $AllInfoProperties+=Get-ADUser -Identity $IDName -Properties * | Select *
    }
        $AllUsersFilter=  $AllInfoProperties| Sort-Object Department,Enabled | Select DisplayName,EmailAddress,OfficePhone,Department,Title,thumbnailPhoto
        #Html5 part
     $html= '<!DOCTYPE HTML>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title>'
    $html+=$title
    $html+="</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>
"

foreach($item in  $AllUsersFilter){


    if ($item.thumbnailPhoto -ne ""){
        $item.thumbnailPhoto | Set-Content -Encoding byte
    }

    $html+="<tr> <td>$($item.DisplayName)</td>  <td>$($item.EmailAddress)</td>  <td>$($item.OfficePhone)</td>  <td>$($item.Department)</td>  <td>$($item.Title)</td>    <td>$($item.thumbnailPhoto)</td>    </tr></tr>"


}

$html+="
</table>
<br>
</body>
</html>"

【问题讨论】:

  • 我们还没有收到您的来信.. 我的回答解决了您的问题吗?如果是这样,请通过单击左侧的✓图标来考虑accepting它。这将帮助其他有类似问题的人更轻松地找到它。
  • 刚接触 SO 你可能不知道这一点,但习惯于点击左侧的 ✓ 图标accept the answer that solved your problem。这将帮助其他有类似问题的人更轻松地找到它,并有助于激励人们回答您未来的问题。

标签: windows powershell active-directory windows-server


【解决方案1】:

您的代码的主要问题是您使用了没有路径的Set-Content,并且您只是将字节数据添加到&lt;td&gt;..&lt;/td&gt; 标记中。

HTML 有&lt;img&gt; 标签,例如here

然后,获取所需用户属性的部分太复杂了(首先您使用-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 { '&nbsp;' }

    # 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

【讨论】:

    猜你喜欢
    • 2023-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-03
    • 1970-01-01
    • 1970-01-01
    • 2020-10-07
    相关资源
    最近更新 更多