我有一个脚本,该脚本可以从Citrix(未注册的计算机,以mm为单位等)生成一些信息。如果我在PowerShell ISE中运行此代码,则HTML可以正常运行。如果我使用相同的凭据将其作为计划的任务运行,它将以纯文本格式显示。在电子邮件的正文和附件中。任务设置为powershell.exe
,参数是路径。它设置为无论用户是否登录并具有最高权限都可以运行。
$head = @"
<style>
body { background-color:#E5E4E2;
font-family:Monospace;
font-size:10pt; }
td, th { border:0px solid black;
border-collapse:collapse;
white-space:pre; }
th { color:white;
background-color:black; }
table, tr, td, th { padding: 2px; margin: 0px ;white-space:pre; }
tr:nth-child(odd) {background-color: lightgray}
table { width:95%;margin-left:5px; margin-bottom:20px;}
h2 {
font-family:Tahoma;
color:#6D7B8D;
}
.footer
{ color:green;
margin-left:10px;
font-family:Tahoma;
font-size:8pt;
font-style:italic;
}
</style>
"@
$bd = Get-BrokerDesktop -MaxRecordCount 5000
$unregisteredonlineVMs = $bd |
? {
($_.RegistrationState -eq 'Unregistered') -and
($_.PowerState -eq 'On')
} |
select HostedMachineName,DesktopGroupName,LastDeregistrationReason |
ConvertTo-Html -Property HostedMachineName, DesktopGroupName, LastDeregistrationReason -Head $head
$unregisteredofflineVMs = $bd |
? {
($_.RegistrationState -eq 'Unregistered') -and
($_.PowerState -eq 'Off')
} |
select HostedMachineName,DesktopGroupName,LastDeregistrationReason |
ConvertTo-Html -Property HostedMachineName, DesktopGroupName, LastDeregistrationReason -Head $head
$unknownVMs = $bd |
? {
($_.RegistrationState -eq 'Unregistered') -and
($_.PowerState -eq 'Unknown')
} |
select DNSName,DesktopGroupName,LastDeregistrationReason |
ConvertTo-Html -Property DNSName, DesktopGroupName, LastDeregistrationReason -Head $head
$mmVMs = $bd |
? { $_.InMaintenanceMode -eq 'True' } |
select HostedMachineName,DesktopGroupName,LastDeregistrationReason |
ConvertTo-Html -Property HostedMachineName, DesktopGroupName, LastDeregistrationReason -Head $head
$body = "<h1><b>Unregistered Online</b></h1> $unregisteredonlineVMs" + "`n`n" + "<h1><b>Unregistered Offline</b></h1> `n`n $unregisteredofflineVMs" + "`n`n" + "<h1><b>Unregistered Unknown Powerstate</b></h1> `n`n $unknownVMs" + "`n`n" + "<h1><b>Maintenance Mode</b></h1> `n`n $mmVMs"
$attach = "<h1><b>Unregistered Online</b></h1> $unregisteredonlineVMs" + "`n`n" + "<h1><b>Unregistered Offline</b></h1> `n`n $unregisteredofflineVMs" + "`n`n" + "<h1><b>Unregistered Unknown Powerstate</b></h1> `n`n $unknownVMs" + "`n`n" + "<h1><b>Maintenance Mode</b></h1> `n`n $mmVMs" | Out-File C:\Temp\VirtualDesktopCheck\XenDestkopCheck_$time.html
Send-MailMessage -From $fromEmail -To $recipients -Subject "XenDesktop Daily Check $time" -Body $body -Priority High -SmtpServer $server -BodyAsHtml -Attachments C:\Temp\VirtualDesktopCheck\XenDestkopCheck_$time.html