Báo cáo HTML PowerShell

TLDR; . Tôi sẽ chỉ ra cách sử dụng một số lệnh mà bạn có thể kết nối để thực hiện tất cả các công việc nặng nhọc cho bạn

Kịch bản - tạo báo cáo từ một số dữ liệu từ xa

Ok, bạn đây rồi, bạn đang muốn đọc dữ liệu từ một nơi và trình bày dữ liệu đó dưới dạng báo cáo web. Dữ liệu ở xa, bạn cần tìm nạp nó bằng cách nào đó, có lẽ bạn cũng cần suy nghĩ về cách chuyển đổi dữ liệu đến và cuối cùng là tạo báo cáo web đó. Nếu bạn là một nhà phát triển, bạn có thể nghĩ rằng oh ok, đây có lẽ là một vài phần chuyển động, 10-20 dòng mã. Nhưng bạn đã nghe nói về PowerShell, nó được cho là mạnh mẽ, thực hiện được nhiều công việc nặng nhọc, vậy tại sao bạn không thử dùng nó

Các bước chúng tôi sẽ thực hiện

Để đạt được nhiệm vụ của mình, chúng ta cần lên kế hoạch cho nó. Thực hiện tuần tự, và ai biết được, có thể đây là thứ chúng ta có thể tái sử dụng?

  1. Lấy dữ liệu. Cần lấy dữ liệu bằng cách nào đó
  2. Chuyển thành. Giả sử dữ liệu này có một số loại định dạng và rất có thể chúng ta cần chuyển đổi nó ở một số dạng khác
  3. Hiện nay. Vì vậy, chúng tôi đã tìm nạp dữ liệu, sắp xếp nó thành một định dạng phù hợp, bây giờ thì sao?

Được làm việc

Chúng tôi có một kế hoạch trò chơi. Bây giờ hãy xem liệu chúng ta có thể tìm thấy các lệnh chúng ta cần không. Một điểm khởi đầu tốt là Microsoft. PowerShell. phần tiện ích. Trong phần này, có rất nhiều lệnh thực hiện rất nhiều công việc nặng nhọc cho bạn

Lấy dữ liệu

Trước tiên, chúng ta cần lấy một số dữ liệu từ xa, vậy chúng ta có những lựa chọn nào?

  • Gọi-WebRequest, điều này dường như cho phép chúng tôi gọi một URL, gửi nội dung, thông tin đăng nhập, v.v., có vẻ đầy hứa hẹn
  • Gọi phương thức nghỉ ngơi. Còn cái này thì sao, nó khác nhau như thế nào?

PowerShell định dạng phản hồi dựa trên loại dữ liệu. Đối với nguồn cấp dữ liệu RSS hoặc ATOM, PowerShell trả về các nút Mục hoặc Mục nhập XML. Đối với Ký hiệu đối tượng JavaScript (JSON) hoặc XML, PowerShell chuyển đổi hoặc giải tuần tự hóa nội dung thành các đối tượng [PSCustomObject]

Nó nhận một phản hồi JSON và biến nó thành một PSCustomObject, thật tuyệt. Chà, hãy xem các lệnh khác của chúng tôi trước khi đưa ra quyết định

Trình bày dữ liệu

Đây là điều cuối cùng chúng ta cần làm nhưng chúng ta cần hiểu liệu có lệnh nào giúp chúng ta tạo báo cáo hay không và quan trọng nhất là đầu vào cần có. tôi nghĩ rằng chúng tôi đã tìm thấy nó

  • ConvertTo-Html Chuyển đổi. NET thành HTML có thể được hiển thị trong trình duyệt Web

Phải, điều đó nhắc nhở chúng ta về điều gì đó,

   
     xmlns="http://www.w3.org/1999/xhtml">
    
    </span>HTML TABLE<span>
    
    
nameheightmasshair_colorskin_coloreye_colorbirth_yeargenderhomeworldfilmsspeciesvehiclesstarshipscreatededitedurl
Luke Skywalker17277blondfairblue19BBYmalehttps://swapi.dev/api/planets/1/System.Object[]System.Object[]System.Object[]System.Object[]09/12/2014 13:50:5120/12/2014 21:17:56https://swapi.dev/api/people/1/
3. Tại sao? . e. đối tượng NET

Tuy nhiên, làm cách nào để chúng tôi lưu báo cáo vào một tệp để chúng tôi có thể lưu trữ báo cáo đó ở đâu đó và để báo cáo được lưu trữ trên máy chủ web?

Tổng quan về giải pháp

Vì vậy, chúng tôi có một lý thuyết về cách làm điều này

  1. Gọi
       
         xmlns="http://www.w3.org/1999/xhtml">
        
        </span>HTML TABLE<span>
        
        
    nameheightmasshair_colorskin_coloreye_colorbirth_yeargenderhomeworldfilmsspeciesvehiclesstarshipscreatededitedurl
    Luke Skywalker17277blondfairblue19BBYmalehttps://swapi.dev/api/planets/1/System.Object[]System.Object[]System.Object[]System.Object[]09/12/2014 13:50:5120/12/2014 21:17:56https://swapi.dev/api/people/1/
    3
  2. Theo sau bởi
       
         xmlns="http://www.w3.org/1999/xhtml">
        
        </span>HTML TABLE<span>
        
        
    nameheightmasshair_colorskin_coloreye_colorbirth_yeargenderhomeworldfilmsspeciesvehiclesstarshipscreatededitedurl
    Luke Skywalker17277blondfairblue19BBYmalehttps://swapi.dev/api/planets/1/System.Object[]System.Object[]System.Object[]System.Object[]09/12/2014 13:50:5120/12/2014 21:17:56https://swapi.dev/api/people/1/
    8
  3. Theo sau bởi
       
         xmlns="http://www.w3.org/1999/xhtml">
        
        </span>HTML TABLE<span>
        
        
    nameheightmasshair_colorskin_coloreye_colorbirth_yeargenderhomeworldfilmsspeciesvehiclesstarshipscreatededitedurl
    Luke Skywalker17277blondfairblue19BBYmalehttps://swapi.dev/api/planets/1/System.Object[]System.Object[]System.Object[]System.Object[]09/12/2014 13:50:5120/12/2014 21:17:56https://swapi.dev/api/people/1/
    5

Xây dựng giải pháp

Có vẻ như quá dễ dàng. Ah tốt, chúng ta hãy cho nó whirl. Trước tiên, hãy chọn một nguồn dữ liệu, SWAPI, API Chiến tranh giữa các vì sao, vì tôi sử dụng

   Invoke-Item ./report.htm
0, phải không?. )

  1. Bắt đầu
       Invoke-Item ./report.htm
    
    1 trong bảng điều khiển
  2. Tạo một tập tin web-báo cáo. ps1
  3. Thêm đoạn mã sau

   Invoke-RestMethod -URI https://swapi.dev/api/people/1/ |
     ConvertTo-Html |
       Out-File report.htm

Vào chế độ toàn màn hình Thoát chế độ toàn màn hình

  1. Chạy
       Invoke-Item ./report.htm
    
    2 (hoặc
       Invoke-Item ./report.htm
    
    3 cho người dùng Windows)

Nó tạo ra một báo cáo. htm cho chúng tôi. Ok, chúng ta hãy có một cái nhìn

   
     xmlns="http://www.w3.org/1999/xhtml">
    
    </span>HTML TABLE<span>
    
    
nameheightmasshair_colorskin_coloreye_colorbirth_yeargenderhomeworldfilmsspeciesvehiclesstarshipscreatededitedurl
Luke Skywalker17277blondfairblue19BBYmalehttps://swapi.dev/api/planets/1/System.Object[]System.Object[]System.Object[]System.Object[]09/12/2014 13:50:5120/12/2014 21:17:56https://swapi.dev/api/people/1/

Vào chế độ toàn màn hình Thoát chế độ toàn màn hình

  1. Hiển thị tệp này trong trình duyệt có
       Invoke-Item ./report.htm
    
    4

   Invoke-Item ./report.htm

Vào chế độ toàn màn hình Thoát chế độ toàn màn hình

Điều này sẽ khởi động một trình duyệt và bạn sẽ thấy một cái gì đó giống như

Báo cáo HTML PowerShell

Ok, bạn có thể hoàn thành ở đây hoặc chúng tôi có thể làm cho nó linh hoạt hơn. Chúng tôi không thích các giá trị được mã hóa cứng, phải không?

Làm cho nó linh hoạt

Tôi nghĩ vậy, bây giờ, hãy thêm một số tham số cho, URL và tên báo cáo

  1. Thêm mã sau vào phần trên cùng của báo cáo web tập lệnh của chúng tôi. ps1

   
     xmlns="http://www.w3.org/1999/xhtml">
    
    </span>HTML TABLE<span>
    
    
nameheightmasshair_colorskin_coloreye_colorbirth_yeargenderhomeworldfilmsspeciesvehiclesstarshipscreatededitedurl
Luke Skywalker17277blondfairblue19BBYmalehttps://swapi.dev/api/planets/1/System.Object[]System.Object[]System.Object[]System.Object[]09/12/2014 13:50:5120/12/2014 21:17:56https://swapi.dev/api/people/1/
5

Vào chế độ toàn màn hình Thoát chế độ toàn màn hình

  1. Hãy gọi nó một lần nữa, lần này với một URL mới "https. //hoán đổi. nhà phát triển/api/người/2/"

   
     xmlns="http://www.w3.org/1999/xhtml">
    
    </span>HTML TABLE<span>
    
    
nameheightmasshair_colorskin_coloreye_colorbirth_yeargenderhomeworldfilmsspeciesvehiclesstarshipscreatededitedurl
Luke Skywalker17277blondfairblue19BBYmalehttps://swapi.dev/api/planets/1/System.Object[]System.Object[]System.Object[]System.Object[]09/12/2014 13:50:5120/12/2014 21:17:56https://swapi.dev/api/people/1/
6

Vào chế độ toàn màn hình Thoát chế độ toàn màn hình

  1. Hãy kiểm tra phản hồi trong báo cáo. htm.

   
     xmlns="http://www.w3.org/1999/xhtml">
    
    </span>HTML TABLE<span>
    
    
nameheightmasshair_colorskin_coloreye_colorbirth_yeargenderhomeworldfilmsspeciesvehiclesstarshipscreatededitedurl
Luke Skywalker17277blondfairblue19BBYmalehttps://swapi.dev/api/planets/1/System.Object[]System.Object[]System.Object[]System.Object[]09/12/2014 13:50:5120/12/2014 21:17:56https://swapi.dev/api/people/1/
7

Vào chế độ toàn màn hình Thoát chế độ toàn màn hình

Lần này chúng ta có ________ 75, vâng, chắc chắn là không Thích, có vẻ như nó đang hoạt động. )

Cải thiện phản hồi

Vì vậy, cho đến nay chúng tôi đã có rất nhiều cột quay trở lại, có thể chúng tôi chỉ cần một vài trường từ phản hồi, chẳng hạn như

   Invoke-Item ./report.htm
6,
   Invoke-Item ./report.htm
7 và
   Invoke-Item ./report.htm
8. Vâng, hãy làm điều đó và chỉ cần chọn những gì chúng ta cần từ phản hồi

  1. Hãy thêm
       Invoke-Item ./report.htm
    
    9 như vậy

   
     xmlns="http://www.w3.org/1999/xhtml">
    
    </span>HTML TABLE<span>
    
    
nameheightmasshair_colorskin_coloreye_colorbirth_yeargenderhomeworldfilmsspeciesvehiclesstarshipscreatededitedurl
Luke Skywalker17277blondfairblue19BBYmalehttps://swapi.dev/api/planets/1/System.Object[]System.Object[]System.Object[]System.Object[]09/12/2014 13:50:5120/12/2014 21:17:56https://swapi.dev/api/people/1/
3

Vào chế độ toàn màn hình Thoát chế độ toàn màn hình

với mã đầy đủ trông như vậy

   
     xmlns="http://www.w3.org/1999/xhtml">
    
    </span>HTML TABLE<span>
    
    
nameheightmasshair_colorskin_coloreye_colorbirth_yeargenderhomeworldfilmsspeciesvehiclesstarshipscreatededitedurl
Luke Skywalker17277blondfairblue19BBYmalehttps://swapi.dev/api/planets/1/System.Object[]System.Object[]System.Object[]System.Object[]09/12/2014 13:50:5120/12/2014 21:17:56https://swapi.dev/api/people/1/
4

Vào chế độ toàn màn hình Thoát chế độ toàn màn hình

  1. Hãy gọi nó một lần nữa

   
     xmlns="http://www.w3.org/1999/xhtml">
    
    </span>HTML TABLE<span>
    
    
nameheightmasshair_colorskin_coloreye_colorbirth_yeargenderhomeworldfilmsspeciesvehiclesstarshipscreatededitedurl
Luke Skywalker17277blondfairblue19BBYmalehttps://swapi.dev/api/planets/1/System.Object[]System.Object[]System.Object[]System.Object[]09/12/2014 13:50:5120/12/2014 21:17:56https://swapi.dev/api/people/1/
5

Vào chế độ toàn màn hình Thoát chế độ toàn màn hình

và báo cáo của chúng tôi bây giờ trông giống như

   
     xmlns="http://www.w3.org/1999/xhtml">
    
    </span>HTML TABLE<span>
    
    
nameheightmasshair_colorskin_coloreye_colorbirth_yeargenderhomeworldfilmsspeciesvehiclesstarshipscreatededitedurl
Luke Skywalker17277blondfairblue19BBYmalehttps://swapi.dev/api/planets/1/System.Object[]System.Object[]System.Object[]System.Object[]09/12/2014 13:50:5120/12/2014 21:17:56https://swapi.dev/api/people/1/
6

Vào chế độ toàn màn hình Thoát chế độ toàn màn hình

Tốt hơn nhiều. ) Trên thực tế, đọc lên một chút, chúng ta chỉ có thể sử dụng -Property trên

   
     xmlns="http://www.w3.org/1999/xhtml">
    
    </span>HTML TABLE<span>
    
    
nameheightmasshair_colorskin_coloreye_colorbirth_yeargenderhomeworldfilmsspeciesvehiclesstarshipscreatededitedurl
Luke Skywalker17277blondfairblue19BBYmalehttps://swapi.dev/api/planets/1/System.Object[]System.Object[]System.Object[]System.Object[]09/12/2014 13:50:5120/12/2014 21:17:56https://swapi.dev/api/people/1/
8, vì vậy chúng ta nhận được

   
     xmlns="http://www.w3.org/1999/xhtml">
    
    </span>HTML TABLE<span>
    
    
nameheightmasshair_colorskin_coloreye_colorbirth_yeargenderhomeworldfilmsspeciesvehiclesstarshipscreatededitedurl
Luke Skywalker17277blondfairblue19BBYmalehttps://swapi.dev/api/planets/1/System.Object[]System.Object[]System.Object[]System.Object[]09/12/2014 13:50:5120/12/2014 21:17:56https://swapi.dev/api/people/1/
0

Vào chế độ toàn màn hình Thoát chế độ toàn màn hình

Làm cho nó đẹp

Thành thật mà nói, báo cáo này là xấu, không có màu sắc, không có gì. Chắc chắn là chúng ta có thể chuyển một tệp CSS tới

   
     xmlns="http://www.w3.org/1999/xhtml">
    
    </span>HTML TABLE<span>
    
    
nameheightmasshair_colorskin_coloreye_colorbirth_yeargenderhomeworldfilmsspeciesvehiclesstarshipscreatededitedurl
Luke Skywalker17277blondfairblue19BBYmalehttps://swapi.dev/api/planets/1/System.Object[]System.Object[]System.Object[]System.Object[]09/12/2014 13:50:5120/12/2014 21:17:56https://swapi.dev/api/people/1/
8?

À vâng, xem qua các tài liệu có tham số

   
     xmlns="http://www.w3.org/1999/xhtml">
    
    </span>HTML TABLE<span>
    
    
nameheightmasshair_colorskin_coloreye_colorbirth_yeargenderhomeworldfilmsspeciesvehiclesstarshipscreatededitedurl
Luke Skywalker17277blondfairblue19BBYmalehttps://swapi.dev/api/planets/1/System.Object[]System.Object[]System.Object[]System.Object[]09/12/2014 13:50:5120/12/2014 21:17:56https://swapi.dev/api/people/1/
52 có đường dẫn tệp. Hãy tạo một tệp CSS sau đó

  1. Tạo
       
         xmlns="http://www.w3.org/1999/xhtml">
        
        </span>HTML TABLE<span>
        
        
    nameheightmasshair_colorskin_coloreye_colorbirth_yeargenderhomeworldfilmsspeciesvehiclesstarshipscreatededitedurl
    Luke Skywalker17277blondfairblue19BBYmalehttps://swapi.dev/api/planets/1/System.Object[]System.Object[]System.Object[]System.Object[]09/12/2014 13:50:5120/12/2014 21:17:56https://swapi.dev/api/people/1/
    53 và thêm CSS sau

   
     xmlns="http://www.w3.org/1999/xhtml">
    
    </span>HTML TABLE<span>
    
    
nameheightmasshair_colorskin_coloreye_colorbirth_yeargenderhomeworldfilmsspeciesvehiclesstarshipscreatededitedurl
Luke Skywalker17277blondfairblue19BBYmalehttps://swapi.dev/api/planets/1/System.Object[]System.Object[]System.Object[]System.Object[]09/12/2014 13:50:5120/12/2014 21:17:56https://swapi.dev/api/people/1/
1

Vào chế độ toàn màn hình Thoát chế độ toàn màn hình

  1. Cập nhật tập lệnh để lấy
       
         xmlns="http://www.w3.org/1999/xhtml">
        
        </span>HTML TABLE<span>
        
        
    nameheightmasshair_colorskin_coloreye_colorbirth_yeargenderhomeworldfilmsspeciesvehiclesstarshipscreatededitedurl
    Luke Skywalker17277blondfairblue19BBYmalehttps://swapi.dev/api/planets/1/System.Object[]System.Object[]System.Object[]System.Object[]09/12/2014 13:50:5120/12/2014 21:17:56https://swapi.dev/api/people/1/
    54 trên
       
         xmlns="http://www.w3.org/1999/xhtml">
        
        </span>HTML TABLE<span>
        
        
    nameheightmasshair_colorskin_coloreye_colorbirth_yeargenderhomeworldfilmsspeciesvehiclesstarshipscreatededitedurl
    Luke Skywalker17277blondfairblue19BBYmalehttps://swapi.dev/api/planets/1/System.Object[]System.Object[]System.Object[]System.Object[]09/12/2014 13:50:5120/12/2014 21:17:56https://swapi.dev/api/people/1/
    8
  2. Chạy lại, bạn sẽ thấy điều này trong trình duyệt

Báo cáo HTML PowerShell

Tóm lược

Tóm lại, chúng tôi đã học được rằng chúng tôi có thể sử dụng chỉ một vài lệnh,

   
     xmlns="http://www.w3.org/1999/xhtml">
    
    </span>HTML TABLE<span>
    
    
nameheightmasshair_colorskin_coloreye_colorbirth_yeargenderhomeworldfilmsspeciesvehiclesstarshipscreatededitedurl
Luke Skywalker17277blondfairblue19BBYmalehttps://swapi.dev/api/planets/1/System.Object[]System.Object[]System.Object[]System.Object[]09/12/2014 13:50:5120/12/2014 21:17:56https://swapi.dev/api/people/1/
3,
   
     xmlns="http://www.w3.org/1999/xhtml">
    
    </span>HTML TABLE<span>
    
    
nameheightmasshair_colorskin_coloreye_colorbirth_yeargenderhomeworldfilmsspeciesvehiclesstarshipscreatededitedurl
Luke Skywalker17277blondfairblue19BBYmalehttps://swapi.dev/api/planets/1/System.Object[]System.Object[]System.Object[]System.Object[]09/12/2014 13:50:5120/12/2014 21:17:56https://swapi.dev/api/people/1/
8 và
   
     xmlns="http://www.w3.org/1999/xhtml">
    
    </span>HTML TABLE<span>
    
    
nameheightmasshair_colorskin_coloreye_colorbirth_yeargenderhomeworldfilmsspeciesvehiclesstarshipscreatededitedurl
Luke Skywalker17277blondfairblue19BBYmalehttps://swapi.dev/api/planets/1/System.Object[]System.Object[]System.Object[]System.Object[]09/12/2014 13:50:5120/12/2014 21:17:56https://swapi.dev/api/people/1/
5 và bùng nổ, chúng tôi đã tạo cho mình một báo cáo

Làm cách nào để hiển thị đầu ra PowerShell trong HTML?

PowerShell cung cấp một lệnh ghép ngắn tích hợp có tên là 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 tính năng 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 tệp HTML trong PowerShell?

Lệnh ghép ngắn Get-Content được sử dụng để đọc nội dung của tệp html.

Làm cách nào để sử dụng HTML trong PowerShell?

New-Item cmdlet được sử dụng để tạo một tệp html và Set-Content cmdlet để đưa nội dung vào đó. .
Trong ví dụ này, chúng tôi đang tạo một tệp html mới có tên test. html. .
Trong ví dụ này, chúng tôi đang thêm nội dung để kiểm tra. .
Trong ví dụ này, chúng tôi đang đọc nội dung của bài kiểm tra

Báo cáo HTML là gì?

Báo cáo HTML có thể được định nghĩa là sử dụng ngôn ngữ HTML để tạo báo cáo trên web hoặc báo cáo được tạo bởi trình tạo báo cáo HTML . Ngoài HTML, bạn cũng có thể nghe nói về HTML5. Đây là phiên bản mới nhất của lập trình HTML, cho phép nhiều loại dữ liệu hơn (e. g. video, âm thanh) được kết hợp.