Hướng dẫn powershell html report - báo cáo html powershell

PowerShell là một công cụ mạnh mẽ giúp bạn đơn giản hóa và tự động hóa các nhiệm vụ tẻ nhạt và lặp đi lặp lại. Quản trị viên thường sử dụng PowerShell để trích xuất thông tin từ hệ thống hoặc cơ sở dữ liệu khi bộ dữ liệu cần phải được trình bày dưới dạng báo cáo. Nhưng các báo cáo trong các tệp văn bản hoặc CSV thường thiếu kiểu dáng và kiểu dáng lạ mắt của HTML. Trong hướng dẫn này, bạn sẽ học cách tạo báo cáo NN HTML với PowerShell,

Show

Không phải là một người đọc? Xem video hướng dẫn liên quan này!

Không xem video? Hãy chắc chắn rằng trình chặn quảng cáo của bạn bị vô hiệu hóa.

PowerShell có thể giúp bạn xây dựng các báo cáo HTML và tránh xa các định dạng nhạt nhẽo khác này. Chỉ sử dụng PowerShell, bạn có thể làm cho một báo cáo trở nên sống động với HTML cho phép bạn trình bày có ý nghĩa về dữ liệu thô được trích xuất từ ​​hệ thống. Bạn thậm chí có thể áp dụng các bảng kiểu xếp tầng (CSS) để dễ dàng tùy chỉnh bố cục báo cáo của bạn.

Trong bài viết này, bạn sẽ tìm hiểu cách sử dụng

Get-CimInstance -Class Win32_OperatingSystem | Select-object Version,Caption,BuildNumber,Manufacturer |  ConvertTo-Html
6 kết hợp với
Get-CimInstance -Class Win32_OperatingSystem | Select-object Version,Caption,BuildNumber,Manufacturer |  ConvertTo-Html
7 CMDLET để tạo báo cáo HTML. Bạn cũng sẽ tìm hiểu kịch bản cơ bản cho CSS và làm thế nào nó có thể hữu ích trong việc định dạng thiết kế báo cáo dựa trên HTML của bạn.

  • Điều kiện tiên quyết
  • Tạo báo cáo thông tin máy tính cơ bản bằng cách sử dụng ConvertTo-HTML
    • Xuất báo cáo vào tệp HTML
    • Kết hợp các báo cáo bằng tham số phân đoạn
    • Thêm nhãn bằng tham số Preontent và Postcontent
    • Thay đổi bố cục bảng bằng tham số
    • Tăng cường báo cáo bằng CSS
    • Sử dụng ID HTML và các thuộc tính lớp trong CSS
  • Sự kết luận
  • Đọc thêm

Điều kiện tiên quyết

Tạo báo cáo thông tin máy tính cơ bản bằng cách sử dụng ConvertTo-HTML

  • Xuất báo cáo vào tệp HTML
  • Kết hợp các báo cáo bằng tham số phân đoạn
  • Thêm nhãn bằng tham số Preontent và Postcontent

Thay đổi bố cục bảng bằng tham số

Tăng cường báo cáo bằng CSS

Sử dụng ID HTML và các thuộc tính lớp trong CSS

Get-CimInstance -Class Win32_OperatingSystem | Select-object Version,Caption,BuildNumber,Manufacturer

Sự kết luận

Hướng dẫn powershell html report - báo cáo html powershell
Đọc thêm

Bài viết này sẽ là một hướng dẫn. Nếu bạn có ý định theo dõi, hãy chắc chắn rằng bạn có các điều kiện tiên quyết sau đây được thiết lập trước thời hạn.

Get-CimInstance -Class Win32_OperatingSystem | Select-object Version,Caption,BuildNumber,Manufacturer |  ConvertTo-Html

Tất cả các ví dụ sẽ được hiển thị bằng Windows 10 Build 1709, nhưng phiên bản này không được yêu cầu

Hướng dẫn powershell html report - báo cáo html powershell
Windows PowerShell 5.1 hoặc sau đó phiên bản hoặc PowerShell Core 7

Xuất báo cáo vào tệp HTML

Kết hợp các báo cáo bằng tham số phân đoạn

Thêm nhãn bằng tham số Preontent và Postcontent

Get-CimInstance -Class Win32_OperatingSystem | Select-object Version,Caption,BuildNumber,Manufacturer | ConvertTo-Html | Out-File -FilePath .\Basic-Computer-Information-Report.html

Thay đổi bố cục bảng bằng tham số

.\Generate-HTML-Report.Ps1

Tăng cường báo cáo bằng CSS

Hướng dẫn powershell html report - báo cáo html powershell
Sử dụng ID HTML và các thuộc tính lớp trong CSS

Sự kết luận

Đọc thêm

Mỗi lệnh sẽ trả về thông tin máy tính khác nhau được định dạng là & NBSP; mã HTML. Để hợp nhất đúng thông tin trong một báo cáo HTML duy nhất, hãy sử dụng tham số

Get-CimInstance -Class Win32_OperatingSystem | Select-object Version,Caption,BuildNumber,Manufacturer | ConvertTo-Html | Out-File -FilePath .\Basic-Computer-Information-Report.html
8 để chỉ nhận phần bảng của mã HTML được tạo bởi
Get-CimInstance -Class Win32_OperatingSystem | Select-object Version,Caption,BuildNumber,Manufacturer |  ConvertTo-Html
8 CMDlet.

Như bạn có thể thấy trong ảnh chụp màn hình bên dưới, PowerShell tạo ra tất cả các phần tử HTML cơ bản khi đầu ra được điều khiển đến

Get-CimInstance -Class Win32_OperatingSystem | Select-object Version,Caption,BuildNumber,Manufacturer |  ConvertTo-Html
8 CMDLET.

Hướng dẫn powershell html report - báo cáo html powershell
Danh sách mã HTML được tạo bằng cách sử dụng cmdlet
Get-CimInstance -Class Win32_OperatingSystem | Select-object Version,Caption,BuildNumber,Manufacturer |  ConvertTo-Html
8

Khi bạn sử dụng tham số

Get-CimInstance -Class Win32_OperatingSystem | Select-object Version,Caption,BuildNumber,Manufacturer | ConvertTo-Html | Out-File -FilePath .\Basic-Computer-Information-Report.html
8, PowerShell chỉ tạo phần tử bảng HTML. Các yếu tố
.\Generate-HTML-Report.Ps1
4,
.\Generate-HTML-Report.Ps1
5,
.\Generate-HTML-Report.Ps1
6,
.\Generate-HTML-Report.Ps1
7 và các yếu tố khác bị bỏ qua. & nbsp; kết quả được hiển thị dưới đây.

Hướng dẫn powershell html report - báo cáo html powershell
Danh sách mã HTML cho bảng bằng tham số phân đoạn

Bây giờ bạn đã biết tham số

Get-CimInstance -Class Win32_OperatingSystem | Select-object Version,Caption,BuildNumber,Manufacturer | ConvertTo-Html | Out-File -FilePath .\Basic-Computer-Information-Report.html
8 hoạt động như thế nào từ ví dụ trên, hãy để áp dụng nó trong tập lệnh.

Các lệnh trong tập lệnh bên dưới thực hiện các mục sau:

  • Năm dòng lệnh đầu tiên nhận được thông tin khác nhau từ máy như hệ điều hành, bộ xử lý, bios, đĩa và dịch vụ.
  • Lọc kết quả bằng tham số
    .\Generate-HTML-Report.Ps1
    9 để chỉ hiển thị các giá trị có liên quan
  • Lưu trữ các giá trị trong các biến tương ứng. Mỗi thông tin được định dạng dưới dạng bảng bằng cách sử dụng
    #The command below will get the Operating System information, convert the result to HTML code as a table and store it to a variable
    $OSinfo = Get-CimInstance -Class Win32_OperatingSystem | ConvertTo-Html -Property Version,Caption,BuildNumber,Manufacturer -Fragment
    
    #The command below will get the Processor information, convert the result to HTML code as table and store it to a variable
    $ProcessInfo = Get-CimInstance -ClassName Win32_Processor | ConvertTo-Html -Property DeviceID,Name,Caption,MaxClockSpeed,SocketDesignation,Manufacturer -Fragment
    
    #The command below will get the BIOS information, convert the result to HTML code as table and store it to a variable
    $BiosInfo = Get-CimInstance -ClassName Win32_BIOS | ConvertTo-Html -Property SMBIOSBIOSVersion,Manufacturer,Name,SerialNumber -Fragment
    
    #The command below will get the details of Disk, convert the result to HTML code as table and store it to a variable
    $DiscInfo = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" | ConvertTo-Html -Property DeviceID,DriveType,ProviderName,VolumeName,Size,FreeSpace -Fragment
    
    #The command below will get first 10 services information, convert the result to HTML code as table and store it to a variable
    $ServicesInfo = Get-CimInstance -ClassName Win32_Service | Select-Object -First 10  |ConvertTo-Html -Property Name,DisplayName,State -Fragment
     
    #The command below will combine all the information gathered into a single HTML report
    $Report = ConvertTo-HTML -Body "$ComputerName $OSinfo $ProcessInfo $BiosInfo $DiscInfo $ServicesInfo" -Title "Computer Information Report" 
    
    #The command below will generate the report to an HTML file
    $Report | Out-File .\Basic-Computer-Information-Report.html
    0
  • Hợp nhất các bảng HTML bằng tham số
    #The command below will get the Operating System information, convert the result to HTML code as a table and store it to a variable
    $OSinfo = Get-CimInstance -Class Win32_OperatingSystem | ConvertTo-Html -Property Version,Caption,BuildNumber,Manufacturer -Fragment
    
    #The command below will get the Processor information, convert the result to HTML code as table and store it to a variable
    $ProcessInfo = Get-CimInstance -ClassName Win32_Processor | ConvertTo-Html -Property DeviceID,Name,Caption,MaxClockSpeed,SocketDesignation,Manufacturer -Fragment
    
    #The command below will get the BIOS information, convert the result to HTML code as table and store it to a variable
    $BiosInfo = Get-CimInstance -ClassName Win32_BIOS | ConvertTo-Html -Property SMBIOSBIOSVersion,Manufacturer,Name,SerialNumber -Fragment
    
    #The command below will get the details of Disk, convert the result to HTML code as table and store it to a variable
    $DiscInfo = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" | ConvertTo-Html -Property DeviceID,DriveType,ProviderName,VolumeName,Size,FreeSpace -Fragment
    
    #The command below will get first 10 services information, convert the result to HTML code as table and store it to a variable
    $ServicesInfo = Get-CimInstance -ClassName Win32_Service | Select-Object -First 10  |ConvertTo-Html -Property Name,DisplayName,State -Fragment
     
    #The command below will combine all the information gathered into a single HTML report
    $Report = ConvertTo-HTML -Body "$ComputerName $OSinfo $ProcessInfo $BiosInfo $DiscInfo $ServicesInfo" -Title "Computer Information Report" 
    
    #The command below will generate the report to an HTML file
    $Report | Out-File .\Basic-Computer-Information-Report.html
    1 vào báo cáo HTML đơn
  • Đặt tiêu đề của báo cáo thành báo cáo thông tin máy tính của người dùng
  • Xuất báo cáo dưới dạng tệp HTML bằng tham số
    Get-CimInstance -Class Win32_OperatingSystem | Select-object Version,Caption,BuildNumber,Manufacturer |  ConvertTo-Html
    7

Thông tin bổ sung về tập lệnh có sẵn trong các bình luận trong đoạn mã bên dưới. Cập nhật tập lệnh với mã sau.

#The command below will get the Operating System information, convert the result to HTML code as a table and store it to a variable
$OSinfo = Get-CimInstance -Class Win32_OperatingSystem | ConvertTo-Html -Property Version,Caption,BuildNumber,Manufacturer -Fragment

#The command below will get the Processor information, convert the result to HTML code as table and store it to a variable
$ProcessInfo = Get-CimInstance -ClassName Win32_Processor | ConvertTo-Html -Property DeviceID,Name,Caption,MaxClockSpeed,SocketDesignation,Manufacturer -Fragment

#The command below will get the BIOS information, convert the result to HTML code as table and store it to a variable
$BiosInfo = Get-CimInstance -ClassName Win32_BIOS | ConvertTo-Html -Property SMBIOSBIOSVersion,Manufacturer,Name,SerialNumber -Fragment

#The command below will get the details of Disk, convert the result to HTML code as table and store it to a variable
$DiscInfo = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" | ConvertTo-Html -Property DeviceID,DriveType,ProviderName,VolumeName,Size,FreeSpace -Fragment

#The command below will get first 10 services information, convert the result to HTML code as table and store it to a variable
$ServicesInfo = Get-CimInstance -ClassName Win32_Service | Select-Object -First 10  |ConvertTo-Html -Property Name,DisplayName,State -Fragment
 
#The command below will combine all the information gathered into a single HTML report
$Report = ConvertTo-HTML -Body "$ComputerName $OSinfo $ProcessInfo $BiosInfo $DiscInfo $ServicesInfo" -Title "Computer Information Report" 

#The command below will generate the report to an HTML file
$Report | Out-File .\Basic-Computer-Information-Report.html

Chạy kịch bản trong bảng điều khiển PowerShell. Bạn có thể thấy đầu ra của báo cáo như hình dưới đây.

Hướng dẫn powershell html report - báo cáo html powershell
Báo cáo HTML với thông tin máy tính cơ bản

Thêm nhãn bằng tham số #The command below will get the Operating System information, convert the result to HTML code as a table and store it to a variable $OSinfo = Get-CimInstance -Class Win32_OperatingSystem | ConvertTo-Html -Property Version,Caption,BuildNumber,Manufacturer -Fragment #The command below will get the Processor information, convert the result to HTML code as table and store it to a variable $ProcessInfo = Get-CimInstance -ClassName Win32_Processor | ConvertTo-Html -Property DeviceID,Name,Caption,MaxClockSpeed,SocketDesignation,Manufacturer -Fragment #The command below will get the BIOS information, convert the result to HTML code as table and store it to a variable $BiosInfo = Get-CimInstance -ClassName Win32_BIOS | ConvertTo-Html -Property SMBIOSBIOSVersion,Manufacturer,Name,SerialNumber -Fragment #The command below will get the details of Disk, convert the result to HTML code as table and store it to a variable $DiscInfo = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" | ConvertTo-Html -Property DeviceID,DriveType,ProviderName,VolumeName,Size,FreeSpace -Fragment #The command below will get first 10 services information, convert the result to HTML code as table and store it to a variable $ServicesInfo = Get-CimInstance -ClassName Win32_Service | Select-Object -First 10 |ConvertTo-Html -Property Name,DisplayName,State -Fragment #The command below will combine all the information gathered into a single HTML report $Report = ConvertTo-HTML -Body "$ComputerName $OSinfo $ProcessInfo $BiosInfo $DiscInfo $ServicesInfo" -Title "Computer Information Report" #The command below will generate the report to an HTML file $Report | Out-File .\Basic-Computer-Information-Report.html4 và #The command below will get the Operating System information, convert the result to HTML code as a table and store it to a variable $OSinfo = Get-CimInstance -Class Win32_OperatingSystem | ConvertTo-Html -Property Version,Caption,BuildNumber,Manufacturer -Fragment #The command below will get the Processor information, convert the result to HTML code as table and store it to a variable $ProcessInfo = Get-CimInstance -ClassName Win32_Processor | ConvertTo-Html -Property DeviceID,Name,Caption,MaxClockSpeed,SocketDesignation,Manufacturer -Fragment #The command below will get the BIOS information, convert the result to HTML code as table and store it to a variable $BiosInfo = Get-CimInstance -ClassName Win32_BIOS | ConvertTo-Html -Property SMBIOSBIOSVersion,Manufacturer,Name,SerialNumber -Fragment #The command below will get the details of Disk, convert the result to HTML code as table and store it to a variable $DiscInfo = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" | ConvertTo-Html -Property DeviceID,DriveType,ProviderName,VolumeName,Size,FreeSpace -Fragment #The command below will get first 10 services information, convert the result to HTML code as table and store it to a variable $ServicesInfo = Get-CimInstance -ClassName Win32_Service | Select-Object -First 10 |ConvertTo-Html -Property Name,DisplayName,State -Fragment #The command below will combine all the information gathered into a single HTML report $Report = ConvertTo-HTML -Body "$ComputerName $OSinfo $ProcessInfo $BiosInfo $DiscInfo $ServicesInfo" -Title "Computer Information Report" #The command below will generate the report to an HTML file $Report | Out-File .\Basic-Computer-Information-Report.html5

Tại thời điểm này, tập lệnh giờ đây có thể nhận được tất cả thông tin cơ bản của máy tính và xuất kết quả thành HTML. Tuy nhiên, như bạn có thể thấy trong ảnh chụp màn hình ở trên, ai đó hoặc người nhận báo cáo có thể gặp khó khăn trong việc hiểu nội dung khi bạn xóa chú thích vì thông tin không được dán nhãn hoặc phân loại đúng.

Bằng cách sử dụng các tham số

#The command below will get the Operating System information, convert the result to HTML code as a table and store it to a variable
$OSinfo = Get-CimInstance -Class Win32_OperatingSystem | ConvertTo-Html -Property Version,Caption,BuildNumber,Manufacturer -Fragment

#The command below will get the Processor information, convert the result to HTML code as table and store it to a variable
$ProcessInfo = Get-CimInstance -ClassName Win32_Processor | ConvertTo-Html -Property DeviceID,Name,Caption,MaxClockSpeed,SocketDesignation,Manufacturer -Fragment

#The command below will get the BIOS information, convert the result to HTML code as table and store it to a variable
$BiosInfo = Get-CimInstance -ClassName Win32_BIOS | ConvertTo-Html -Property SMBIOSBIOSVersion,Manufacturer,Name,SerialNumber -Fragment

#The command below will get the details of Disk, convert the result to HTML code as table and store it to a variable
$DiscInfo = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" | ConvertTo-Html -Property DeviceID,DriveType,ProviderName,VolumeName,Size,FreeSpace -Fragment

#The command below will get first 10 services information, convert the result to HTML code as table and store it to a variable
$ServicesInfo = Get-CimInstance -ClassName Win32_Service | Select-Object -First 10  |ConvertTo-Html -Property Name,DisplayName,State -Fragment
 
#The command below will combine all the information gathered into a single HTML report
$Report = ConvertTo-HTML -Body "$ComputerName $OSinfo $ProcessInfo $BiosInfo $DiscInfo $ServicesInfo" -Title "Computer Information Report" 

#The command below will generate the report to an HTML file
$Report | Out-File .\Basic-Computer-Information-Report.html
4 và
#The command below will get the Operating System information, convert the result to HTML code as a table and store it to a variable
$OSinfo = Get-CimInstance -Class Win32_OperatingSystem | ConvertTo-Html -Property Version,Caption,BuildNumber,Manufacturer -Fragment

#The command below will get the Processor information, convert the result to HTML code as table and store it to a variable
$ProcessInfo = Get-CimInstance -ClassName Win32_Processor | ConvertTo-Html -Property DeviceID,Name,Caption,MaxClockSpeed,SocketDesignation,Manufacturer -Fragment

#The command below will get the BIOS information, convert the result to HTML code as table and store it to a variable
$BiosInfo = Get-CimInstance -ClassName Win32_BIOS | ConvertTo-Html -Property SMBIOSBIOSVersion,Manufacturer,Name,SerialNumber -Fragment

#The command below will get the details of Disk, convert the result to HTML code as table and store it to a variable
$DiscInfo = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" | ConvertTo-Html -Property DeviceID,DriveType,ProviderName,VolumeName,Size,FreeSpace -Fragment

#The command below will get first 10 services information, convert the result to HTML code as table and store it to a variable
$ServicesInfo = Get-CimInstance -ClassName Win32_Service | Select-Object -First 10  |ConvertTo-Html -Property Name,DisplayName,State -Fragment
 
#The command below will combine all the information gathered into a single HTML report
$Report = ConvertTo-HTML -Body "$ComputerName $OSinfo $ProcessInfo $BiosInfo $DiscInfo $ServicesInfo" -Title "Computer Information Report" 

#The command below will generate the report to an HTML file
$Report | Out-File .\Basic-Computer-Information-Report.html
5, bạn có thể thêm nhãn vào mỗi bảng để bất kỳ ai cũng có thể dễ dàng nhận ra nội dung của báo cáo.

Tham số

#The command below will get the Operating System information, convert the result to HTML code as a table and store it to a variable
$OSinfo = Get-CimInstance -Class Win32_OperatingSystem | ConvertTo-Html -Property Version,Caption,BuildNumber,Manufacturer -Fragment

#The command below will get the Processor information, convert the result to HTML code as table and store it to a variable
$ProcessInfo = Get-CimInstance -ClassName Win32_Processor | ConvertTo-Html -Property DeviceID,Name,Caption,MaxClockSpeed,SocketDesignation,Manufacturer -Fragment

#The command below will get the BIOS information, convert the result to HTML code as table and store it to a variable
$BiosInfo = Get-CimInstance -ClassName Win32_BIOS | ConvertTo-Html -Property SMBIOSBIOSVersion,Manufacturer,Name,SerialNumber -Fragment

#The command below will get the details of Disk, convert the result to HTML code as table and store it to a variable
$DiscInfo = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" | ConvertTo-Html -Property DeviceID,DriveType,ProviderName,VolumeName,Size,FreeSpace -Fragment

#The command below will get first 10 services information, convert the result to HTML code as table and store it to a variable
$ServicesInfo = Get-CimInstance -ClassName Win32_Service | Select-Object -First 10  |ConvertTo-Html -Property Name,DisplayName,State -Fragment
 
#The command below will combine all the information gathered into a single HTML report
$Report = ConvertTo-HTML -Body "$ComputerName $OSinfo $ProcessInfo $BiosInfo $DiscInfo $ServicesInfo" -Title "Computer Information Report" 

#The command below will generate the report to an HTML file
$Report | Out-File .\Basic-Computer-Information-Report.html
4 Chỉ định văn bản để thêm trước thẻ mở
#The command below will get the Operating System information, convert the result to HTML code as a table and store it to a variable
$OSinfo = Get-CimInstance -Class Win32_OperatingSystem | ConvertTo-Html -Property Version,Caption,BuildNumber,Manufacturer -Fragment

#The command below will get the Processor information, convert the result to HTML code as table and store it to a variable
$ProcessInfo = Get-CimInstance -ClassName Win32_Processor | ConvertTo-Html -Property DeviceID,Name,Caption,MaxClockSpeed,SocketDesignation,Manufacturer -Fragment

#The command below will get the BIOS information, convert the result to HTML code as table and store it to a variable
$BiosInfo = Get-CimInstance -ClassName Win32_BIOS | ConvertTo-Html -Property SMBIOSBIOSVersion,Manufacturer,Name,SerialNumber -Fragment

#The command below will get the details of Disk, convert the result to HTML code as table and store it to a variable
$DiscInfo = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" | ConvertTo-Html -Property DeviceID,DriveType,ProviderName,VolumeName,Size,FreeSpace -Fragment

#The command below will get first 10 services information, convert the result to HTML code as table and store it to a variable
$ServicesInfo = Get-CimInstance -ClassName Win32_Service | Select-Object -First 10  |ConvertTo-Html -Property Name,DisplayName,State -Fragment
 
#The command below will combine all the information gathered into a single HTML report
$Report = ConvertTo-HTML -Body "$ComputerName $OSinfo $ProcessInfo $BiosInfo $DiscInfo $ServicesInfo" -Title "Computer Information Report" 

#The command below will generate the report to an HTML file
$Report | Out-File .\Basic-Computer-Information-Report.html
9 và tham số
#The command below will get the Operating System information, convert the result to HTML code as a table and store it to a variable
$OSinfo = Get-CimInstance -Class Win32_OperatingSystem | ConvertTo-Html -Property Version,Caption,BuildNumber,Manufacturer -Fragment

#The command below will get the Processor information, convert the result to HTML code as table and store it to a variable
$ProcessInfo = Get-CimInstance -ClassName Win32_Processor | ConvertTo-Html -Property DeviceID,Name,Caption,MaxClockSpeed,SocketDesignation,Manufacturer -Fragment

#The command below will get the BIOS information, convert the result to HTML code as table and store it to a variable
$BiosInfo = Get-CimInstance -ClassName Win32_BIOS | ConvertTo-Html -Property SMBIOSBIOSVersion,Manufacturer,Name,SerialNumber -Fragment

#The command below will get the details of Disk, convert the result to HTML code as table and store it to a variable
$DiscInfo = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" | ConvertTo-Html -Property DeviceID,DriveType,ProviderName,VolumeName,Size,FreeSpace -Fragment

#The command below will get first 10 services information, convert the result to HTML code as table and store it to a variable
$ServicesInfo = Get-CimInstance -ClassName Win32_Service | Select-Object -First 10  |ConvertTo-Html -Property Name,DisplayName,State -Fragment
 
#The command below will combine all the information gathered into a single HTML report
$Report = ConvertTo-HTML -Body "$ComputerName $OSinfo $ProcessInfo $BiosInfo $DiscInfo $ServicesInfo" -Title "Computer Information Report" 

#The command below will generate the report to an HTML file
$Report | Out-File .\Basic-Computer-Information-Report.html
5 chỉ định văn bản để thêm sau khi đóng thẻ
#The command below will get the name of the computer
$ComputerName = "

Computer name: $env:computername

" #The command below will get the Operating System information, convert the result to HTML code as table and store it to a variable $OSinfo = Get-CimInstance -Class Win32_OperatingSystem | ConvertTo-Html -Property Version,Caption,BuildNumber,Manufacturer -Fragment -PreContent "

Operating System Information

" #The command below will get the Processor information, convert the result to HTML code as table and store it to a variable $ProcessInfo = Get-CimInstance -ClassName Win32_Processor | ConvertTo-Html -Property DeviceID,Name,Caption,MaxClockSpeed,SocketDesignation,Manufacturer -Fragment -PreContent "

Processor Information

" #The command below will get the BIOS information, convert the result to HTML code as table and store it to a variable $BiosInfo = Get-CimInstance -ClassName Win32_BIOS | ConvertTo-Html -Property SMBIOSBIOSVersion,Manufacturer,Name,SerialNumber -Fragment -PreContent "

BIOS Information

" #The command below will get the details of Disk, convert the result to HTML code as table and store it to a variable $DiscInfo = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" | ConvertTo-Html -Property DeviceID,DriveType,ProviderName,VolumeName,Size,FreeSpace -Fragment -PreContent "

Disk Information

" #The command below will get first 10 services information, convert the result to HTML code as table and store it to a variable $ServicesInfo = Get-CimInstance -ClassName Win32_Service | Select-Object -First 10 | ConvertTo-Html -Property Name,DisplayName,State -Fragment -PreContent "

Services Information

" #The command below will combine all the information gathered into a single HTML report $Report = ConvertTo-HTML -Body "$ComputerName $OSinfo $ProcessInfo $BiosInfo $DiscInfo $ServicesInfo" -Title "Computer Information Report" -PostContent "

Creation Date: $(Get-Date)

" #The command below will generate the report to an HTML file $Report | Out-File .\Basic-Computer-Information-Report.html

1. Các giá trị được thêm vào các tham số này không được chuyển đổi tự động thành mã HTML, vì vậy bạn cần sử dụng rõ ràng các thẻ HTML để nó được hiển thị đúng dưới dạng các phần tử HTML.

Cập nhật tập lệnh bằng cách sử dụng các lệnh bên dưới sau đó chạy tập lệnh trong bảng điều khiển & nbsp; PowerShell.

Dưới đây là những thay đổi trong tập lệnh:

  • Lệnh mới được thêm vào để có được tên của máy tính. Giá trị của biến
    #The command below will get the name of the computer
    $ComputerName = "

    Computer name: $env:computername

    " #The command below will get the Operating System information, convert the result to HTML code as table and store it to a variable $OSinfo = Get-CimInstance -Class Win32_OperatingSystem | ConvertTo-Html -Property Version,Caption,BuildNumber,Manufacturer -Fragment -PreContent "

    Operating System Information

    " #The command below will get the Processor information, convert the result to HTML code as table and store it to a variable $ProcessInfo = Get-CimInstance -ClassName Win32_Processor | ConvertTo-Html -Property DeviceID,Name,Caption,MaxClockSpeed,SocketDesignation,Manufacturer -Fragment -PreContent "

    Processor Information

    " #The command below will get the BIOS information, convert the result to HTML code as table and store it to a variable $BiosInfo = Get-CimInstance -ClassName Win32_BIOS | ConvertTo-Html -Property SMBIOSBIOSVersion,Manufacturer,Name,SerialNumber -Fragment -PreContent "

    BIOS Information

    " #The command below will get the details of Disk, convert the result to HTML code as table and store it to a variable $DiscInfo = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" | ConvertTo-Html -Property DeviceID,DriveType,ProviderName,VolumeName,Size,FreeSpace -Fragment -PreContent "

    Disk Information

    " #The command below will get first 10 services information, convert the result to HTML code as table and store it to a variable $ServicesInfo = Get-CimInstance -ClassName Win32_Service | Select-Object -First 10 | ConvertTo-Html -Property Name,DisplayName,State -Fragment -PreContent "

    Services Information

    " #The command below will combine all the information gathered into a single HTML report $Report = ConvertTo-HTML -Body "$ComputerName $OSinfo $ProcessInfo $BiosInfo $DiscInfo $ServicesInfo" -Title "Computer Information Report" -PostContent "

    Creation Date: $(Get-Date)

    " #The command below will generate the report to an HTML file $Report | Out-File .\Basic-Computer-Information-Report.html

    2 có thẻ
    #The command below will get the name of the computer
    $ComputerName = "

    Computer name: $env:computername

    " #The command below will get the Operating System information, convert the result to HTML code as table and store it to a variable $OSinfo = Get-CimInstance -Class Win32_OperatingSystem | ConvertTo-Html -Property Version,Caption,BuildNumber,Manufacturer -Fragment -PreContent "

    Operating System Information

    " #The command below will get the Processor information, convert the result to HTML code as table and store it to a variable $ProcessInfo = Get-CimInstance -ClassName Win32_Processor | ConvertTo-Html -Property DeviceID,Name,Caption,MaxClockSpeed,SocketDesignation,Manufacturer -Fragment -PreContent "

    Processor Information

    " #The command below will get the BIOS information, convert the result to HTML code as table and store it to a variable $BiosInfo = Get-CimInstance -ClassName Win32_BIOS | ConvertTo-Html -Property SMBIOSBIOSVersion,Manufacturer,Name,SerialNumber -Fragment -PreContent "

    BIOS Information

    " #The command below will get the details of Disk, convert the result to HTML code as table and store it to a variable $DiscInfo = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" | ConvertTo-Html -Property DeviceID,DriveType,ProviderName,VolumeName,Size,FreeSpace -Fragment -PreContent "

    Disk Information

    " #The command below will get first 10 services information, convert the result to HTML code as table and store it to a variable $ServicesInfo = Get-CimInstance -ClassName Win32_Service | Select-Object -First 10 | ConvertTo-Html -Property Name,DisplayName,State -Fragment -PreContent "

    Services Information

    " #The command below will combine all the information gathered into a single HTML report $Report = ConvertTo-HTML -Body "$ComputerName $OSinfo $ProcessInfo $BiosInfo $DiscInfo $ServicesInfo" -Title "Computer Information Report" -PostContent "

    Creation Date: $(Get-Date)

    " #The command below will generate the report to an HTML file $Report | Out-File .\Basic-Computer-Information-Report.html

    3 để thay đổi định dạng văn bản thành tiêu đề khi được hiển thị trong trình duyệt.
  • Các nhãn khác nhau được thêm vào trong mỗi bảng bằng tham số và giá trị
    #The command below will get the Operating System information, convert the result to HTML code as a table and store it to a variable
    $OSinfo = Get-CimInstance -Class Win32_OperatingSystem | ConvertTo-Html -Property Version,Caption,BuildNumber,Manufacturer -Fragment
    
    #The command below will get the Processor information, convert the result to HTML code as table and store it to a variable
    $ProcessInfo = Get-CimInstance -ClassName Win32_Processor | ConvertTo-Html -Property DeviceID,Name,Caption,MaxClockSpeed,SocketDesignation,Manufacturer -Fragment
    
    #The command below will get the BIOS information, convert the result to HTML code as table and store it to a variable
    $BiosInfo = Get-CimInstance -ClassName Win32_BIOS | ConvertTo-Html -Property SMBIOSBIOSVersion,Manufacturer,Name,SerialNumber -Fragment
    
    #The command below will get the details of Disk, convert the result to HTML code as table and store it to a variable
    $DiscInfo = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" | ConvertTo-Html -Property DeviceID,DriveType,ProviderName,VolumeName,Size,FreeSpace -Fragment
    
    #The command below will get first 10 services information, convert the result to HTML code as table and store it to a variable
    $ServicesInfo = Get-CimInstance -ClassName Win32_Service | Select-Object -First 10  |ConvertTo-Html -Property Name,DisplayName,State -Fragment
     
    #The command below will combine all the information gathered into a single HTML report
    $Report = ConvertTo-HTML -Body "$ComputerName $OSinfo $ProcessInfo $BiosInfo $DiscInfo $ServicesInfo" -Title "Computer Information Report" 
    
    #The command below will generate the report to an HTML file
    $Report | Out-File .\Basic-Computer-Information-Report.html
    4 được đặt trong các thẻ
    #The command below will get the name of the computer
    $ComputerName = "

    Computer name: $env:computername

    " #The command below will get the Operating System information, convert the result to HTML code as table and store it to a variable $OSinfo = Get-CimInstance -Class Win32_OperatingSystem | ConvertTo-Html -Property Version,Caption,BuildNumber,Manufacturer -Fragment -PreContent "

    Operating System Information

    " #The command below will get the Processor information, convert the result to HTML code as table and store it to a variable $ProcessInfo = Get-CimInstance -ClassName Win32_Processor | ConvertTo-Html -Property DeviceID,Name,Caption,MaxClockSpeed,SocketDesignation,Manufacturer -Fragment -PreContent "

    Processor Information

    " #The command below will get the BIOS information, convert the result to HTML code as table and store it to a variable $BiosInfo = Get-CimInstance -ClassName Win32_BIOS | ConvertTo-Html -Property SMBIOSBIOSVersion,Manufacturer,Name,SerialNumber -Fragment -PreContent "

    BIOS Information

    " #The command below will get the details of Disk, convert the result to HTML code as table and store it to a variable $DiscInfo = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" | ConvertTo-Html -Property DeviceID,DriveType,ProviderName,VolumeName,Size,FreeSpace -Fragment -PreContent "

    Disk Information

    " #The command below will get first 10 services information, convert the result to HTML code as table and store it to a variable $ServicesInfo = Get-CimInstance -ClassName Win32_Service | Select-Object -First 10 | ConvertTo-Html -Property Name,DisplayName,State -Fragment -PreContent "

    Services Information

    " #The command below will combine all the information gathered into a single HTML report $Report = ConvertTo-HTML -Body "$ComputerName $OSinfo $ProcessInfo $BiosInfo $DiscInfo $ServicesInfo" -Title "Computer Information Report" -PostContent "

    Creation Date: $(Get-Date)

    " #The command below will generate the report to an HTML file $Report | Out-File .\Basic-Computer-Information-Report.html

    3
  • Nhãn ngày tạo được thêm vào cuối báo cáo bằng tham số và giá trị
    #The command below will get the Operating System information, convert the result to HTML code as a table and store it to a variable
    $OSinfo = Get-CimInstance -Class Win32_OperatingSystem | ConvertTo-Html -Property Version,Caption,BuildNumber,Manufacturer -Fragment
    
    #The command below will get the Processor information, convert the result to HTML code as table and store it to a variable
    $ProcessInfo = Get-CimInstance -ClassName Win32_Processor | ConvertTo-Html -Property DeviceID,Name,Caption,MaxClockSpeed,SocketDesignation,Manufacturer -Fragment
    
    #The command below will get the BIOS information, convert the result to HTML code as table and store it to a variable
    $BiosInfo = Get-CimInstance -ClassName Win32_BIOS | ConvertTo-Html -Property SMBIOSBIOSVersion,Manufacturer,Name,SerialNumber -Fragment
    
    #The command below will get the details of Disk, convert the result to HTML code as table and store it to a variable
    $DiscInfo = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" | ConvertTo-Html -Property DeviceID,DriveType,ProviderName,VolumeName,Size,FreeSpace -Fragment
    
    #The command below will get first 10 services information, convert the result to HTML code as table and store it to a variable
    $ServicesInfo = Get-CimInstance -ClassName Win32_Service | Select-Object -First 10  |ConvertTo-Html -Property Name,DisplayName,State -Fragment
     
    #The command below will combine all the information gathered into a single HTML report
    $Report = ConvertTo-HTML -Body "$ComputerName $OSinfo $ProcessInfo $BiosInfo $DiscInfo $ServicesInfo" -Title "Computer Information Report" 
    
    #The command below will generate the report to an HTML file
    $Report | Out-File .\Basic-Computer-Information-Report.html
    5 được đặt trong thẻ
    #The command below will get the name of the computer
    $ComputerName = "

    Computer name: $env:computername

    " #The command below will get the Operating System information, convert the result to HTML code as table and store it to a variable $OSinfo = Get-CimInstance -Class Win32_OperatingSystem | ConvertTo-Html -Property Version,Caption,BuildNumber,Manufacturer -Fragment -PreContent "

    Operating System Information

    " #The command below will get the Processor information, convert the result to HTML code as table and store it to a variable $ProcessInfo = Get-CimInstance -ClassName Win32_Processor | ConvertTo-Html -Property DeviceID,Name,Caption,MaxClockSpeed,SocketDesignation,Manufacturer -Fragment -PreContent "

    Processor Information

    " #The command below will get the BIOS information, convert the result to HTML code as table and store it to a variable $BiosInfo = Get-CimInstance -ClassName Win32_BIOS | ConvertTo-Html -Property SMBIOSBIOSVersion,Manufacturer,Name,SerialNumber -Fragment -PreContent "

    BIOS Information

    " #The command below will get the details of Disk, convert the result to HTML code as table and store it to a variable $DiscInfo = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" | ConvertTo-Html -Property DeviceID,DriveType,ProviderName,VolumeName,Size,FreeSpace -Fragment -PreContent "

    Disk Information

    " #The command below will get first 10 services information, convert the result to HTML code as table and store it to a variable $ServicesInfo = Get-CimInstance -ClassName Win32_Service | Select-Object -First 10 | ConvertTo-Html -Property Name,DisplayName,State -Fragment -PreContent "

    Services Information

    " #The command below will combine all the information gathered into a single HTML report $Report = ConvertTo-HTML -Body "$ComputerName $OSinfo $ProcessInfo $BiosInfo $DiscInfo $ServicesInfo" -Title "Computer Information Report" -PostContent "

    Creation Date: $(Get-Date)

    " #The command below will generate the report to an HTML file $Report | Out-File .\Basic-Computer-Information-Report.html

    7

#The command below will get the name of the computer
$ComputerName = "

Computer name: $env:computername

" #The command below will get the Operating System information, convert the result to HTML code as table and store it to a variable $OSinfo = Get-CimInstance -Class Win32_OperatingSystem | ConvertTo-Html -Property Version,Caption,BuildNumber,Manufacturer -Fragment -PreContent "

Operating System Information

" #The command below will get the Processor information, convert the result to HTML code as table and store it to a variable $ProcessInfo = Get-CimInstance -ClassName Win32_Processor | ConvertTo-Html -Property DeviceID,Name,Caption,MaxClockSpeed,SocketDesignation,Manufacturer -Fragment -PreContent "

Processor Information

" #The command below will get the BIOS information, convert the result to HTML code as table and store it to a variable $BiosInfo = Get-CimInstance -ClassName Win32_BIOS | ConvertTo-Html -Property SMBIOSBIOSVersion,Manufacturer,Name,SerialNumber -Fragment -PreContent "

BIOS Information

" #The command below will get the details of Disk, convert the result to HTML code as table and store it to a variable $DiscInfo = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" | ConvertTo-Html -Property DeviceID,DriveType,ProviderName,VolumeName,Size,FreeSpace -Fragment -PreContent "

Disk Information

" #The command below will get first 10 services information, convert the result to HTML code as table and store it to a variable $ServicesInfo = Get-CimInstance -ClassName Win32_Service | Select-Object -First 10 | ConvertTo-Html -Property Name,DisplayName,State -Fragment -PreContent "

Services Information

" #The command below will combine all the information gathered into a single HTML report $Report = ConvertTo-HTML -Body "$ComputerName $OSinfo $ProcessInfo $BiosInfo $DiscInfo $ServicesInfo" -Title "Computer Information Report" -PostContent "

Creation Date: $(Get-Date)

" #The command below will generate the report to an HTML file $Report | Out-File .\Basic-Computer-Information-Report.html

Bạn có thể thấy với một chút công việc làm thêm, báo cáo có thể trông tốt hơn rất nhiều, báo cáo nên được cập nhật như hình dưới đây.

Hướng dẫn powershell html report - báo cáo html powershell
Báo cáo HTML được thêm vào với các nhãn bằng cách sử dụng các tham số Preontent và Postcontent

Thay đổi bố cục bảng bằng tham số #The command below will get the name of the computer $ComputerName = "

Computer name: $env:computername

" #The command below will get the Operating System information, convert the result to HTML code as table and store it to a variable $OSinfo = Get-CimInstance -Class Win32_OperatingSystem | ConvertTo-Html -Property Version,Caption,BuildNumber,Manufacturer -Fragment -PreContent "

Operating System Information

" #The command below will get the Processor information, convert the result to HTML code as table and store it to a variable $ProcessInfo = Get-CimInstance -ClassName Win32_Processor | ConvertTo-Html -Property DeviceID,Name,Caption,MaxClockSpeed,SocketDesignation,Manufacturer -Fragment -PreContent "

Processor Information

" #The command below will get the BIOS information, convert the result to HTML code as table and store it to a variable $BiosInfo = Get-CimInstance -ClassName Win32_BIOS | ConvertTo-Html -Property SMBIOSBIOSVersion,Manufacturer,Name,SerialNumber -Fragment -PreContent "

BIOS Information

" #The command below will get the details of Disk, convert the result to HTML code as table and store it to a variable $DiscInfo = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" | ConvertTo-Html -Property DeviceID,DriveType,ProviderName,VolumeName,Size,FreeSpace -Fragment -PreContent "

Disk Information

" #The command below will get first 10 services information, convert the result to HTML code as table and store it to a variable $ServicesInfo = Get-CimInstance -ClassName Win32_Service | Select-Object -First 10 | ConvertTo-Html -Property Name,DisplayName,State -Fragment -PreContent "

Services Information

" #The command below will combine all the information gathered into a single HTML report $Report = ConvertTo-HTML -Body "$ComputerName $OSinfo $ProcessInfo $BiosInfo $DiscInfo $ServicesInfo" -Title "Computer Information Report" -PostContent "

Creation Date: $(Get-Date)

" #The command below will generate the report to an HTML file $Report | Out-File .\Basic-Computer-Information-Report.html8

Có lẽ, bảng HTML được tạo có nhiều cột và bạn muốn thay đổi định dạng để hiển thị đúng giá trị dưới dạng danh sách, bạn có thể sử dụng tham số

#The command below will get the name of the computer
$ComputerName = "

Computer name: $env:computername

" #The command below will get the Operating System information, convert the result to HTML code as table and store it to a variable $OSinfo = Get-CimInstance -Class Win32_OperatingSystem | ConvertTo-Html -Property Version,Caption,BuildNumber,Manufacturer -Fragment -PreContent "

Operating System Information

" #The command below will get the Processor information, convert the result to HTML code as table and store it to a variable $ProcessInfo = Get-CimInstance -ClassName Win32_Processor | ConvertTo-Html -Property DeviceID,Name,Caption,MaxClockSpeed,SocketDesignation,Manufacturer -Fragment -PreContent "

Processor Information

" #The command below will get the BIOS information, convert the result to HTML code as table and store it to a variable $BiosInfo = Get-CimInstance -ClassName Win32_BIOS | ConvertTo-Html -Property SMBIOSBIOSVersion,Manufacturer,Name,SerialNumber -Fragment -PreContent "

BIOS Information

" #The command below will get the details of Disk, convert the result to HTML code as table and store it to a variable $DiscInfo = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" | ConvertTo-Html -Property DeviceID,DriveType,ProviderName,VolumeName,Size,FreeSpace -Fragment -PreContent "

Disk Information

" #The command below will get first 10 services information, convert the result to HTML code as table and store it to a variable $ServicesInfo = Get-CimInstance -ClassName Win32_Service | Select-Object -First 10 | ConvertTo-Html -Property Name,DisplayName,State -Fragment -PreContent "

Services Information

" #The command below will combine all the information gathered into a single HTML report $Report = ConvertTo-HTML -Body "$ComputerName $OSinfo $ProcessInfo $BiosInfo $DiscInfo $ServicesInfo" -Title "Computer Information Report" -PostContent "

Creation Date: $(Get-Date)

" #The command below will generate the report to an HTML file $Report | Out-File .\Basic-Computer-Information-Report.html

8. Theo mặc định, khi bạn ống đầu ra đến
#The command below will get the name of the computer
$ComputerName = "

Computer name: $env:computername

" #The command below will get the Operating System information, convert the result to HTML code as table and store it to a variable $OSinfo = Get-CimInstance -Class Win32_OperatingSystem | ConvertTo-Html -As List -Property Version,Caption,BuildNumber,Manufacturer -Fragment -PreContent "

Operating System Information

" #The command below will get the Processor information, convert the result to HTML code as table and store it to a variable $ProcessInfo = Get-CimInstance -ClassName Win32_Processor | ConvertTo-Html -As List -Property DeviceID,Name,Caption,MaxClockSpeed,SocketDesignation,Manufacturer -Fragment -PreContent "

Processor Information

" #The command below will get the BIOS information, convert the result to HTML code as table and store it to a variable $BiosInfo = Get-CimInstance -ClassName Win32_BIOS | ConvertTo-Html -As List -Property SMBIOSBIOSVersion,Manufacturer,Name,SerialNumber -Fragment -PreContent "

BIOS Information

" #The command below will get the details of Disk, convert the result to HTML code as table and store it to a variable $DiscInfo = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" | ConvertTo-Html -As List -Property DeviceID,DriveType,ProviderName,VolumeName,Size,FreeSpace -Fragment -PreContent "

Disk Information

" #The command below will get first 10 services information, convert the result to HTML code as table and store it to a variable $ServicesInfo = Get-CimInstance -ClassName Win32_Service | Select-Object -First 10 |ConvertTo-Html -Property Name,DisplayName,State -Fragment -PreContent "

Services Information

" #The command below will combine all the information gathered into a single HTML report $Report = ConvertTo-HTML -Body "$ComputerName $OSinfo $ProcessInfo $BiosInfo $DiscInfo $ServicesInfo" -Title "Computer Information Report" -PostContent "

Creation Date: $(Get-Date)

" #The command below will generate the report to an HTML file $Report | Out-File .\Basic-Computer-Information-Report.html

0, PowerShell sẽ tạo ra một bảng HTML giống với định dạng bảng Windows PowerShell.

Như bạn có thể thấy trong ảnh chụp màn hình bên dưới, tiêu đề bảng hiển thị các tên thuộc tính như phiên bản, chú thích, số xây dựng và nhà sản xuất và mỗi hàng bảng đại diện cho một đối tượng và hiển thị các giá trị đối tượng cho mỗi thuộc tính.

Hướng dẫn powershell html report - báo cáo html powershell
Mã HTML được tạo bởi màn hình convertto-HTML được định dạng thành bảng

Để thay đổi bố cục của bảng thành danh sách, hãy sử dụng tham số

#The command below will get the name of the computer
$ComputerName = "

Computer name: $env:computername

" #The command below will get the Operating System information, convert the result to HTML code as table and store it to a variable $OSinfo = Get-CimInstance -Class Win32_OperatingSystem | ConvertTo-Html -As List -Property Version,Caption,BuildNumber,Manufacturer -Fragment -PreContent "

Operating System Information

" #The command below will get the Processor information, convert the result to HTML code as table and store it to a variable $ProcessInfo = Get-CimInstance -ClassName Win32_Processor | ConvertTo-Html -As List -Property DeviceID,Name,Caption,MaxClockSpeed,SocketDesignation,Manufacturer -Fragment -PreContent "

Processor Information

" #The command below will get the BIOS information, convert the result to HTML code as table and store it to a variable $BiosInfo = Get-CimInstance -ClassName Win32_BIOS | ConvertTo-Html -As List -Property SMBIOSBIOSVersion,Manufacturer,Name,SerialNumber -Fragment -PreContent "

BIOS Information

" #The command below will get the details of Disk, convert the result to HTML code as table and store it to a variable $DiscInfo = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" | ConvertTo-Html -As List -Property DeviceID,DriveType,ProviderName,VolumeName,Size,FreeSpace -Fragment -PreContent "

Disk Information

" #The command below will get first 10 services information, convert the result to HTML code as table and store it to a variable $ServicesInfo = Get-CimInstance -ClassName Win32_Service | Select-Object -First 10 |ConvertTo-Html -Property Name,DisplayName,State -Fragment -PreContent "

Services Information

" #The command below will combine all the information gathered into a single HTML report $Report = ConvertTo-HTML -Body "$ComputerName $OSinfo $ProcessInfo $BiosInfo $DiscInfo $ServicesInfo" -Title "Computer Information Report" -PostContent "

Creation Date: $(Get-Date)

" #The command below will generate the report to an HTML file $Report | Out-File .\Basic-Computer-Information-Report.html

1 sau đó được theo sau bởi danh sách. PowerShell tạo bảng HTML hai cột cho mỗi đối tượng giống với định dạng danh sách Windows PowerShell. Cột đầu tiên hiển thị các tên thuộc tính như phiên bản, chú thích, số xây dựng và nhà sản xuất và cột thứ hai hiển thị giá trị thuộc tính.

Hướng dẫn powershell html report - báo cáo html powershell
Mã HTML được tạo bởi CMDlet ConvertTO-HTML được định dạng làm danh sách

Từ các ví dụ trên, bây giờ bạn có ý tưởng về cách thay đổi bố cục của bảng, hãy để áp dụng tham số

#The command below will get the name of the computer
$ComputerName = "

Computer name: $env:computername

" #The command below will get the Operating System information, convert the result to HTML code as table and store it to a variable $OSinfo = Get-CimInstance -Class Win32_OperatingSystem | ConvertTo-Html -Property Version,Caption,BuildNumber,Manufacturer -Fragment -PreContent "

Operating System Information

" #The command below will get the Processor information, convert the result to HTML code as table and store it to a variable $ProcessInfo = Get-CimInstance -ClassName Win32_Processor | ConvertTo-Html -Property DeviceID,Name,Caption,MaxClockSpeed,SocketDesignation,Manufacturer -Fragment -PreContent "

Processor Information

" #The command below will get the BIOS information, convert the result to HTML code as table and store it to a variable $BiosInfo = Get-CimInstance -ClassName Win32_BIOS | ConvertTo-Html -Property SMBIOSBIOSVersion,Manufacturer,Name,SerialNumber -Fragment -PreContent "

BIOS Information

" #The command below will get the details of Disk, convert the result to HTML code as table and store it to a variable $DiscInfo = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" | ConvertTo-Html -Property DeviceID,DriveType,ProviderName,VolumeName,Size,FreeSpace -Fragment -PreContent "

Disk Information

" #The command below will get first 10 services information, convert the result to HTML code as table and store it to a variable $ServicesInfo = Get-CimInstance -ClassName Win32_Service | Select-Object -First 10 | ConvertTo-Html -Property Name,DisplayName,State -Fragment -PreContent "

Services Information

" #The command below will combine all the information gathered into a single HTML report $Report = ConvertTo-HTML -Body "$ComputerName $OSinfo $ProcessInfo $BiosInfo $DiscInfo $ServicesInfo" -Title "Computer Information Report" -PostContent "

Creation Date: $(Get-Date)

" #The command below will generate the report to an HTML file $Report | Out-File .\Basic-Computer-Information-Report.html

8 trong tập lệnh của chúng tôi để thay đổi bố cục của hệ điều hành, bộ xử lý, BIOS và bảng thông tin đĩa thành định dạng liệt kê.

Cập nhật tập lệnh với mã sau đây. Kịch bản có tham số

#The command below will get the name of the computer
$ComputerName = "

Computer name: $env:computername

" #The command below will get the Operating System information, convert the result to HTML code as table and store it to a variable $OSinfo = Get-CimInstance -Class Win32_OperatingSystem | ConvertTo-Html -As List -Property Version,Caption,BuildNumber,Manufacturer -Fragment -PreContent "

Operating System Information

" #The command below will get the Processor information, convert the result to HTML code as table and store it to a variable $ProcessInfo = Get-CimInstance -ClassName Win32_Processor | ConvertTo-Html -As List -Property DeviceID,Name,Caption,MaxClockSpeed,SocketDesignation,Manufacturer -Fragment -PreContent "

Processor Information

" #The command below will get the BIOS information, convert the result to HTML code as table and store it to a variable $BiosInfo = Get-CimInstance -ClassName Win32_BIOS | ConvertTo-Html -As List -Property SMBIOSBIOSVersion,Manufacturer,Name,SerialNumber -Fragment -PreContent "

BIOS Information

" #The command below will get the details of Disk, convert the result to HTML code as table and store it to a variable $DiscInfo = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" | ConvertTo-Html -As List -Property DeviceID,DriveType,ProviderName,VolumeName,Size,FreeSpace -Fragment -PreContent "

Disk Information

" #The command below will get first 10 services information, convert the result to HTML code as table and store it to a variable $ServicesInfo = Get-CimInstance -ClassName Win32_Service | Select-Object -First 10 |ConvertTo-Html -Property Name,DisplayName,State -Fragment -PreContent "

Services Information

" #The command below will combine all the information gathered into a single HTML report $Report = ConvertTo-HTML -Body "$ComputerName $OSinfo $ProcessInfo $BiosInfo $DiscInfo $ServicesInfo" -Title "Computer Information Report" -PostContent "

Creation Date: $(Get-Date)

" #The command below will generate the report to an HTML file $Report | Out-File .\Basic-Computer-Information-Report.html

1 trong các dòng lệnh cho hệ điều hành, bộ xử lý, BIOS và đĩa.

#The command below will get the name of the computer
$ComputerName = "

Computer name: $env:computername

" #The command below will get the Operating System information, convert the result to HTML code as table and store it to a variable $OSinfo = Get-CimInstance -Class Win32_OperatingSystem | ConvertTo-Html -As List -Property Version,Caption,BuildNumber,Manufacturer -Fragment -PreContent "

Operating System Information

" #The command below will get the Processor information, convert the result to HTML code as table and store it to a variable $ProcessInfo = Get-CimInstance -ClassName Win32_Processor | ConvertTo-Html -As List -Property DeviceID,Name,Caption,MaxClockSpeed,SocketDesignation,Manufacturer -Fragment -PreContent "

Processor Information

" #The command below will get the BIOS information, convert the result to HTML code as table and store it to a variable $BiosInfo = Get-CimInstance -ClassName Win32_BIOS | ConvertTo-Html -As List -Property SMBIOSBIOSVersion,Manufacturer,Name,SerialNumber -Fragment -PreContent "

BIOS Information

" #The command below will get the details of Disk, convert the result to HTML code as table and store it to a variable $DiscInfo = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" | ConvertTo-Html -As List -Property DeviceID,DriveType,ProviderName,VolumeName,Size,FreeSpace -Fragment -PreContent "

Disk Information

" #The command below will get first 10 services information, convert the result to HTML code as table and store it to a variable $ServicesInfo = Get-CimInstance -ClassName Win32_Service | Select-Object -First 10 |ConvertTo-Html -Property Name,DisplayName,State -Fragment -PreContent "

Services Information

" #The command below will combine all the information gathered into a single HTML report $Report = ConvertTo-HTML -Body "$ComputerName $OSinfo $ProcessInfo $BiosInfo $DiscInfo $ServicesInfo" -Title "Computer Information Report" -PostContent "

Creation Date: $(Get-Date)

" #The command below will generate the report to an HTML file $Report | Out-File .\Basic-Computer-Information-Report.html

Sau khi áp dụng tham số

#The command below will get the name of the computer
$ComputerName = "

Computer name: $env:computername

" #The command below will get the Operating System information, convert the result to HTML code as table and store it to a variable $OSinfo = Get-CimInstance -Class Win32_OperatingSystem | ConvertTo-Html -As List -Property Version,Caption,BuildNumber,Manufacturer -Fragment -PreContent "

Operating System Information

" #The command below will get the Processor information, convert the result to HTML code as table and store it to a variable $ProcessInfo = Get-CimInstance -ClassName Win32_Processor | ConvertTo-Html -As List -Property DeviceID,Name,Caption,MaxClockSpeed,SocketDesignation,Manufacturer -Fragment -PreContent "

Processor Information

" #The command below will get the BIOS information, convert the result to HTML code as table and store it to a variable $BiosInfo = Get-CimInstance -ClassName Win32_BIOS | ConvertTo-Html -As List -Property SMBIOSBIOSVersion,Manufacturer,Name,SerialNumber -Fragment -PreContent "

BIOS Information

" #The command below will get the details of Disk, convert the result to HTML code as table and store it to a variable $DiscInfo = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" | ConvertTo-Html -As List -Property DeviceID,DriveType,ProviderName,VolumeName,Size,FreeSpace -Fragment -PreContent "

Disk Information

" #The command below will get first 10 services information, convert the result to HTML code as table and store it to a variable $ServicesInfo = Get-CimInstance -ClassName Win32_Service | Select-Object -First 10 |ConvertTo-Html -Property Name,DisplayName,State -Fragment -PreContent "

Services Information

" #The command below will combine all the information gathered into a single HTML report $Report = ConvertTo-HTML -Body "$ComputerName $OSinfo $ProcessInfo $BiosInfo $DiscInfo $ServicesInfo" -Title "Computer Information Report" -PostContent "

Creation Date: $(Get-Date)

" #The command below will generate the report to an HTML file $Report | Out-File .\Basic-Computer-Information-Report.html

1, báo cáo phải được cập nhật như hình dưới đây. Bố cục bảng cho hệ điều hành, bộ xử lý, BIOS và thông tin đĩa hiện được thay đổi thành danh sách.

Hướng dẫn powershell html report - báo cáo html powershell
Báo cáo HTML với các bảng được định dạng làm danh sách

Tăng cường báo cáo bằng CSS

Hãy cùng đưa báo cáo lên cấp độ tiếp theo và bắt đầu thêm thiết kế bằng CSS. CSS được sử dụng để kiểm soát cách báo cáo HTML sẽ xem xét trong trình duyệt web. CSS kiểm soát các phông chữ, văn bản, màu sắc, nền, lề và bố cục. Ở cuối phần này, bạn sẽ có thể xem báo cáo sẽ chuyển đổi từ định dạng đơn giản sang phong phú bằng CSS.

Có ba cách để áp dụng CSS trong HTML như nội tuyến, nội bộ và bên ngoài. Đối với bài viết này, bạn sẽ áp dụng phương thức nội bộ bằng tham số

#The command below will get the name of the computer
$ComputerName = "

Computer name: $env:computername

" #The command below will get the Operating System information, convert the result to HTML code as table and store it to a variable $OSinfo = Get-CimInstance -Class Win32_OperatingSystem | ConvertTo-Html -As List -Property Version,Caption,BuildNumber,Manufacturer -Fragment -PreContent "

Operating System Information

" #The command below will get the Processor information, convert the result to HTML code as table and store it to a variable $ProcessInfo = Get-CimInstance -ClassName Win32_Processor | ConvertTo-Html -As List -Property DeviceID,Name,Caption,MaxClockSpeed,SocketDesignation,Manufacturer -Fragment -PreContent "

Processor Information

" #The command below will get the BIOS information, convert the result to HTML code as table and store it to a variable $BiosInfo = Get-CimInstance -ClassName Win32_BIOS | ConvertTo-Html -As List -Property SMBIOSBIOSVersion,Manufacturer,Name,SerialNumber -Fragment -PreContent "

BIOS Information

" #The command below will get the details of Disk, convert the result to HTML code as table and store it to a variable $DiscInfo = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" | ConvertTo-Html -As List -Property DeviceID,DriveType,ProviderName,VolumeName,Size,FreeSpace -Fragment -PreContent "

Disk Information

" #The command below will get first 10 services information, convert the result to HTML code as table and store it to a variable $ServicesInfo = Get-CimInstance -ClassName Win32_Service | Select-Object -First 10 |ConvertTo-Html -Property Name,DisplayName,State -Fragment -PreContent "

Services Information

" #The command below will combine all the information gathered into a single HTML report $Report = ConvertTo-HTML -Body "$ComputerName $OSinfo $ProcessInfo $BiosInfo $DiscInfo $ServicesInfo" -Title "Computer Information Report" -PostContent "

Creation Date: $(Get-Date)

" #The command below will generate the report to an HTML file $Report | Out-File .\Basic-Computer-Information-Report.html

5 trong báo cáo HTML.

Tham số

#The command below will get the name of the computer
$ComputerName = "

Computer name: $env:computername

" #The command below will get the Operating System information, convert the result to HTML code as table and store it to a variable $OSinfo = Get-CimInstance -Class Win32_OperatingSystem | ConvertTo-Html -As List -Property Version,Caption,BuildNumber,Manufacturer -Fragment -PreContent "

Operating System Information

" #The command below will get the Processor information, convert the result to HTML code as table and store it to a variable $ProcessInfo = Get-CimInstance -ClassName Win32_Processor | ConvertTo-Html -As List -Property DeviceID,Name,Caption,MaxClockSpeed,SocketDesignation,Manufacturer -Fragment -PreContent "

Processor Information

" #The command below will get the BIOS information, convert the result to HTML code as table and store it to a variable $BiosInfo = Get-CimInstance -ClassName Win32_BIOS | ConvertTo-Html -As List -Property SMBIOSBIOSVersion,Manufacturer,Name,SerialNumber -Fragment -PreContent "

BIOS Information

" #The command below will get the details of Disk, convert the result to HTML code as table and store it to a variable $DiscInfo = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" | ConvertTo-Html -As List -Property DeviceID,DriveType,ProviderName,VolumeName,Size,FreeSpace -Fragment -PreContent "

Disk Information

" #The command below will get first 10 services information, convert the result to HTML code as table and store it to a variable $ServicesInfo = Get-CimInstance -ClassName Win32_Service | Select-Object -First 10 |ConvertTo-Html -Property Name,DisplayName,State -Fragment -PreContent "

Services Information

" #The command below will combine all the information gathered into a single HTML report $Report = ConvertTo-HTML -Body "$ComputerName $OSinfo $ProcessInfo $BiosInfo $DiscInfo $ServicesInfo" -Title "Computer Information Report" -PostContent "

Creation Date: $(Get-Date)

" #The command below will generate the report to an HTML file $Report | Out-File .\Basic-Computer-Information-Report.html

5 chỉ định nội dung của thẻ
.\Generate-HTML-Report.Ps1
5. Thẻ
.\Generate-HTML-Report.Ps1
5 là một phần của cấu trúc HTML nơi bạn đặt mã cho CSS. Như bạn có thể thấy trong ảnh chụp màn hình bên dưới thẻ
#The command below will get the name of the computer
$ComputerName = "

Computer name: $env:computername

" #The command below will get the Operating System information, convert the result to HTML code as table and store it to a variable $OSinfo = Get-CimInstance -Class Win32_OperatingSystem | ConvertTo-Html -As List -Property Version,Caption,BuildNumber,Manufacturer -Fragment -PreContent "

Operating System Information

" #The command below will get the Processor information, convert the result to HTML code as table and store it to a variable $ProcessInfo = Get-CimInstance -ClassName Win32_Processor | ConvertTo-Html -As List -Property DeviceID,Name,Caption,MaxClockSpeed,SocketDesignation,Manufacturer -Fragment -PreContent "

Processor Information

" #The command below will get the BIOS information, convert the result to HTML code as table and store it to a variable $BiosInfo = Get-CimInstance -ClassName Win32_BIOS | ConvertTo-Html -As List -Property SMBIOSBIOSVersion,Manufacturer,Name,SerialNumber -Fragment -PreContent "

BIOS Information

" #The command below will get the details of Disk, convert the result to HTML code as table and store it to a variable $DiscInfo = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" | ConvertTo-Html -As List -Property DeviceID,DriveType,ProviderName,VolumeName,Size,FreeSpace -Fragment -PreContent "

Disk Information

" #The command below will get first 10 services information, convert the result to HTML code as table and store it to a variable $ServicesInfo = Get-CimInstance -ClassName Win32_Service | Select-Object -First 10 |ConvertTo-Html -Property Name,DisplayName,State -Fragment -PreContent "

Services Information

" #The command below will combine all the information gathered into a single HTML report $Report = ConvertTo-HTML -Body "$ComputerName $OSinfo $ProcessInfo $BiosInfo $DiscInfo $ServicesInfo" -Title "Computer Information Report" -PostContent "

Creation Date: $(Get-Date)

" #The command below will generate the report to an HTML file $Report | Out-File .\Basic-Computer-Information-Report.html

9 đã được bao gồm khi mã HTML được tạo bởi màn hình
Get-CimInstance -Class Win32_OperatingSystem | Select-object Version,Caption,BuildNumber,Manufacturer |  ConvertTo-Html
8.

Hướng dẫn powershell html report - báo cáo html powershell
Mã HTML hiển thị phần tử đầu

Bây giờ, hãy sử dụng CSS để định dạng báo cáo HTML. Đầu tiên, sao chép mã bên dưới và dán nó ở đầu tập lệnh. Mã CSS được gán trong biến

$header = @"


@”
1 sẽ thay đổi định dạng của các văn bản trong báo cáo được đặt trong thẻ
#The command below will get the name of the computer
$ComputerName = "

Computer name: $env:computername

" #The command below will get the Operating System information, convert the result to HTML code as table and store it to a variable $OSinfo = Get-CimInstance -Class Win32_OperatingSystem | ConvertTo-Html -Property Version,Caption,BuildNumber,Manufacturer -Fragment -PreContent "

Operating System Information

" #The command below will get the Processor information, convert the result to HTML code as table and store it to a variable $ProcessInfo = Get-CimInstance -ClassName Win32_Processor | ConvertTo-Html -Property DeviceID,Name,Caption,MaxClockSpeed,SocketDesignation,Manufacturer -Fragment -PreContent "

Processor Information

" #The command below will get the BIOS information, convert the result to HTML code as table and store it to a variable $BiosInfo = Get-CimInstance -ClassName Win32_BIOS | ConvertTo-Html -Property SMBIOSBIOSVersion,Manufacturer,Name,SerialNumber -Fragment -PreContent "

BIOS Information

" #The command below will get the details of Disk, convert the result to HTML code as table and store it to a variable $DiscInfo = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" | ConvertTo-Html -Property DeviceID,DriveType,ProviderName,VolumeName,Size,FreeSpace -Fragment -PreContent "

Disk Information

" #The command below will get first 10 services information, convert the result to HTML code as table and store it to a variable $ServicesInfo = Get-CimInstance -ClassName Win32_Service | Select-Object -First 10 | ConvertTo-Html -Property Name,DisplayName,State -Fragment -PreContent "

Services Information

" #The command below will combine all the information gathered into a single HTML report $Report = ConvertTo-HTML -Body "$ComputerName $OSinfo $ProcessInfo $BiosInfo $DiscInfo $ServicesInfo" -Title "Computer Information Report" -PostContent "

Creation Date: $(Get-Date)

" #The command below will generate the report to an HTML file $Report | Out-File .\Basic-Computer-Information-Report.html

3.

$header = @"


@”

Tiếp theo, sử dụng tham số

#The command below will get the name of the computer
$ComputerName = "

Computer name: $env:computername

" #The command below will get the Operating System information, convert the result to HTML code as table and store it to a variable $OSinfo = Get-CimInstance -Class Win32_OperatingSystem | ConvertTo-Html -As List -Property Version,Caption,BuildNumber,Manufacturer -Fragment -PreContent "

Operating System Information

" #The command below will get the Processor information, convert the result to HTML code as table and store it to a variable $ProcessInfo = Get-CimInstance -ClassName Win32_Processor | ConvertTo-Html -As List -Property DeviceID,Name,Caption,MaxClockSpeed,SocketDesignation,Manufacturer -Fragment -PreContent "

Processor Information

" #The command below will get the BIOS information, convert the result to HTML code as table and store it to a variable $BiosInfo = Get-CimInstance -ClassName Win32_BIOS | ConvertTo-Html -As List -Property SMBIOSBIOSVersion,Manufacturer,Name,SerialNumber -Fragment -PreContent "

BIOS Information

" #The command below will get the details of Disk, convert the result to HTML code as table and store it to a variable $DiscInfo = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" | ConvertTo-Html -As List -Property DeviceID,DriveType,ProviderName,VolumeName,Size,FreeSpace -Fragment -PreContent "

Disk Information

" #The command below will get first 10 services information, convert the result to HTML code as table and store it to a variable $ServicesInfo = Get-CimInstance -ClassName Win32_Service | Select-Object -First 10 |ConvertTo-Html -Property Name,DisplayName,State -Fragment -PreContent "

Services Information

" #The command below will combine all the information gathered into a single HTML report $Report = ConvertTo-HTML -Body "$ComputerName $OSinfo $ProcessInfo $BiosInfo $DiscInfo $ServicesInfo" -Title "Computer Information Report" -PostContent "

Creation Date: $(Get-Date)

" #The command below will generate the report to an HTML file $Report | Out-File .\Basic-Computer-Information-Report.html

5 và gán biến
$header = @"


@”
1. Cập nhật tập lệnh bằng cách sử dụng mã bên dưới.

$Report = ConvertTo-HTML -Body "$ComputerName $OSinfo $ProcessInfo $BiosInfo $DiscInfo $ServicesInfo" `
-Title "Computer Information" -Head $header -PostContent "

Creation Date: $(Get-Date)

"

Khi bạn chạy tập lệnh, báo cáo sẽ được cập nhật như hiển thị bên dưới. Lưu ý rằng phần tử HTML duy nhất bị ảnh hưởng là

#The command below will get the name of the computer
$ComputerName = "

Computer name: $env:computername

" #The command below will get the Operating System information, convert the result to HTML code as table and store it to a variable $OSinfo = Get-CimInstance -Class Win32_OperatingSystem | ConvertTo-Html -Property Version,Caption,BuildNumber,Manufacturer -Fragment -PreContent "

Operating System Information

" #The command below will get the Processor information, convert the result to HTML code as table and store it to a variable $ProcessInfo = Get-CimInstance -ClassName Win32_Processor | ConvertTo-Html -Property DeviceID,Name,Caption,MaxClockSpeed,SocketDesignation,Manufacturer -Fragment -PreContent "

Processor Information

" #The command below will get the BIOS information, convert the result to HTML code as table and store it to a variable $BiosInfo = Get-CimInstance -ClassName Win32_BIOS | ConvertTo-Html -Property SMBIOSBIOSVersion,Manufacturer,Name,SerialNumber -Fragment -PreContent "

BIOS Information

" #The command below will get the details of Disk, convert the result to HTML code as table and store it to a variable $DiscInfo = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" | ConvertTo-Html -Property DeviceID,DriveType,ProviderName,VolumeName,Size,FreeSpace -Fragment -PreContent "

Disk Information

" #The command below will get first 10 services information, convert the result to HTML code as table and store it to a variable $ServicesInfo = Get-CimInstance -ClassName Win32_Service | Select-Object -First 10 | ConvertTo-Html -Property Name,DisplayName,State -Fragment -PreContent "

Services Information

" #The command below will combine all the information gathered into a single HTML report $Report = ConvertTo-HTML -Body "$ComputerName $OSinfo $ProcessInfo $BiosInfo $DiscInfo $ServicesInfo" -Title "Computer Information Report" -PostContent "

Creation Date: $(Get-Date)

" #The command below will generate the report to an HTML file $Report | Out-File .\Basic-Computer-Information-Report.html

3 được áp dụng cho nhãn tên máy tính của Cameron. Các bước trên là một ví dụ tuyệt vời về cách bạn có thể kiểm soát hoặc điều khiển thiết kế của báo cáo HTML bằng CSS.

Hướng dẫn powershell html report - báo cáo html powershell
Báo cáo HTML với CSS bằng tham số đầu

Để thêm thiết kế vào các bảng và nhãn khác được đặt trong thẻ

#The command below will get the name of the computer
$ComputerName = "

Computer name: $env:computername

" #The command below will get the Operating System information, convert the result to HTML code as table and store it to a variable $OSinfo = Get-CimInstance -Class Win32_OperatingSystem | ConvertTo-Html -Property Version,Caption,BuildNumber,Manufacturer -Fragment -PreContent "

Operating System Information

" #The command below will get the Processor information, convert the result to HTML code as table and store it to a variable $ProcessInfo = Get-CimInstance -ClassName Win32_Processor | ConvertTo-Html -Property DeviceID,Name,Caption,MaxClockSpeed,SocketDesignation,Manufacturer -Fragment -PreContent "

Processor Information

" #The command below will get the BIOS information, convert the result to HTML code as table and store it to a variable $BiosInfo = Get-CimInstance -ClassName Win32_BIOS | ConvertTo-Html -Property SMBIOSBIOSVersion,Manufacturer,Name,SerialNumber -Fragment -PreContent "

BIOS Information

" #The command below will get the details of Disk, convert the result to HTML code as table and store it to a variable $DiscInfo = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" | ConvertTo-Html -Property DeviceID,DriveType,ProviderName,VolumeName,Size,FreeSpace -Fragment -PreContent "

Disk Information

" #The command below will get first 10 services information, convert the result to HTML code as table and store it to a variable $ServicesInfo = Get-CimInstance -ClassName Win32_Service | Select-Object -First 10 | ConvertTo-Html -Property Name,DisplayName,State -Fragment -PreContent "

Services Information

" #The command below will combine all the information gathered into a single HTML report $Report = ConvertTo-HTML -Body "$ComputerName $OSinfo $ProcessInfo $BiosInfo $DiscInfo $ServicesInfo" -Title "Computer Information Report" -PostContent "

Creation Date: $(Get-Date)

" #The command below will generate the report to an HTML file $Report | Out-File .\Basic-Computer-Information-Report.html

3 trong báo cáo, tiếp tục cập nhật biến
$header = @"


@”
1 với mã CSS bên dưới.

$header = @"

"@

Sau khi cập nhật và chạy tập lệnh, báo cáo nên được định dạng như dưới đây - nhờ CSS, giờ đây nó rất hấp dẫn và trông chuyên nghiệp.

Hướng dẫn powershell html report - báo cáo html powershell
Báo cáo HTML được định dạng bằng CSS

Sử dụng ID HTML và các thuộc tính lớp trong CSS

Các phần tử HTML là các khối xây dựng của toàn bộ báo cáo HTML của bạn, CSS sử dụng các yếu tố này làm bộ chọn để biết nên áp dụng phong cách ở đâu. Từ các ví dụ trước, mã CSS đã được áp dụng cho các phần tử HTML ________ 78, ________ 78 và

$Report = ConvertTo-HTML -Body "$ComputerName $OSinfo $ProcessInfo $BiosInfo $DiscInfo $ServicesInfo" `
-Title "Computer Information" -Head $header -PostContent "

Creation Date: $(Get-Date)

"

0 trong báo cáo. Nhưng nếu bạn cần áp dụng phong cách khác nhau trên các yếu tố khác nhau? Đây là nơi các thuộc tính ID và lớp xuất hiện. Khi thiết kế báo cáo HTML của bạn, bạn có thể sử dụng ID hoặc lớp để xác định một phần tử.

Xin lưu ý rằng một phần tử HTML chỉ có thể có một ID duy nhất thuộc về phần tử duy nhất đó, trong khi tên lớp có thể được sử dụng bởi nhiều phần tử. Đối với các nhà phát triển web, các thuộc tính này không chỉ để thiết kế trang mà chủ yếu được sử dụng trong kịch bản để xử lý cách trang sẽ phản hồi mọi sự kiện hoặc yêu cầu.

Hãy để áp dụng các thuộc tính ID và lớp trong báo cáo HTML. Như bạn có thể thấy trong đoạn mã bên dưới nhãn ngày tạo được đặt trong thẻ

#The command below will get the name of the computer
$ComputerName = "

Computer name: $env:computername

" #The command below will get the Operating System information, convert the result to HTML code as table and store it to a variable $OSinfo = Get-CimInstance -Class Win32_OperatingSystem | ConvertTo-Html -Property Version,Caption,BuildNumber,Manufacturer -Fragment -PreContent "

Operating System Information

" #The command below will get the Processor information, convert the result to HTML code as table and store it to a variable $ProcessInfo = Get-CimInstance -ClassName Win32_Processor | ConvertTo-Html -Property DeviceID,Name,Caption,MaxClockSpeed,SocketDesignation,Manufacturer -Fragment -PreContent "

Processor Information

" #The command below will get the BIOS information, convert the result to HTML code as table and store it to a variable $BiosInfo = Get-CimInstance -ClassName Win32_BIOS | ConvertTo-Html -Property SMBIOSBIOSVersion,Manufacturer,Name,SerialNumber -Fragment -PreContent "

BIOS Information

" #The command below will get the details of Disk, convert the result to HTML code as table and store it to a variable $DiscInfo = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" | ConvertTo-Html -Property DeviceID,DriveType,ProviderName,VolumeName,Size,FreeSpace -Fragment -PreContent "

Disk Information

" #The command below will get first 10 services information, convert the result to HTML code as table and store it to a variable $ServicesInfo = Get-CimInstance -ClassName Win32_Service | Select-Object -First 10 | ConvertTo-Html -Property Name,DisplayName,State -Fragment -PreContent "

Services Information

" #The command below will combine all the information gathered into a single HTML report $Report = ConvertTo-HTML -Body "$ComputerName $OSinfo $ProcessInfo $BiosInfo $DiscInfo $ServicesInfo" -Title "Computer Information Report" -PostContent "

Creation Date: $(Get-Date)

" #The command below will generate the report to an HTML file $Report | Out-File .\Basic-Computer-Information-Report.html

7.

Get-CimInstance -Class Win32_OperatingSystem | Select-object Version,Caption,BuildNumber,Manufacturer |  ConvertTo-Html
0

Khi báo cáo được tạo và xem trong trình duyệt, nhãn ngày sáng tạo được định dạng như dưới đây.

Hướng dẫn powershell html report - báo cáo html powershell
Báo cáo HTML hiển thị nhãn ngày sáng tạo

Để định dạng nhãn ngày tạo bằng thuộc tính ID - Đầu tiên, hãy gán tên ID ‘CreationDate‘ cho

#The command below will get the name of the computer
$ComputerName = "

Computer name: $env:computername

" #The command below will get the Operating System information, convert the result to HTML code as table and store it to a variable $OSinfo = Get-CimInstance -Class Win32_OperatingSystem | ConvertTo-Html -Property Version,Caption,BuildNumber,Manufacturer -Fragment -PreContent "

Operating System Information

" #The command below will get the Processor information, convert the result to HTML code as table and store it to a variable $ProcessInfo = Get-CimInstance -ClassName Win32_Processor | ConvertTo-Html -Property DeviceID,Name,Caption,MaxClockSpeed,SocketDesignation,Manufacturer -Fragment -PreContent "

Processor Information

" #The command below will get the BIOS information, convert the result to HTML code as table and store it to a variable $BiosInfo = Get-CimInstance -ClassName Win32_BIOS | ConvertTo-Html -Property SMBIOSBIOSVersion,Manufacturer,Name,SerialNumber -Fragment -PreContent "

BIOS Information

" #The command below will get the details of Disk, convert the result to HTML code as table and store it to a variable $DiscInfo = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" | ConvertTo-Html -Property DeviceID,DriveType,ProviderName,VolumeName,Size,FreeSpace -Fragment -PreContent "

Disk Information

" #The command below will get first 10 services information, convert the result to HTML code as table and store it to a variable $ServicesInfo = Get-CimInstance -ClassName Win32_Service | Select-Object -First 10 | ConvertTo-Html -Property Name,DisplayName,State -Fragment -PreContent "

Services Information

" #The command below will combine all the information gathered into a single HTML report $Report = ConvertTo-HTML -Body "$ComputerName $OSinfo $ProcessInfo $BiosInfo $DiscInfo $ServicesInfo" -Title "Computer Information Report" -PostContent "

Creation Date: $(Get-Date)

" #The command below will generate the report to an HTML file $Report | Out-File .\Basic-Computer-Information-Report.html

7 Thẻ. Tên ID nên được đặt bên trong thẻ bắt đầu. Mã cập nhật được hiển thị dưới đây.

Get-CimInstance -Class Win32_OperatingSystem | Select-object Version,Caption,BuildNumber,Manufacturer |  ConvertTo-Html
1

Thứ hai, tạo mã CSS mới để định dạng nhãn ngày tạo. Sử dụng ký hiệu

$Report = ConvertTo-HTML -Body "$ComputerName $OSinfo $ProcessInfo $BiosInfo $DiscInfo $ServicesInfo" `
-Title "Computer Information" -Head $header -PostContent "

Creation Date: $(Get-Date)

"

3 theo sau là tên ID khi khai báo ID trong CSS. Thêm mã CSS bên dưới trong biến
$header = @"


@”
1 sau đó lưu và chạy tập lệnh trong bảng điều khiển PowerShell.

Get-CimInstance -Class Win32_OperatingSystem | Select-object Version,Caption,BuildNumber,Manufacturer |  ConvertTo-Html
2

Sau khi gán ID và tạo mã CSS mới nhắm mục tiêu thuộc tính ID của thẻ

#The command below will get the name of the computer
$ComputerName = "

Computer name: $env:computername

" #The command below will get the Operating System information, convert the result to HTML code as table and store it to a variable $OSinfo = Get-CimInstance -Class Win32_OperatingSystem | ConvertTo-Html -Property Version,Caption,BuildNumber,Manufacturer -Fragment -PreContent "

Operating System Information

" #The command below will get the Processor information, convert the result to HTML code as table and store it to a variable $ProcessInfo = Get-CimInstance -ClassName Win32_Processor | ConvertTo-Html -Property DeviceID,Name,Caption,MaxClockSpeed,SocketDesignation,Manufacturer -Fragment -PreContent "

Processor Information

" #The command below will get the BIOS information, convert the result to HTML code as table and store it to a variable $BiosInfo = Get-CimInstance -ClassName Win32_BIOS | ConvertTo-Html -Property SMBIOSBIOSVersion,Manufacturer,Name,SerialNumber -Fragment -PreContent "

BIOS Information

" #The command below will get the details of Disk, convert the result to HTML code as table and store it to a variable $DiscInfo = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" | ConvertTo-Html -Property DeviceID,DriveType,ProviderName,VolumeName,Size,FreeSpace -Fragment -PreContent "

Disk Information

" #The command below will get first 10 services information, convert the result to HTML code as table and store it to a variable $ServicesInfo = Get-CimInstance -ClassName Win32_Service | Select-Object -First 10 | ConvertTo-Html -Property Name,DisplayName,State -Fragment -PreContent "

Services Information

" #The command below will combine all the information gathered into a single HTML report $Report = ConvertTo-HTML -Body "$ComputerName $OSinfo $ProcessInfo $BiosInfo $DiscInfo $ServicesInfo" -Title "Computer Information Report" -PostContent "

Creation Date: $(Get-Date)

" #The command below will generate the report to an HTML file $Report | Out-File .\Basic-Computer-Information-Report.html

7, báo cáo sẽ được cập nhật như hiển thị bên dưới.

Hướng dẫn powershell html report - báo cáo html powershell
Báo cáo HTML hiển thị nhãn ngày sáng tạo được định dạng bằng CSS qua ID làm bộ chọn

Hãy để áp dụng thuộc tính lớp trong bảng thông tin dịch vụ. Sử dụng CSS thay đổi màu của văn bản thành màu xanh lá cây ** khi giá trị của trạng thái đang chạy và sử dụng màu đỏ khi dừng giá trị.

Hướng dẫn powershell html report - báo cáo html powershell
Báo cáo HTML hiển thị bảng thông tin dịch vụ

Như đã đề cập trước đây, các thuộc tính lớp có thể được gán cho nhiều phần tử HTML. & nbsp; Trong báo cáo HTML, phần tử giữ văn bản đang chạy và dừng là thẻ

$Report = ConvertTo-HTML -Body "$ComputerName $OSinfo $ProcessInfo $BiosInfo $DiscInfo $ServicesInfo" `
-Title "Computer Information" -Head $header -PostContent "

Creation Date: $(Get-Date)

"

6. Sử dụng phương thức
$Report = ConvertTo-HTML -Body "$ComputerName $OSinfo $ProcessInfo $BiosInfo $DiscInfo $ServicesInfo" `
-Title "Computer Information" -Head $header -PostContent "

Creation Date: $(Get-Date)

"

7 của PowerShell, hãy gán tên lớp RunningStatus và StopStatus cho tất cả các thẻ
$Report = ConvertTo-HTML -Body "$ComputerName $OSinfo $ProcessInfo $BiosInfo $DiscInfo $ServicesInfo" `
-Title "Computer Information" -Head $header -PostContent "

Creation Date: $(Get-Date)

"

6 trong Bảng thông tin dịch vụ. Sử dụng các lệnh bên dưới và cập nhật tập lệnh.

Get-CimInstance -Class Win32_OperatingSystem | Select-object Version,Caption,BuildNumber,Manufacturer |  ConvertTo-Html
3

Thêm mã CSS sau đây trong biến

$header = @"


@”
1. Tất cả các thẻ có tên lớp RunningStatus sẽ có giá trị thập lục phân của
$header = @"

"@
0 & nbsp; tương đương với màu xanh lá cây .

Get-CimInstance -Class Win32_OperatingSystem | Select-object Version,Caption,BuildNumber,Manufacturer |  ConvertTo-Html
4

Lưu và chạy tập lệnh. Bảng thông tin dịch vụ trong báo cáo nên được cập nhật như hình dưới đây.

Hướng dẫn powershell html report - báo cáo html powershell
Báo cáo HTML hiển thị bảng thông tin dịch vụ được định dạng bằng CSS thông qua bộ chọn lớp

Dưới đây là bố cục cuối cùng của báo cáo HTML được định dạng bằng CSS.

Hướng dẫn powershell html report - báo cáo html powershell
Bố cục cuối cùng của báo cáo HTML

Dưới đây là các lệnh hoàn chỉnh để tạo html-báo cáo.ps1

Get-CimInstance -Class Win32_OperatingSystem | Select-object Version,Caption,BuildNumber,Manufacturer |  ConvertTo-Html
5

Sự kết luận

Trong bài viết này, bạn đã học cách chuyển đổi các đối tượng (kết quả) thành mã HTML và tạo chúng thành báo cáo HTML.

Tạo báo cáo cho định dạng HTML cho bạn khả năng áp dụng CSS giúp báo cáo dễ dàng hơn để tăng cường và thao tác. Có rất nhiều tài nguyên trực tuyến miễn phí mà bạn có thể sử dụng để tăng cấp các kỹ năng thiết kế mã hóa HTML và CSS của mình.

Tôi hy vọng bài viết này cung cấp cho bạn đủ ý tưởng về cách bạn có thể tạo và cải thiện báo cáo HTML của bạn. Chúc mừng!

Đọc thêm

  • ConvertTo-Html
  • Thu thập thông tin về máy tính
  • HTML và CSS