Powershell 获取Pc&;使用一个脚本远程监视信息
代码1远程获取PC信息,代码2远程获取监视器信息。如何将代码2合并到代码1中?我希望输出看起来像PC信息,然后是监视器信息。我试图合并,但犯了很多错误 代码1记录所有信息,如查询的计算机和未查询的计算机Powershell 获取Pc&;使用一个脚本远程监视信息,powershell,Powershell,代码1远程获取PC信息,代码2远程获取监视器信息。如何将代码2合并到代码1中?我希望输出看起来像PC信息,然后是监视器信息。我试图合并,但犯了很多错误 代码1记录所有信息,如查询的计算机和未查询的计算机 Code 1: Courtesy of Alexander Obersht $ArrComputers = gc .\computernames.txt $OutputLog = ".\output.log" $NotRespondingLog = ".\
Code 1: Courtesy of Alexander Obersht
$ArrComputers = gc .\computernames.txt
$OutputLog = ".\output.log"
$NotRespondingLog = ".\notresponding.log"
$ErrorActionPreference = "Stop"
Clear-Host
ForEach ($Computer in $ArrComputers)
{
try
{
$computerSystem = get-wmiobject Win32_ComputerSystem -Computer $Computer
$computerBIOS = get-wmiobject Win32_BIOS -Computer $Computer
$Version = Get-WmiObject -Namespace "Root\CIMv2" `
-Query "Select * from Win32_ComputerSystemProduct" `
-computer $computer | select -ExpandProperty version
}
catch
{
$Computer | Out-File -FilePath $NotRespondingLog -Append -Encoding UTF8
continue
}
$Header = "System Information for: {0}" -f $computerSystem.Name
write-host $Header -BackgroundColor DarkCyan
$Header | Out-File -FilePath $OutputLog -Append -Encoding UTF8
$Output = (@"
-------------------------------------------------------
Model: {0}
Serial Number: {1}
Version: {2}
-------------------------------------------------------
"@) -f $computerSystem.Model, $computerBIOS.SerialNumber, $Version
Write-Host $Output
$Output | Out-File -FilePath $OutputLog -Append -Encoding UTF8
}
Code 2:
$users = gc .\computernames1.txt
gwmi WmiMonitorID -Namespace root\wmi -computername $users |
Select PSComputerName,
@{n="Model";e={[System.Text.Encoding]::ASCII.GetString($_.UserFriendlyName -ne 00)}},
@{n="Serial Number";e={[System.Text.Encoding]::ASCII.GetString($_.SerialNumberID -ne 00)}} |
Format-List | Out-File '.\report.csv'
根据您之前的问题更新了我的解决方案:
$ArrComputers = gc .\computernames.txt
$OutputLog = ".\output.log" # Main log
$NotRespondingLog = ".\notresponding.log" # Logging "unqueried" hosts
$ErrorActionPreference = "Stop" # Or add '-EA Stop' to Get-WmiObject queries
Clear-Host
ForEach ($Computer in $ArrComputers)
{
try
{
$computerSystem = get-wmiobject Win32_ComputerSystem -Computer $Computer
$computerBIOS = get-wmiobject Win32_BIOS -Computer $Computer
$Version = Get-WmiObject -Namespace "Root\CIMv2" `
-Query "Select * from Win32_ComputerSystemProduct" `
-computer $computer | select -ExpandProperty version
$MonitorInfo = gwmi WmiMonitorID -Namespace root\wmi `
-computername $Computer `
| Select PSComputerName, `
@{n="Model";e={[System.Text.Encoding]::ASCII.GetString(`
$_.UserFriendlyName -ne 00)}},
@{n="Serial Number";e={[System.Text.Encoding]::ASCII.GetString(`
$_.SerialNumberID -ne 00)}}
}
catch
{
$Computer | Out-File -FilePath $NotRespondingLog -Append -Encoding UTF8
continue
}
$Header = "System Information for: {0}" -f $computerSystem.Name
# Outputting and logging header.
write-host $Header -BackgroundColor DarkCyan
$Header | Out-File -FilePath $OutputLog -Append -Encoding UTF8
$Output = (@"
-------------------------------------------------------
Model : {0}
Serial Number : {1}
Version : {2}
Monitor Model : {3}
Monitor Serial : {4}
-------------------------------------------------------
"@) -f $computerSystem.Model, $computerBIOS.SerialNumber, $Version, `
$MonitorInfo.Model, $MonitorInfo."Serial Number"
# Ouputting and logging WMI data
Write-Host $Output
$Output | Out-File -FilePath $OutputLog -Append -Encoding UTF8
}
嘿@Alexander Obersht,我尝试了同样的方法,但我收到了“索引(基于零的)必须大于或等于零,并且小于……的大小”错误。此外,此解决方案显示,串行和监视器模型:system.object[]抱歉,我无法再现这些问题-可能是因为在我的计算机上
UserFriendlyName
是$null
。不过,Serial是一个字符串-在控制台输出和日志(PowerShell 2.0)中都是。我一直收到这样的消息:型号:336969序列号:12345548版本:Opticore监视器型号:System.Object[]我会一直尝试排除故障,感谢您的帮助!!监视器序列号:System.Object[]这很奇怪。我不明白GetSting()
如何返回字符串以外的任何内容。如果无法处理输入数组,则会出错。我使用的是Powershell v 4.0 btw