ConvertTo-Html -Fragment

Để lấy thông tin đĩa ở định dạng HTML trong PowerShell, giả sử chúng ta đã có tập lệnh để xuất thông tin đĩa

$dtype = @{
    2 = "Removable disk"
    3=  "Fixed local disk"
    4=  "Network disk"
    5 = "CD/DVD disk"}

Get-CimInstance Win32_LogicalDisk |  Select DeviceID, 
        @{N="Total Size(GB)";E={[math]::Round(($_.Size/1GB),2)}}, 
        @{N="Free Space(GB)";E={[math]::Round(($_.FreeSpace/1GB),2)}},
        @{N="DiskType"; E={$dtype.Item([int]$_.DriveType)}} | Ft –auto
ConvertTo-Html -Fragment

Bây giờ để có được đầu ra ở định dạng HTML, bạn cần sử dụng lệnh ConvertTo-HTML. Lệnh của chúng ta sẽ giống như

Get-CimInstance Win32_LogicalDisk |  Select DeviceID, 
        @{N="Total Size(GB)";E={[math]::Round(($_.Size/1GB),2)}}, 
        @{N="Free Space(GB)";E={[math]::Round(($_.FreeSpace/1GB),2)}},
        @{N="DiskType"; E={$dtype.Item([int]$_.DriveType)}} | 
  ConvertTo-Html | Out-File -FilePath C:\diskoutput.html

Đầu ra được lưu vào Diskoutput. html trong ổ C và khi bạn mở tệp đầu ra, nó sẽ giống như. Bạn cũng có thể mở tệp đầu ra bằng lệnh bên dưới

Invoke-Expression C:\diskoutput.html
ConvertTo-Html -Fragment
Đầu ra

Đầu ra này ở định dạng đơn giản, bạn có thể thêm kiểu CSS để sửa đổi đầu ra HTML. Dưới đây là ví dụ về đầu ra HTML kiểu CSS

Một tính năng tiết kiệm thời gian tuyệt vời là khả năng dọn dẹp HTML hiện có. Ví dụ: giả sử bạn đang chỉnh sửa một trang trong WordPress hoặc một hệ thống quản lý nội dung khác, có thể hữu ích nếu bạn có thể xóa định dạng mà bạn không cần.  

Để dọn dẹp HTML của bạn, tất cả những gì bạn cần làm là dán nó vào hộp soạn thảo HTML ở bên phải. Khi bạn dán mã vào, bạn sẽ nhận được bản xem trước giao diện của mã trong Trình chỉnh sửa trực quan ở bên trái. Sau đó, bạn có thể trực tiếp chỉnh sửa HTML của mình trong Trình chỉnh sửa HTML ở bên phải hoặc sử dụng Trình chỉnh sửa trực quan ở bên trái để thực hiện các thay đổi của mình

Để dọn dẹp HTML của bạn, bạn có thể sử dụng các tùy chọn Dọn dẹp HTML. Bạn cũng có thể thêm, tìm và thay thế các lệnh để thay đổi mã của mình; . Sau khi tất cả các tùy chọn dọn dẹp được chọn, hãy nhấp vào nút Dọn dẹp HTML của tôi màu xanh lá cây. Sau đó, bạn có thể sao chép HTML đã chuyển đổi vào khay nhớ tạm hoặc lưu và tải xuống dưới dạng HTML

Nếu bạn mắc lỗi, bạn có thể nhấp vào nút hoàn tác bên dưới Trình chỉnh sửa HTML để hoàn tác các thay đổi của mình - điều này hữu ích để kiểm tra xem tổ hợp cài đặt nào hoạt động tốt nhất

Tôi có một tập lệnh để tạo báo cáo html. Tôi cần tạo định dạng có điều kiện cho một hàng nếu nó lớn hơn 250. Hàng nó cần nhắm mục tiêu là lập chỉ mục, tôi đã thử một vài thứ nhưng dường như tôi không hiểu nó hoạt động như thế nào

Tập lệnh Powershell

PowerShell

###################################################
# create variables
###################################################
$obj = @()
$date = Get-Date
$hostname = $Env:COMPUTERNAME
$servertype = ""
# $runcommand = diagevent blocking

######################################################
# check the hostname to see if it is production or dev
######################################################
if ($hostname -eq 'HQ1635') { $servertype = "Production Server" }
else { $servertype = "Development" }


##########################################################################
# Set working directory - below will need to be un-commented in production
##########################################################################
#Set-Location -Path 'C:\Xerox\Docushare\bin'

###################################
# get the information from the file 
###################################
$search1 = (Get-Content C:\test\diagevent_out.txt)

###################
# clean up the file
###################
foreach($line in $search1){
    # cleaning
    $nline = $line.Split(",") #-replace """",""
    # select whats needed
    $properties = @{
        'process' = $nline[0]
        'remaining' = $nline[2]
    }
    # create varible for output
    $obj += New-Object PSObject -Property $properties 
}

###################################################################################
# more dynamic search, search for the line, then see how many handles indexingQ has
###################################################################################
# variable to search for - this can be changed to any process on the server, just put the title - leave spaces before and after
$mystring = " indexingQ "
# search for the string in the log file
$results2 = Select-String -Path "C:\test\diagevent_out.txt" -Pattern "$mystring"
$results3 = ($results2 -split " ")[7] 
$results4 = ($results3 -split ",")[0]
###########################end search 1 ###############

########################################################################################
#cosmetics for dynamic form - here you can toggle the threshold higher or lower then 249   
########################################################################################
 $report1= if ($results4 -ile '249') { "

indexingQ is stable
" , "$mystring = $results4 handles

"
} else { "

indexingQ is unstable
" , "$mystring = $results4 handles

"
} ###################### # create fancy table ###################### $Header = @" "@ $Body = @"

Docushare Process Report

Todays Date is:
$date

Computer Name:
$hostname

Server Type:
$servertype

Status: $report1

"@ $footer = @"
"@ ##################################### # create the report and attach style ##################################### $output = $obj | ConvertTo-Html -property Process, Remaining -Head $Header -Body $Body $output | Out-File -FilePath 'C:\test\diagevent_out(clean).html'


Tập tin văn bản nó đọc

Chữ

C:\Xerox\Docushare\bin>diagevent blocking
Running diagevent blocking
Queue DSServer.9 is holding this many handles: 0, is limited to a maximum of 1000 events, and has this far to go: 1000
Queue indexingQ2 is holding this many handles: 0, is limited to a maximum of 1000 events, and has this far to go: 1000
Queue DSServer.6 is holding this many handles: 0, is limited to a maximum of 1000 events, and has this far to go: 1000
Queue DSServer.5 is holding this many handles: 0, is limited to a maximum of 1000 events, and has this far to go: 1000
Queue DSServer.4 is holding this many handles: 0, is limited to a maximum of 1000 events, and has this far to go: 1000
Queue DSServer.2 is holding this many handles: 0, is limited to a maximum of 1000 events, and has this far to go: 1000
Queue DSServer.1 is holding this many handles: 0, is limited to a maximum of 1000 events, and has this far to go: 1000
Queue RoutingUsernameChanged.17 is holding this many handles: 0, is limited to a maximum of 1000 events, and has this far to go: 1000
Queue DSServer.110 is holding this many handles: 0, is limited to a maximum of 1000 events, and has this far to go: 1000
Queue dayQ is holding this many handles: 0, is limited to a maximum of 1000 events, and has this far to go: 1000
Queue MailAgent.13 is holding this many handles: 0, is limited to a maximum of 1000 events, and has this far to go: 1000
Queue AutoClassifyService is holding this many handles: 0, is limited to a maximum of 1000 events, and has this far to go: 1000
Queue subscriptionQ is holding this many handles: 0, is limited to a maximum of 1000 events, and has this far to go: 1000
Queue DSServer.10 is holding this many handles: 0, is limited to a maximum of 1000 events, and has this far to go: 1000
Queue weekQ is holding this many handles: 0, is limited to a maximum of 1000 events, and has this far to go: 1000
Queue RoutingChangeUser.15 is holding this many handles: 0, is limited to a maximum of 1000 events, and has this far to go: 1000
Queue indexingQ is holding this many handles: 249, is limited to a maximum of 1000 events, and has this far to go: 1000
Queue DSServer.12 is holding this many handles: 0, is limited to a maximum of 1000 events, and has this far to go: 1000
Queue DSServer.11 is holding this many handles: 0, is limited to a maximum of 1000 events, and has this far to go: 1000
Queue RoutingReadDateFormat.14 is holding this many handles: 0, is limited to a maximum of 1000 events, and has this far to go: 1000
Queue Amber-schemaChange.7 is holding this many handles: 0, is limited to a maximum of 1000 events, and has this far to go: 1000
Queue Subscription.8 is holding this many handles: 0, is limited to a maximum of 1000 events, and has this far to go: 1000
Queue MailAgent.133 is holding this many handles: 0, is limited to a maximum of 1000 events, and has this far to go: 1000
Queue WebDAV.18 is holding this many handles: 0, is limited to a maximum of 1000 events, and has this far to go: 1000
Queue DSServer.163 is holding this many handles: 0, is limited to a maximum of 1000 events, and has this far to go: 1000
Queue RoutingLinkChanged.16 is holding this many handles: 0, is limited to a maximum of 1000 events, and has this far to go: 1000
Queue VerityConversion.3 is holding this many handles: 0, is limited to a maximum of 1000 events, and has this far to go: 1000
Queue DSServer.109 is holding this many handles: 0, is limited to a maximum of 1000 events, and has this far to go: 1000And the report it generates 

tệp đính kèm Tệp đính kèm diagevent_out. txt 3. 47KB

ConvertTo là gì

Cmdlet ConvertTo-Html chuyển đổi. Các đối tượng NET thành HTML có thể được hiển thị trong trình duyệt Web . Bạn có thể sử dụng lệnh ghép ngắn này để hiển thị đầu ra của một lệnh trong trang Web.

Làm cách nào để chuyển đổi tập lệnh PowerShell sang HTML?

PowerShell cung cấp lệnh ghép ngắn tích hợp có tên ConvertTo-Html . Điều này lấy các đối tượng làm đầu vào và chuyển đổi từng đối tượng thành trang web HTML. Để sử dụng điều này, chỉ cần lấy đầu ra và chuyển trực tiếp sang ConvertTo-Html. Lệnh ghép ngắn sau đó sẽ trả về một chuỗi HTML lớn.

Làm cách nào để có được đầu ra PowerShell trong HTML?

Ví dụ: giả sử chúng tôi cần báo cáo các dịch vụ ở định dạng HTML, sau đó chúng tôi có thể sử dụng ConvertTo-HTML làm đường dẫn . Lệnh đầu tiên sẽ lấy đầu ra trong tệp HTML và lệnh thứ hai (ii) là bí danh của lệnh Invoke-Item.

Làm cách nào để tạo bảng HTML trong PowerShell?

Cách tạo báo cáo HTML bằng PowerShell .
Xuất báo cáo thành tệp HTML
Kết hợp các báo cáo bằng cách sử dụng tham số phân đoạn
Thêm nhãn bằng thông số PreContent và PostContent
Thay đổi bố cục bảng bằng cách sử dụng làm tham số
Nâng cao Báo cáo bằng CSS
Sử dụng HTML Id và thuộc tính lớp trong CSS