请求编写PowerShell脚本:通过CSV读取机器名获取显示器序列号
PowerShell Script to Fetch Monitor Serial Numbers from CSV Machine Names
Hey there! I’ve put together a script that’ll automate this process for you—no more manually swapping machine names and pasting serials. Here’s how it works, plus the full code to get you up and running:
How the Script Works
- Reads your original CSV file (so you don’t risk overwriting your raw data)
- For each machine name in the first column, remotely queries the system to pull all connected monitor serial numbers
- Writes the serials (separated by semicolons for readability) to a new CSV file
- Includes basic error handling for offline machines or permission issues
Full Script
# Update these paths to match your actual CSV files $inputCsvPath = "C:\Your\Path\Machines.csv" $outputCsvPath = "C:\Your\Path\Machines_With_Serials.csv" # Import the list of machines from your CSV $machineList = Import-Csv -Path $inputCsvPath # Loop through each machine to fetch serials foreach ($machine in $machineList) { $machineName = $machine."machine name" Write-Host "Processing $machineName..." try { # Use CIM (modern alternative to WMI) to get monitor info remotely $monitors = Get-CimInstance -ClassName WmiMonitorID -ComputerName $machineName -ErrorAction Stop # Convert the raw byte array serial numbers to readable strings $serialNumbers = $monitors | ForEach-Object { $serialBytes = $_.SerialNumberID if ($serialBytes -ne $null) { [System.Text.Encoding]::ASCII.GetString($serialBytes).Trim([char]0) } } | Where-Object { $_ -ne "" } # Join multiple serials with a delimiter (adjust to commas if you prefer) $machine."Monitor Serial Numbers" = $serialNumbers -join "; " } catch { # Log errors for unreachable machines or permission issues Write-Warning "Failed to get data for $machineName : $_" $machine."Monitor Serial Numbers" = "Error: $($_.Exception.Message)" } } # Export the updated list to a new CSV $machineList | Export-Csv -Path $outputCsvPath -NoTypeInformation -Encoding UTF8 Write-Host "Done! Results saved to $outputCsvPath"
Setup & Usage Tips
- CSV Structure: Make sure your input CSV has a column named
machine name(match the exact header—if yours is different, update$machine."machine name"to match). You can add a blankMonitor Serial Numberscolumn beforehand, or the script will create it automatically. - Permissions: Run the script in an elevated PowerShell window (right-click > Run as Administrator), and ensure you have administrative access to the remote machines. Firewalls should allow WMI/CIM traffic too.
- Delimiter Adjustment: If you want commas instead of semicolons for multiple serials, change
-join "; "to-join ", ". - Error Handling: If a machine is offline or you lack access, the script will note the error in the output CSV instead of stopping entirely.
To use it:
- Replace the file paths at the top with your actual CSV locations.
- Run the script.
- Open the output CSV to see each machine’s monitor serials (or error messages) in the corresponding row.
This should fully replace your old VBScript and automate the whole workflow. Let me know if you need tweaks for your specific environment!
内容的提问来源于stack exchange,提问作者SirFrancis
相关产品推荐
相关产品推荐

