1

我正在尝试执行一系列步骤并创建一个 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”。不支持指定的方法。在 C:\temp\rajiv\DailyCheck_000.ps1:88 char:16 + Convertto-HTML <<<< @params | Out-File C:\Temp\Rajiv\Site_000.html + CategoryInfo : InvalidArgument: (:) [ConvertTo-Html], ParameterBindingException + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.ConvertToHtmlCommand

我曾尝试使用 Out-String,无论是否使用 Convertto-HTML for $bkp,但都是徒劳的。有人可以帮忙吗?

谢谢!

4

1 回答 1

2

当您定义$bkpstatas时,问题就开始了@(),这使它成为 type Object[]。这意味着当您设置'PostContent'$cmsvc_000, $cmdbsvc_000, $bkpstat(类型为StringStringObject[])时,形成的数组必须是类型,Object[]而不是String[]您期望的类型。

您需要首先转换$bkpstat为字符串以生成 cmdlet 期望的数组类型。

于 2013-01-17T09:13:51.550 回答