PowerShell chuyển đổi từ HTML

PowerShell bao gồm một số khả năng tuyệt vời để làm việc với hai dạng dữ liệu có cấu trúc phổ biến. HTML và XML. Tại sao nó quan trọng? . Nếu bạn sử dụng PowerShell, các tệp trợ giúp, định dạng và loại là XML. Chức năng "cmdlet over objects" mới trong PowerShell v3 dựa trên XML. Chức năng liên quan đến HTML và XML không có bất kỳ thay đổi lớn nào trong PowerShell v3. Chúng tôi sẽ đề cập đến các khả năng khác nhau và cung cấp một số ví dụ ngắn gọn về cách bạn có thể muốn sử dụng chúng

Tạo một trang HTML từ một hoặc nhiều đối tượng PowerShell

Syntax  
      ConvertTo-Html [[-head] string[] ] [[-title] string] [[-body] string[] ]
         [-CssUri Uri] [[-property] Object[] ]
            [-As {TABLE | LIST}] [-inputObject psobject]
               [-PreContent string[]] [-PostContent string[]] [CommonParameters]
  
     ConvertTo-Html [-Fragment] [[-property] Object[] ]
            [-As {TABLE | LIST}] [-inputObject psobject]
               [-PreContent string[]] [-PostContent string[]] [CommonParameters]

Key
   -As string
Format the object as a table or a list. Valid values are TABLE or LIST. -CssUri Uri
The Uniform Resource Identifier [URI] of the cascading style sheet The CSS URI is included as a style sheet link in the output. -head string Text to include in the element of the HTML output. default = "HTML TABLE"
If you specify -Head, the -Title parameter is ignored. -title string Text to include in the element of the HTML output. -body string Text to include in the element of the HTML. -Fragment
Generate only an HTML table. The HTML, HEAD, TITLE, and BODY tags are omitted. -inputObject psobject The object[s] to represent as an HTML table. A variable that contains the object or a command/expression that gets the object. When the -InputObject parameter is used to submit a collection of items, such as all of the services on a computer, ConvertTo-Html receives one object that represents the collection. Because one object cannot be converted, ConvertTo-Html returns the entire collection unchanged. To convert multiple items, pipe them to ConvertTo-Html. This parameter is an implementation detail: its purpose is to enable input via the pipeline, and its direct use with arrays [collections] does not [yet] provide any useful functionality. -property Object Properties of the input object to appear in the HTML table. -PreContent string[]
Text to add before the opening tag. -PostContent string[]
Text to add after the closing
tag.

The object property names appear as HTML table column headings.

get-date | convertto-html

Save the system processes to C:\processes.html

PS C:\> Get-Process | ConvertTo-Html name,path,fileversion | Set-Content c:\processes.htm

Save the system services to C:\services.html

PS C:\> get-service | ConvertTo-Html -Title "Services" -Body "

The result of get-service

" -Property Name,Status  >  c:\services.html

Save the system services to C:\services.html and format in color [example from Hung Yuwu ]:

PS C:\> get-service | ConvertTo-Html -Title "Services" -Body "

The result of get-service

" -Property Name,Status |
foreach {if[$_ -like "*Running*"]{$_ -replace "", ""}elseif[$_ -like "*Stopped*"]{$_ -replace "", ""}else{$_}} > c:\services.html

Save the system services to C:\services.html and format with css, then open the HTML page with Invoke-Item:

PS C:\> get-service | ConvertTo-Html -CssUri "SS64.css" > c:\services.html
PS C:\> Invoke-Item c:\services.html

Get events from the "Windows PowerShell" event log, select only the ID, Level, and Task properties and format as HTML:

PS C:\> get-eventlog -log "Windows PowerShell" | convertto-html -property id, level, task

“If you have an important point to make, don’t try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack” ~ Winston Churchill

Related PowerShell Cmdlets:

export-clixml - Produce a clixml representation of a PowerShell objects.
export-csv - Export to Comma Separated Values [spreadsheet].
Invoke-Item - Invoke an executable or open a file.

Copyright © 1999-2022 SS64.com
Some rights reserved

Làm cách nào để tích hợp HTML với 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

Cách nào sau đây là cách chính xác để xuất dữ liệu PowerShell sang báo cáo HTML?

Để tạo báo cáo HTML bằng PowerShell, chúng ta có thể sử dụng lệnh ConvertTo-HTML . Ví dụ: giả sử chúng ta cần báo cáo các dịch vụ ở định dạng HTML, sau đó chúng ta có thể sử dụng ConvertTo-HTML làm đường dẫn.

Nền tảng nào được sử dụng cho PowerShell?

PowerShell là giải pháp tự động hóa tác vụ đa nền tảng được tạo thành từ trình bao dòng lệnh, ngôn ngữ tập lệnh và khung quản lý cấu hình. PowerShell chạy trên Windows, Linux và macOS .

Chủ Đề