2013-11-22 5 views
-1

Мне нужна помощь по выводу отчета для сценария PowerShell, который я собираю. Сценарий получает информацию с компьютера по следующим элементам.Формат вывода отчета HTML PowerShell

Последний Rebooted по Uptime Pending Reboot Patching Информация

Проблема у меня есть с "латания Информация" продукции. Информация, которую я хочу вернуть под этим заголовком, - «Hotfixid», «description», «installedby», «installedon»

Если я укажу одно значение, например «Updates.hotfixid», я получаю результаты, но если я попытаюсь добавить другие я возвращаю System.Object [] System.Object [].

Также вывод под этим заголовком обертывается, но я бы хотел, чтобы он перечислил.

Ниже приведен сценарий

$comps = Get-Content 'c:\temp\comp.txt' 

    $total = $null 

    $html = @' 
    <html> 
    <head> 
    <meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'> 
    <title> Server Maintenance Report</title> 
    <STYLE TYPE="text/css"> 
    <!-- 
    td { 
    font-family: Tahoma; 
    font-size: 13px; 
    border-top: 1px solid #999999; 
    border-right: 1px solid #999999; 
    border-bottom: 1px solid #999999; 
    border-left: 1px solid #999999; 
    padding-top: 0px; 
    padding-right: 0px; 
    padding-bottom: 0px; 
    padding-left: 0px; 
    } 
    body { 
    margin-left: 5px; 
    margin-top: 5px; 
    margin-right: 0px; 
    margin-bottom: 10px; 

    table { 
    border: thin solid #000000; 
    } 
    --> 
    </style> 
    </head> 
    <body> 
    <table width='100%'> 
    <tr bgcolor='#CCCCCC'> 
    <td colspan='7' height='25' align='center'><strong><font color="#003399" size="4" face="tahoma"> Report </font></strong></td> 
    </tr> 
    </table> 
    <table width='100%'><tbody> 
      <tr bgcolor=#CCCCCC> 
      <td width='14%' height='15' align='center'> <strong> <font color="#003399" size="2" face="tahoma" >Server Name</font></strong></td> 
      <td width='14%' height='15' align='center'> <strong> <font color="#003399" size="2" face="tahoma" >Last Rebooted By</font></strong></td> 
      <td width='16%' height='15' align='center'> <strong> <font color="#003399" size="2" face="tahoma" >Uptime</font></strong></td> 
      <td width='8%' height='15' align='center'> <strong> <font color="#003399" size="2" face="tahoma" >Pending Reboot</font></strong></td> 
      <td width='50%' height='15' align='center'> <strong> <font color="#003399" size="2" face="tahoma" >Patching Information</font></strong></td> 
      </tr>" 
    </table> 
    <table width='100%'><tbody> 
    '@ 

    Function Uptime($comp){ 
      function WMIDateStringToDate($Bootup) { 
      [System.Management.ManagementDateTimeconverter]::ToDateTime($Bootup) 
      } 
      $NameSpace = "Root\CIMV2" 
      $wmi = [WMISearcher]"" 
      #$wmi.options.timeout = '0:0:30' #set timeout to 30 seconds 
      $query = 'Select * from Win32_OperatingSystem' 
      $wmi.scope.path = "\\$comp\$NameSpace" 

      $wmi.query = $query 
      Try{ 
       $wmiresult = $wmi.Get() 
       foreach ($wmioutput in $wmiresult){ 
        $Bootup = $wmioutput.LastBootUpTime 
        $LastBootUpTime = WMIDateStringToDate($Bootup) 
        $now = Get-Date 
        $Reporttime = $now - $lastBootUpTime 
        $d = $Reporttime.Days 
        $h = $Reporttime.Hours 
        $m = $Reporttime.Minutes 
        $time = "Up for: {0} days, {1} hours, {2:N0} minutes" -f $d,$h,$m 
        return $time 
       } 
      } 
      Catch [Exception] { 
       $uperr = '<font color="#FF0000"> RPC Issue : </font>'+ $_ 
       return $uperr 
      } 
     } 

    Function RebootedBy($comp){ 
      try { 
      gwmi win32_ntlogevent -filter "LogFile='System' and EventCode='1074' and Message like '%restart%'" -ComputerName $comp | 
      Where {$_.ConvertToDateTime($_.TimeGenerated) -gt $lastBootUpTime} | 
      select User,@{n="Time";e={$_.ConvertToDateTime($_.TimeGenerated)}} -First 1 
      } 
      Catch [Exception] { 
       $uperr = '<font color="#FF0000"> RPC Issue : </font>'+ $_ 
       return $uperr 
      } 
     } 


    Function Updates ($comp){ 
      try { 
      $date = Get-Date '26/09/2013' 
      (Get-HotFix -ComputerName $comp| where-object {$_.hotfixid -ne "file 1"} | where "InstalledOn" -gt $date | Select hotfixid,description,installedby,@{l="InstalledOn";e={[DateTime]::Parse($_.psbase.properties["installedon"].value,$([System.Globalization.CultureInfo]::GetCultureInfo("en-US")))}}) 
      } 
      Catch [Exception] { 
       $uperr = '<font color="#FF0000"> RPC Issue : </font>'+ $_ 
       return $uperr 
      } 
     } 


    function PendingReboot ($comp){ 
     process { 
      try { 
       $WMI_OS = "" 
       $RegCon = "" 
       $WMI_OS = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $comp -ErrorAction Stop 
       if ($?){ 
       try{ 
        $RegCon = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]"LocalMachine",$comp) 
        If ($WMI_OS.BuildNumber -ge 6001){ 
         $RegValueSetupex = "" 
         $RegValuePFRO2k8 = "" 
         $RegSubKeySM = $RegCon.OpenSubKey("SYSTEM\CurrentControlSet\Control\Session Manager\") 
         $RegValueSetupex = $RegSubKeySM.GetValue("SetupExecute",$null) 
         if ($RegValueSetupex){ 
          $RegValueSetupex = $true 
         } 

         $RegSubKeySM = $RegCon.OpenSubKey("SYSTEM\CurrentControlSet\Control\Session Manager\") 
         $RegValuePFRO2k8 = $RegSubKeySM.GetValue("PendingFileRenameOperations",$null) 
         if ($RegValuePFRO2k8){ 
          $RegValuePFRO2k8 = $true 
         } 

         $RegCon.Close() 

         if ($RegValueSetupex -eq $true -or $RegValuePFRO2k8 -eq $true){ 
          return '<font color="#FF0000">'+$true 
         } 
         else { 
          return $false       
         } 
        } 
        else{ 
         $RegValuePFRO2k3 = $false; 
         $RegCon = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]"LocalMachine","$comp") 
         $RegSubKeySM = $RegCon.OpenSubKey("SYSTEM\CurrentControlSet\Control\Session Manager\") 
         $RegValuePFRO2k3 = $RegSubKeySM.GetValue("PendingFileRenameOperations",$null) 
         $RegCon.Close() 
         If ($RegValuePFRO2k3) { 
          return '<font color="#FFFF00">'+$true; 
         } 
         else { 
          return $false; 
         } 
        } 

       } 
       catch { 
        return '<font color="#7CFC00">'+"Remote Registry Service OK" 
       } 
       } 
       else { 
        throw $error[0].Exception 
       } 
      } 
      catch { 
        return '<font color="#FF0000">'+"RPC Issue"   
      } 
     } 
    } 


    $i=0 # for Progress bar 

    foreach($comp in $comps){ 
     $i++ 
     $ErrorActionPreference = "SilentlyContinue" 
     Write-Progress -Activity "Report v1.0" -Status ("Checking : {0}" -f $comp) -PercentComplete ($i/$comps.count*100) -Id 0 
     $ErrorActionPreference = "Continue" 
     #region Var_Nulling :p 
     $Reporttimestatus = $null 

     #endregion 
     $Reporttimestatus = uptime -comp $comp 
     $RebootedBy = RebootedBy $comp 
     $Pending = PendingReboot -comp $comp 
     $Updates = Updates -comp $comp 

     $newobj = $null 
     $newobj = new-object psobject 
     $newobj | add-member -membertype noteproperty -name "Server" -value $comp 
     $newobj | add-member -membertype noteproperty -name "Uptime" -value $Reporttimestatus 
     $newobj | add-member -membertype noteproperty -name "RebootedBy" -value $RebootedBy.User , $RebootedBy.Time 
     $newobj | add-member -membertype noteproperty -name "PendingReboot" -value $Pending 
     $newobj | add-member -membertype noteproperty -name "Updates" -value $Updates.hotfixid 
     #, $Updates.description , $Updates.installedby , $Updates.InstalledOn 

     $htmlserver = $newobj.Server 
     $htmluptime = $newobj.Uptime 
     $htmlrebootedby = $newobj.RebootedBy 
     $htmlpendingreboot = $newobj.PendingReboot 
     $htmlupdates = $newobj.Updates 

    $current = " 
      <tr bgcolor=#CCCCCC> 
      <td width='14%' align='center'>$htmlserver</td> 
      <td width='14%' align='center'>$htmlrebootedby</td> 
      <td width='16%' align='center'>$htmluptime</td> 
      <td width='8%' align='center'>$htmlpendingreboot</td> 
      <td width='50%' align='left'>$htmlupdates</td> 
      </tr> " 

     $total += $current 

    } 

    $HTMLEnd = @" 
    </div> 
    </body> 
    </html> 
    "@ 

    $MainHtml= $html + $total + $HTMLEnd 
    $MainHtml | Out-File "c:\temp\Report.html" -Append 
+1

-1. Не просто сбрасывайте тонны кода и просите людей исправить его для вас. Создайте минимальный фрагмент, который воспроизводит конкретную проблему, с которой вы сталкиваетесь, с максимально возможным количеством внешних зависимостей. Это хорошее упражнение в целом, и в этом процессе вы можете обнаружить разрешение самостоятельно. – latkin

ответ

1

Вы пытаетесь присвоить Обновляет массив значения массивов, так как в Powershell V3 $ arrayOfObject.propertyName выводит значение свойства для каждого элемента в этом массиве. Эта функция называется перечислением участников. Вот почему форматирование, которое вы получаете, не является идеальным.

Почему вы не назначить поле «Обновления» значение $ обновления т.д .:

$newobj | add-member -membertype noteproperty -name "Updates" -value $Updates 

, который будет содержать массив объектов, каждый из которых будет иметь свойства hotfixid, описание, installeby, installedOn. Затем в вашем $current фрагменте HTML я буду перебирать каждое обновление и выплюнуть каждое отдельное поле. Возможно, таблица внутри таблицы.

$current = @" 
     <tr bgcolor=#CCCCCC> 
     <td width='14%' align='center'>$htmlserver</td> 
     <td width='14%' align='center'>$htmlrebootedby</td> 
     <td width='16%' align='center'>$htmluptime</td> 
     <td width='8%' align='center'>$htmlpendingreboot</td> 
     </tr> 
     $(foreach ($update in $updates) { 
      "<tr><td>$($update.hotfixid)</td>" 
      "<td>$($update.description)</td>" 
      "<td>$($update.installedby)</td>" 
      "<td>$($update.installedon)</td></tr>" 
     }) 
"@ 
+0

Спасибо ... добавление -value $ Обновления были тем, о чем я говорил. – user1890242

Смежные вопросы