我需要一些关于我正在整理的PowerShell脚本的报告输出的帮助。该脚本从计算机获取有关以下项目的信息。
上次重启 正常运行时间 待重启 补丁信息
我遇到的问题是“修补信息”输出。我想在此标题下返回的信息是“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
答案 0 :(得分:1)
您正在尝试为Updates分配一个数组值,因为在Powershell V3中$ arrayOfObject.propertyName输出该数组中每个元素的属性值。该功能称为成员枚举。这就是为什么你得到的格式不理想。
为什么不为“更新”字段分配值$ updates例如:
$newobj | add-member -membertype noteproperty -name "Updates" -value $Updates
它将包含一个对象数组,每个对象都有属性hotfixid,description,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>"
})
"@