PowerShell - 将邮箱统计信息导出为HTML报告

时间:2016-02-15 01:24:04

标签: powershell

我正在尝试生成一个系统报告,该报告可以提取网络主机的基本统计信息。我的脚本按预期工作,直到" o365ExchConn"功能,它管理所有" Import-Pssession"输出到用于在脚本末尾生成HTML片段的变量。

如何排除此输出?我只需要在函数中指定数组中指定的统计信息。 (即Displayname,TotalItemsize& TotalItemCount)。

    $computername = Read-Host -Prompt "Enter Hostname"
$path = Read-Host -Prompt "Enter path for HTML report output (e.g. C:\Temp)"
$date = Get-Date


function Get-CSInfo {

  param($computername)

  $os = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $computername

  $cs = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $computername

  $props = @{‘Hostname’=$computername
             ‘OS Version’=$os.version
             ‘OS Build’=$os.buildnumber
             ‘Service Pack’=$os.sevicepackmajorversion;
             'OS Name'=$os.caption}

  $obj = New-Object -TypeName PSObject -Property $props

  Write-Output $obj

  }

  function Get-RamInfo {
    $os = Get-WmiObject -class Win32_OperatingSystem -ComputerName $ComputerName
    $cs = Get-WmiObject -class Win32_ComputerSystem -ComputerName $ComputerName
    $props = @{'RAM (GB) - Total Available'="{0:N2}" -f ($cs.totalphysicalmemory / 1GB);
               'RAM (GB) - Free for Use'="{0:N2}" -f ($os.freephysicalmemory / 1MB);
               'RAM (GB) - Currently in Use'="{0:N2}" -f ($cs.totalphysicalmemory / 1GB) - ($os.freephysicalmemory / 1MB)}
    $obj = New-Object -TypeName PSObject -Property $props

    Write-Output $obj
}

function Get-InfoDisk {
param($computername)
    $drives = Get-WmiObject -class Win32_LogicalDisk -ComputerName $ComputerName -Filter "DriveType=3"
    foreach ($drive in $drives) {      
        $props = @{'Drive'=$drive.DeviceID;
                   'Size'=$drive.size / 1GB -as [int];
                   'Free'="{0:N2}" -f ($drive.freespace / 1GB);
                   'FreePct'=$drive.freespace / $drive.size * 100 -as [int]}
        $obj = New-Object -TypeName PSObject -Property $props
        Write-Output $obj
    }
}

function Get-InfoBadService {
    $svcs = Get-WmiObject -class Win32_Service -ComputerName $ComputerName -Filter "StartMode='Auto' AND State<>'Running'"
    foreach ($svc in $svcs) {
        $props = @{'ServiceName'=$svc.name;
                   'LogonAccount'=$svc.startname;
                   'DisplayName'=$svc.displayname}
       $obj = New-Object -TypeName PSObject -Property $props
       Write-Output $obj
    }}


function Get-EventLogs {
    $time = (Get-Date) - (New-TimeSpan -Day 1)
    $logs = Get-WinEvent -ComputerName $ComputerName -FilterHashTable @{LogName='Application', 'System'; Level=1,2,3; StartTime=$time}
        foreach ($log in $logs) {
        $props = @{'Provider Name'=$log.ProviderName;
                   'Timestamp'=$log.TimeCreated;
                   'Id'=$log.Id;
                   'Error Type'=$log.LevelDisplayName;
                   'Message'=$log.Message;
                   }

      $obj = New-Object -TypeName PSObject -Property $props
      Write-Output $obj   
}
}

function Get-SecurityLogs {
    $time = (Get-Date) - (New-TimeSpan -Day 1)
    $sec_logs = Get-EventLog -ComputerName $computername -LogName Security -EntryType FailureAudit -After $time
        foreach ($sec_log in $sec_logs) {
        $props = @{'Timestamp'=$sec_log.TimeGenerated;
                   'Event ID'=$sec_log.EventID;
                   'Error Type'=$sec_log.EntryType;
                   'Message'=$sec_log.Message;
                   }

      $obj = New-Object -TypeName PSObject -Property $props
      Write-Output $obj   
}
}

function O365ExchConn {
        $credentials = Get-Credential
        Write-Output "Creating remote PowerShell session ..."
        $session = New-PSSession -ConfigurationName Microsoft.Exchange -Credential $credentials -ConnectionUri https://outlook.office365.com/powershell-liveid?DelegatedOrg=*ENTERCOMPANYNAMETENANTNAME* -Authentication Basic -AllowRedirection

Write-Output "Importing PowerShell session ..."

Import-PSSession $session -AllowClobber
$userlist = Get-Mailbox | Get-MailboxStatistics | ft displayname, totalitemsize, itemcount
foreach($user in $userlist){
$props = @{'DisplayName'=$user.displayname;
           'Total Size'=$user.totalitemsize;
           'Total Items'=$user.itemcount;
           }

           $obj = New-Object -TypeName PSObject -Property $props
           Write-Output $obj

}
}


$frag1 = Get-CSInfo –computername $computername |

ConvertTo-Html -As LIST -Fragment -PreContent ‘<h2>System Info</h2>’ |
Out-String

$frag2 = Get-InfoDisk -ComputerName $computername |
ConvertTo-Html -As Table -Fragment -PreContent '<h2>Disk Info</h2>' |
Out-String

$frag3 = Get-RamInfo -ComputerName $computername |
ConvertTo-Html -As Table -Fragment -PreContent '<h2>Memory Info</h2>' |
Out-String

$frag4 = Get-InfoBadService -ComputerName $computername |
ConvertTo-Html -As Table -Fragment -PreContent '<h2>Stopped Services</h2>' |
Out-String

$frag5 = Get-EventLogs -ComputerName $computername |
ConvertTo-Html -As Table -Fragment -PreContent '<h2>Application / System Logs</h2>' |
Out-String

$frag6 = Get-SecurityLogs -ComputerName $computername |
ConvertTo-Html -As Table -Fragment -PreContent '<h2>Security Logs</h2>' |
Out-String

$frag7 = O365ExchConn -ComputerName $computername |
ConvertTo-Html -As List -Fragment -PreContent '<h2>Exchange Mailbox Stats</h2>' |
Out-String


$head = @’

<style>

body { background-color:#dddddd;

       font-family:Tahoma;

       font-size:12pt; }

td, th { border:1px solid black;

         border-collapse:collapse; }

th { color:white;

     background-color:black; }

table, tr, td, th { padding: 2px; margin: 0px }

table { margin-left:50px; }

</style>

‘@

$filepath = Join-Path -Path $path -ChildPath "$computername.html"

ConvertTo-HTML -head $head -PostContent $frag1,$frag2,$frag3,$frag4,$frag5,$frag6,$frag7 -PreContent “<h1>System Maintenance Report for $computername</h1><br><h2>Generated on $date” |

Out-File $filepath

1 个答案:

答案 0 :(得分:1)

PowerShell有多种方法可以阻止cmdlet在不需要的地方输出文本。

See this question for more information

[void] Import-PSSession $session -AllowClobber

Import-PSSession $session -AllowClobber | Out-Null

$ImportPSSession = Import-PSSession $session -AllowClobber