我正在尝试执行一系列步骤并创建一个HTML文件。以下是代码:
$bkpstat = @()
$a = Get-Date
$b = $a.AddDays(-1)
$b = $b.ToShortDateString();
$StartTime = "10:00:00 PM"
$EndTime = "11:00:00 PM"
$before = $b + " " + $StartTime
$after = $b + " " + $EndTime
$before = [datetime]$before
$after = [datetime]$after
$sms_svc = "SMS Agent Host","SMS_EXECUTIVE","SMS_SITE_COMPONENT_MANAGER","SMS_SITE_VSS_WRITER","SMS_REPORTING_POINT","SMS_SERVER_LOCATOR_POINT"
$smsdb_svc = "SMS Agent Host","SMS_SITE_SQL_BACKUP_WDSMS01"
$style = "<style>$(get-content C:\Temp\Rajiv\style.css)</style>"
$bkpsys = New-Object PSObject
$bkpsys | Add-Member -type NoteProperty -name SiteCode -value 000
$bkpstat += $bkpsys
if(Test-Connection WDSMS01 -Count 3 -BufferSize 3 -Quiet)
{
Write-Host "SMS01 is available. Checking service status"
$cmsvc_000 = Get-Service $sms_svc -ComputerName SMS01 | Convertto-HTML -Property Name, Status -PreContent "<h2>Service Status for SMS01 </h2>" | Out-String
Write-Host "Checking Site Server backup for 000"
$smsbkp_000 = Get-EventLog -ComputerName SMS01 -LogName Application -After $after -Before $before -Source "SMS Server" | ?{$_.EventID -eq 6833} | Select MachineName, Message
if ($smsbkp_000)
{
$bkpsys | Add-Member -type NoteProperty -Name SiteServer -Value "Success"
}
else
{
$bkpsys | Add-Member -type NoteProperty -Name SiteServer -Value "Failed"
}
}
else
{
Write-Host "Server SMS01 is not reachable"
$bkpsys | Add-Member -type NoteProperty -name SiteServer -value "Not Reachable"
}
if(Test-Connection SCCMSQL01 -Count 3 -BufferSize 3 -Quiet)
{
Write-Host "SCCMSQL01 is available. Checking service status"
$cmdbsvc_000 = Get-Service $smsdb_svc -ComputerName SCCMSQL01 | Convertto-HTML -Property Name, Status -PreContent "<h2>Service Status for SCCMSQL01 </h2>" | Out-String
Write-Host "Checking Database backup for SMS_000"
$sqlbkp_000 = Get-EventLog -ComputerName SCCMSQL01 -LogName Application -After $after -Before $before -Source "MSSQLSERVER" | ?{$_.EventID -eq 18264} | Select MachineName, Message
if ($sqlbkp_000)
{
$bkpsys | Add-Member -type NoteProperty -name Database -value "Success"
}
else
{
$bkpsys | Add-Member -type NoteProperty -Name Database -Value "Failed"
}
}
else
{
Write-Host "Server SCCMSQL01 is not reachable"
$bkpsys | Add-Member -type NoteProperty -name Database -value "Not Reachable"
}
$bkpstat
$params = @{'Head'="<title>Report for ConfigMgr Site 000</title>$style";
'PreContent'="<h1>Daily Check</h1>";
'PostContent'=$cmsvc_000, $cmdbsvc_000, $bkpstat}
Convertto-HTML @params | Out-File C:\Temp\Rajiv\Site_000.html
我得到的错误信息是:
ConvertTo-Html:无法将'System.Object []'转换为参数'PostContent'所需的'System.String'类型。规格 ified方法不受支持。 在C:\ temp \ rajiv \ DailyCheck_000.ps1:88 char:16 + Convertto-HTML&lt;&lt;&lt;&lt; @params | Out-File C:\ Temp \ Rajiv \ Site_000.html + CategoryInfo:InvalidArgument:(:) [ConvertTo-Html],ParameterBindingException + FullyQualifiedErrorId:CannotConvertArgument,Microsoft.PowerShell.Commands.ConvertToHtmlCommand
我尝试使用Out-String,使用和转换为$ bkp的Convertto-HTML,徒劳无功。请有人帮忙。
谢谢!
答案 0 :(得分:2)
当您将$bkpstat
定义为@()
时会出现问题,这会使其类型为Object[]
。这意味着当您将'PostContent'
设置为$cmsvc_000, $cmdbsvc_000, $bkpstat
(类型为String
,String
和Object[]
)时,所形成的数组必须为{ {1}}代替Object[]
,就像你期待的那样。
您需要先将String[]
转换为字符串,以生成cmdlet所期望的数组类型。