2017-04-04 165 views
0

我希望以下腳本中的圖像與表格上方的右上角對齊。目前它與表格左上方對齊,這可能嗎?另外,我想在表格頂部添加一個額外的標題行,使用固定標題跨越表格的寬度,這是否可以不破壞表格?將表中的圖像對齊並添加標題標題行

#Set variables 
$image = "C:\Scriptrepository\XXXXX.gif" 
$ImageBits = [Convert]::ToBase64String((Get-Content $image -Encoding Byte)) 
$ImageHTML = "<img src=data:image/gif;base64,$($ImageBits) alt=XXXXX;/>" 
$CurrentDate = Get-Date -Format F 
$Post = "<br><br> Members of $ADGroupName <br> Generated on $CurrentDate" 

#Function for alternating table colour rows 
function global:Set-AlternatingRows { 
    [CmdletBinding()] 
    Param(
     [Parameter(Mandatory, ValueFromPipeline)] 
     [string]$Line, 

     [Parameter(Mandatory)] 
     [string]$CSSEvenClass, 

     [Parameter(Mandatory)] 
     [string]$CSSOddClass 
    ) 

    Begin { 
     $ClassName = $CSSEvenClass 
    } 
    Process { 
     if ($Line.Contains("<tr><td>")) { 
      $Line = $Line.Replace("<tr>","<tr class=""$ClassName"">") 
      if ($ClassName -eq $CSSEvenClass) { 
       $ClassName = $CSSOddClass 
      } else { 
       $ClassName = $CSSEvenClass 
      } 
     } 
     return $Line 
    } 
} 

#Function to validate request and create variable from input 
function global:GetADGroupMembersrecursively { 
    Write-Host "Enter Distribution List/AD Group name:" -ForegroundColor Green 
    $global:ADGroupName = Read-Host 
    if ([string]::IsNullOrEmpty($ADGroupName)) { 
     cls 
     Write-Host -ForegroundColor red "Cannot be blank, please re-enter AD Group/distribution list name" | Out-Null 
     GetADGroupMembersRecursively 
    } 
    $global:DLCheck = DSQuery group -Name "$ADGroupName" 
    if ($DLCheck -eq $null) { 
     cls 
     Write-Host -ForegroundColor red "Did not find AD Group/Distribution List, please verify this is the correct name" | Out-Null 
     GetADGroupMembersRecursively 
    } 
} 

GetADGroupMembersRecursively 

$Head = @" 
<style> 
BODY {font-family: Arial; font-size: 8pt; color: #4C607B;} 
TABLE{margin: auto; font-family: Segoe UI; box-shadow: 10px 10px 5px #888; width: 100%;height :50px;} 
TH, TD {border: 1px solid #ffffff; border-collapse: collapse;padding: 3px;} 
TH {font-size: 1.2em; background-color: #003366; color: #ffffff; } 
TD {color: #000000; } 
.even { background-color: #efefef; } 
.odd { background-color: #c0c0c0; } 
TR { background: #b8d1f3; } 
H4 {font-family: Arial; font-size: 12pt; color: #4C607B;align=right;} 
H5 {font-family: Arial; font-size: 8pt; color: #4C607B;align=right;} 
</style> 
"@ 

#Table creation 
Get-ADGroupMember -Identity $ADGroupName -Recursive | 
    Get-ADUser -pr GivenName, Surname, mail, sAMAccountName | 
    Select-Object GivenName, Surname, mail, sAMAccountName | 
    sort Surname | 
    ConvertTo-Html -Head $head -Body $ImageHTML -PostContent $Post -As Table | 
    Set-AlternatingRows -CSSOddClass odd -CSSEvenClass even | 
    Out-File C:\ScriptRepository\Results\$ADGroupName.htm 
Invoke-Item C:\ScriptRepository\Results\$ADGroupName.htm 
+1

首先,制定出_result_應該是什麼 - 什麼HTML和CSS會提供你想要的輸出嗎?一旦你有了,編寫PowerShell代碼來生成它應該相當簡單。 –

+0

它從AD查找生成一個表格,表格工作正常,只是將圖像放置在表格的右側,並在表格的頂部添加一行,並標題出現問題。 –

+1

使用正確的HTML和CSS構建帶有虛擬表格的網頁,看起來像您想要的樣子。如果需要的話,可以手工完成 - 即使它沒有「真實」數據,你也只是想要一些看起來正確的東西。然後,使用它作爲指導,您應該能夠計算出腳本應該發出的HTML和CSS,以包裝您從AD查找創建的表格。 –

回答

0

把圖像中的塊級元素(<p>例如),並調整該元素的權利:

... 
$ImageHTML = "<p class='pic'><img src=.../></p>" 
... 
$Head = @" 
<style> 
... 
p.pic {text-align: right} 
... 
</style> 
"@ 
... 
+0

Cheers Ansgar,我將\t p.pic {text-align:right}添加到$ Head,並按照您的建議修改$ ImageHTML =「

NHSImprovement;/「它的功能完美。非常感謝您的時間和幫助,非常感謝。 –