樓主,剛剛用 deepSeek 詢問是否需有建議。deepSeek回復如下
程式內容有沒有錯誤
這個批次檔案的內容看起來大致上是正確的,並且能夠執行其預期的功能,即顯示 Windows 系統的各種資訊。不過,有一些小問題和改進建議可以提出來:
變數未定義:
在 $cpu_name_temp 的處理中,有一個條件判斷 if (-not $cpu_name),但 $cpu_name 在這之前並未被定義。這可能會導致錯誤。建議將條件判斷改為 if (-not $cpu_name_temp)。
錯誤處理:
在獲取 $product_key 和 $install_date 時,使用了 try-catch 來處理可能的錯誤,這是一個好的做法。不過,其他部分(如 $cpu_name 和 $gpu_name)也可以考慮加入類似的錯誤處理,以確保程式在遇到問題時不會崩潰。
記憶體大小顯示:
在顯示記憶體大小時,$ram_size_B 的值可能會為 null,這會導致 $ram_size_GB 也為 null。建議在計算 $ram_size_GB 之前,先檢查 $ram_size_B 是否為 null。
顯示卡型號:
在獲取顯示卡型號時,$gpu_name_temp 是一個陣列,但 $gpu_name 的處理方式可能會導致顯示卡型號顯示不完整。建議檢查 $gpu_name_temp 是否為空陣列,並在顯示時進行適當的處理。
暫停功能:
在程式的最後,使用了 :pause 標籤和 goto pause 來實現暫停功能。這雖然可以讓使用者暫停程式,但可能會導致程式無法正常結束。建議使用 pause 指令來實現暫停功能,並在需要時讓使用者按下任意鍵繼續。
程式結束提示:
最後的提示訊息 Write-Host '如要離開此程式,請直接關閉此視窗....' 是正確的,但可以考慮在顯示完所有資訊後,自動結束程式,而不是讓使用者手動關閉視窗。
以下是修正後的程式碼片段:
@echo off
title Win資訊顯示器(by hone7576)
cls
powershell -command ^
"Write-Host '歡迎使用 Windows資訊顯示器 v2 重製版 (WMIC轉PowerShell WMI)' -ForegroundColor DarkGreen; " ^
"Write-Host ''; " ^
"Write-Host '╔═══════════════════════════════。繼續使用即代表同意下列條款。═══════════════════════════════╗' -ForegroundColor Red; " ^
"Write-Host '║ ║' -ForegroundColor Red; " ^
"Write-Host '║ 1.此程式由hone7576開發,保證安全無毒,而且開源免費,如果您是付費獲得的,就代表您被騙了! ║' -ForegroundColor Red; " ^
"Write-Host '║ ║' -ForegroundColor Red; " ^
"Write-Host '║ 2.本人只會將此程式發布到apk.tw論壇,其他來源均為轉載。 ║' -ForegroundColor Red; " ^
"Write-Host '║ ║' -ForegroundColor Red; " ^
"Write-Host '║ 3.此程式僅供廣大網友們能夠 『更加方便的查看 Windows 的各種資訊』, ║' -ForegroundColor Red; " ^
"Write-Host '║ 禁止用於其他用途(例如:盜取他人金鑰),否則後果自負。 ║' -ForegroundColor Red; " ^
"Write-Host '║ ║' -ForegroundColor Red; " ^
"Write-Host '║ 4.此程式需要使用較新的 Windows 功能,舊版 Windows 可能無法使用此程式。 ║' -ForegroundColor Red; " ^
"Write-Host '║ ║' -ForegroundColor Red; " ^
"Write-Host '║ 5.除了此條款之外,歡迎盡情修改此程式。 ║' -ForegroundColor Red; " ^
"Write-Host '║ ║' -ForegroundColor Red; " ^
"Write-Host '║ 6.如果您未經hone7576許可,禁止修改或刪除此條款。 ║' -ForegroundColor Red; " ^
"Write-Host '║ ║' -ForegroundColor Red; " ^
"Write-Host '║ 7.hone7576保留修改此條款之權利。 ║' -ForegroundColor Red; " ^
"Write-Host '║ ║' -ForegroundColor Red; " ^
"Write-Host '╚═══════════════════════════════。繼續使用即代表同意上述條款。═══════════════════════════════╝' -ForegroundColor Red; " ^
"Write-Host ''; " ^
"Write-Host ''; " ^
"Write-Host '3秒後可以跳過此頁面。'; " ^
"Start-Sleep -Seconds 3; " ^
"Write-Host ''; " ^
"Write-Host '您可以跳過了,按下任意按鍵以跳過此頁面....' -ForegroundColor DarkGreen; " ^
"$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown'); " ^
cls ^
"Write-Host ''; " ^
"Write-Host '正在收集 Windows 的資訊....'; " ^
"try { $product_key = (Get-WmiObject -Query 'SELECT OA3xOriginalProductKey FROM SoftwareLicensingService').OA3xOriginalProductKey } catch { $product_key = $null }; " ^
"$windows_type = ((Get-WmiObject -Class Win32_OperatingSystem).Caption -replace 'Microsoft ', '').Trim( ); " ^
"$windows_version = (Get-WmiObject -Class Win32_OperatingSystem).Version; " ^
"$build_number = (Get-WmiObject -Class Win32_OperatingSystem).BuildNumber; " ^
"if ([Environment]::Is64BitOperatingSystem) { $os_arch = '64 位元' } else { $os_arch = '32 位元' }; " ^
"$service_pack = (Get-WmiObject -Class Win32_OperatingSystem).CSDVersion; if ($service_pack) { $service_pack = $service_pack.Trim( ) } else { $service_pack = '無' }; " ^
"try { $install_date = [Management.ManagementDateTimeConverter]::ToDateTime((Get-WmiObject -Class Win32_OperatingSystem).InstallDate); $install_date_str = '{0}年{1}月{2}日' -f $install_date.Year, $install_date.Month, $install_date.Day } catch { $install_date_str = '(未知)' }; " ^
"try { $last_boot = [Management.ManagementDateTimeConverter]::ToDateTime((Get-WmiObject -Class Win32_OperatingSystem).LastBootUpTime); $last_boot_str = '{0}年{1}月{2}日' -f $last_boot.Year, $last_boot.Month, $last_boot.Day } catch { $last_boot_str = '(未知)' }; " ^
"$manufacturer = (Get-WmiObject -Class Win32_ComputerSystem).Manufacturer.Trim( ); " ^
"$model = (Get-WmiObject -Class Win32_ComputerSystem).Model.Trim( ); " ^
"$bios_version = (Get-WmiObject -Class Win32_BIOS).SMBIOSBIOSVersion.Trim( ); " ^
"$cpu_name_temp = (Get-WmiObject -Class Win32_Processor).Name.Trim( ); if (-not $cpu_name_temp) { $cpu_name_temp = '(未知)' }; " ^
"$cpu_name = [string]::Join('、', $cpu_name_temp); " ^
"$gpu_name_temp = Get-WmiObject -Class Win32_VideoController | ForEach-Object { $_.Caption.Trim( ) }; " ^
"$gpu_name = [string]::Join('、', $gpu_name_temp); " ^
"$ram_size_B = (Get-WmiObject -Class Win32_ComputerSystem).TotalPhysicalMemory; if ($ram_size_B) { $ram_size_GB = [math]::Round($ram_size_B / 1GB, 2) } else { $ram_size_GB = $null }; " ^
"$country_code = (Get-WmiObject -Class Win32_OperatingSystem).CountryCode; " ^
cls ^
"Write-Host ''; " ^
"Write-Host 'Windows 版本 :' $windows_type; " ^
"if ($windows_version) { Write-Host 'Windows 詳細版本 :' $windows_version } else { Write-Host 'Windows 詳細版本 :(未知)' }; " ^
"if ($build_number) { Write-Host '建構號 :' $build_number } else { Write-Host '建構號 : (未知)' }; " ^
"Write-Host '系統架構 :' $os_arch; " ^
"if ($country_code) { Write-Host '國家代號 :' $country_code } else { Write-Host '國家代號 : (未知)' }; " ^
"Write-Host '服務包版本 :' $service_pack; " ^
"Write-Host ''; " ^
"if ($product_key) { Write-Host 'Windows 激活金鑰 :' $product_key -ForegroundColor Blue} else { Write-Host 'Windows 激活金鑰 : 無法偵測到金鑰,您的 Windows 可能尚未激活。' }; " ^
"Write-Host ''; " ^
"Write-Host '系統安裝(更新)日期 :' $install_date_str; " ^
"Write-Host '最近一次的開機時間 :' $last_boot_str; " ^
"Write-Host ''; " ^
"Write-Host '處理器型號 :' $cpu_name; " ^
"if ($gpu_name) { Write-Host '顯示卡型號 :' $gpu_name } else { Write-Host '顯示卡型號 : (未知)' }; " ^
"if ($ram_size_GB) { Write-Host '記憶體大小 : 大約為'$ram_size_GB' GB (' $ram_size_B 'B)' } else { Write-Host '記憶體大小 : (未知)' }; " ^
"if ($manufacturer) { Write-Host '系統製造商 :' $manufacturer } else { Write-Host '系統製造商 : 無,您的電腦可能不是套裝機。' }; " ^
"if ($model) { Write-Host '電腦型號 :' $model } else { Write-Host '電腦型號 : 無,您的電腦可能不是套裝機。' }; " ^
"if ($bios_version) { Write-Host 'BIOS 版本 :' $bios_version } else { Write-Host 'BIOS 版本 : (未知)' }; " ^
"Write-Host ''; " ^
"Write-Host '═════════════════════════════════════════════════════════════════'; " ^
"Write-Host '網路詳細資訊 :'; " ^
"Write-Host ''; " ^
"Get-NetIPConfiguration | ForEach-Object { if ($_.IPv4Address) { Write-Host '網路適配器 :'$_.InterfaceAlias -ForegroundColor Yellow; Write-Host ' IPv4 位址 :'($_.IPv4Address.IPAddress); } if ($_.IPv4DefaultGateway) { Write-Host ' 預設閘道 :'$_.IPv4DefaultGateway.NextHop; } Write-Host '' }; " ^
"Write-Host '═════════════════════════════════════════════════════════════════'; " ^
"Write-Host ''; " ^
"Write-Host '如要離開此程式,請直接關閉此視窗....'"
pause
主要修正點:
修正 $cpu_name_temp 的條件判斷。
加入對 $ram_size_B 的檢查。
修正 $gpu_name_temp 的處理方式。
移除不必要的 :pause 標籤和 goto pause,改用 pause 指令。
這些修正應該能夠讓程式更加穩定和易於使用。 |

|